Created
April 26, 2013 06:23
-
-
Save fxsjy/5465353 to your computer and use it in GitHub Desktop.
SimpleAuthServer: A SimpleHTTPServer with authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BaseHTTPServer | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
import sys | |
import base64 | |
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=\"Test\"') | |
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() |
Is any chance to support post request as well, probably implement it by BaseHTTPRequestHandler?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python3 Version, refactored to behave like python3 -m http.serve helper. Added --username and --password for basic HTTP Auth. (Authorization: Basic)
https://gist.github.com/mauler/593caee043f5fe4623732b4db5145a82