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


Messages In This Thread
Converting web server from python 2 to python 3 - by floatingshed - Jul-14-2018, 07:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,589 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  How can i create a server for already existing client using Python? Chapanson 21 7,531 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Python script multi client server sonra 1 2,474 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  python echo server kerzol81 2 2,739 Jan-16-2020, 06:01 PM
Last Post: codexcotechnologies
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 3,826 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  python -m http.server giving invalid syntax echowit 5 7,183 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,696 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