Jul-14-2018, 07:49 AM
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.
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()