Python Forum
Converting web server from python 2 to python 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting web server from python 2 to python 3
#1
I have been using this script, found online not my work!

In python2 it works perfectly, but in python3 (using 2to3) we get the "a bytes-like object is required, not 'str'" error.
I can't figure out how to encode "sys.argv[2]", if I replace it with static credentials: b'user:pass' the error goes away. This is not a problem actually, I would prefer it, but it creates another problem: setting the port. How/where is sys.argv[1] (the port number) actually used? Any help much appreciated.

import os
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import sys
import base64

os.chdir("H:\\")

key = ""

class AuthHandler(SimpleHTTPRequestHandler):
    ''' Main class to present webpages and authentication. '''
    def do_HEAD(self):
        print "send header"
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_AUTHHEAD(self):
        print "send header"
        self.send_response(401)
        self.send_header('WWW-Authenticate', 'Basic realm=\"Marradio\"')
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        global key
        ''' Present frontpage with user authentication. '''
        if self.headers.getheader('Authorization') == None:
            self.do_AUTHHEAD()
            self.wfile.write('no auth header received')
            pass
        elif self.headers.getheader('Authorization') == 'Basic '+key:
            SimpleHTTPRequestHandler.do_GET(self)
            pass
        else:
            self.do_AUTHHEAD()
            self.wfile.write(self.headers.getheader('Authorization'))
            self.wfile.write('not authenticated')
            pass

def test(HandlerClass = AuthHandler,
         ServerClass = BaseHTTPServer.HTTPServer):
    BaseHTTPServer.test(HandlerClass, ServerClass)


if __name__ == '__main__':
    if len(sys.argv)<3:
        print "usage SimpleAuthServer.py [port] [username:password]"
        sys.exit()
    key = base64.b64encode(sys.argv[2])

    test()
Reply
#2
Please, always include any (and complete, unaltered) errors in error tags.
This is very important as it exposes conditions that lead up to the actual error

Thank You
Reply
#3
Apologies.

Traceback (most recent call last):
  File "E:\py3-server.py", line 52, in <module>
    key = base64.b64encode(sys.argv[2])
  File "C:\Python36\lib\base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
Reply
#4
key = base64.b64encode(sys.argv[2])
whatever's in argv[2], needs to be preceeded witk b, like b'my string'
Reply
#5
Usage: py3-server.py 2727 user:password

sys.argv[2] = user:password
b'user:password' produces the same error.
Reply
#6
I fixed it by changing the line:
Quote:encoded = binascii.b2a_base64(s, newline=False)
to:
Quote:encoded = binascii.b2a_base64(bytes(s, "utf-8"), newline=False)
in base64.py (which makes me nervous!)
Now we have a new error:

Quote:----------------------------------------
Exception happened during processing of request from ('192.168.0.10', 50059)
Traceback (most recent call last):
File "C:\Python36\lib\socketserver.py", line 317, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python36\lib\socketserver.py", line 348, in process_request
self.finish_request(request, client_address)
File "C:\Python36\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python36\lib\socketserver.py", line 696, in __init__
self.handle()
File "C:\Python36\lib\http\server.py", line 418, in handle
self.handle_one_request()
File "C:\Python36\lib\http\server.py", line 406, in handle_one_request
method()
File "E:\Python\FTP_server\py3-Marradio_server.py", line 30, in do_GET
if self.headers.getheader('Authorization') == None:
AttributeError: 'HTTPMessage' object has no attribute 'getheader'
----------------------------------------

OK so getheaders should be get in python 3, I would expect 2to3 to correct that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,280 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  How can i create a server for already existing client using Python? Chapanson 21 7,318 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Python script multi client server sonra 1 2,429 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  python echo server kerzol81 2 2,675 Jan-16-2020, 06:01 PM
Last Post: codexcotechnologies
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 3,756 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  python -m http.server giving invalid syntax echowit 5 7,094 May-17-2018, 02:13 PM
Last Post: echowit
  python socketio-client-2 and nod js socketio server communication is not success vmyadhu2 2 6,642 Oct-02-2017, 04:22 AM
Last Post: vmyadhu2

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020