Skip to content

Instantly share code, notes, and snippets.

@fxsjy
Created April 26, 2013 06:23
Show Gist options
  • Select an option

  • Save fxsjy/5465353 to your computer and use it in GitHub Desktop.

Select an option

Save fxsjy/5465353 to your computer and use it in GitHub Desktop.
SimpleAuthServer: A SimpleHTTPServer with authentication
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()
@rubo77

rubo77 commented Jan 29, 2016

Copy link
Copy Markdown

This works perfect. Can you migrate this to github and add two more features?

  • a definable stylesheet to the options
  • UTF-8 support

ghost commented Mar 31, 2016

Copy link
Copy Markdown

@rubo77 I can, contact me for my hourly rates.

@dawsonpaul

Copy link
Copy Markdown

Any chance you can wrap an https server socket around it?

import BaseHTTPServer, SimpleHTTPServer
import ssl

httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='path/to/localhost.pem', server_side=True)
httpd.serve_forever()

@goya191

goya191 commented Jul 29, 2016

Copy link
Copy Markdown

I have extended the server to use SSL and imported it to github
https://github.com/goya191/SimpleAuthServerSSL.py

@tamberg

tamberg commented Oct 31, 2016

Copy link
Copy Markdown

Hi Sun, would you mind to add a open source license to this example? Ideally something liberal like MIT or Apache 2.

@tianhuil

tianhuil commented Jun 6, 2017

Copy link
Copy Markdown

Here's a version that's pip installable https://github.com/tianhuil/SimpleHTTPAuthServer and freely licensable version or just run:

Installation

pip install git+git://github.com/tianhuil/SimpleHTTPAuthServer.git@master

Usage

python -m SimpleHTTPAuthServer -h

@Cristy94

Cristy94 commented Nov 9, 2017

Copy link
Copy Markdown

How do you stop it? :D

@tatethurston

Copy link
Copy Markdown

This is great, thank you!

@zukkie777

Copy link
Copy Markdown

Please let me know the update schedule, from python2 to python3.

When will it be updated?

@zukkie777

Copy link
Copy Markdown

I updated to python 3 but it is not handled properly,
(Updated using 2to3.)

Traceback (most recent call last):
   File "SimpleAuthServer.py", line 41, in
     key = base64.b64encode (sys.argv [2])
   File "/opt/rh/rh-python36/root/usr/lib64/python3.6/base64.py", line 58, in b64encode
     encoded = binascii.b2a_base64 (s, newline = False)
TypeError: a bytes-like object is required, not 'str'

@fmakawa

fmakawa commented Oct 1, 2018

Copy link
Copy Markdown

Here's a version that's pip installable https://github.com/tianhuil/SimpleHTTPAuthServer and freely licensable version or just run:

Installation

pip install git+git://github.com/tianhuil/SimpleHTTPAuthServer.git@master

Usage

python -m SimpleHTTPAuthServer -h

how would this work with multiple user names and passwords?

@Snape3058

Snape3058 commented Jan 16, 2019

Copy link
Copy Markdown

I updated to python 3 but it is not handled properly,
(Updated using 2to3.)

Traceback (most recent call last):
   File "SimpleAuthServer.py", line 41, in
     key = base64.b64encode (sys.argv [2])
   File "/opt/rh/rh-python36/root/usr/lib64/python3.6/base64.py", line 58, in b64encode
     encoded = binascii.b2a_base64 (s, newline = False)
TypeError: a bytes-like object is required, not 'str'

I did not try the 2to3 converted version, but by modifying the 95 line of SimpleHTTPAuthServer/main.py to

SimpleHTTPAuthHandler.KEY = base64.b64encode(args.key.encode("utf-8"))

can make the program start. View here for more info.
(Although some other errors happened.)

@mauler

mauler commented Oct 31, 2019

Copy link
Copy Markdown

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

@ebrahimiali

Copy link
Copy Markdown

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