Skip to content

Instantly share code, notes, and snippets.

@leonardossz
Created March 22, 2016 16:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leonardossz/029939ec4c7bf79dd2bc to your computer and use it in GitHub Desktop.
Save leonardossz/029939ec4c7bf79dd2bc to your computer and use it in GitHub Desktop.
Python SimpleHTTPServer adapted to use Basic HTTP Authentication method.
# -*- coding: utf-8 -*-
import sys
import base64
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
class AuthenticationHandler(SimpleHTTPRequestHandler):
key = ''
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_AUTHHEAD(self):
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Secure HTTP Environment\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
if self.headers.getheader('Authorization') is None:
self.do_AUTHHEAD()
self.wfile.write('No auth received')
elif self.headers.getheader('Authorization') == 'Basic ' + self.key:
SimpleHTTPRequestHandler.do_GET(self)
else:
self.do_AUTHHEAD()
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('Not authenticated')
if __name__ == '__main__':
if len(sys.argv) < 3:
print "Usage: SecureHTTPServer.py [port] [username:password]"
sys.exit()
AuthenticationHandler.key = base64.b64encode(sys.argv[2])
BaseHTTPServer.test(AuthenticationHandler, BaseHTTPServer.HTTPServer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment