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() |
This comment has been minimized.
This comment has been minimized.
@rubo77 I can, contact me for my hourly rates. |
This comment has been minimized.
This comment has been minimized.
Any chance you can wrap an https server socket around it? import BaseHTTPServer, SimpleHTTPServer httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler) |
This comment has been minimized.
This comment has been minimized.
I have extended the server to use SSL and imported it to github |
This comment has been minimized.
This comment has been minimized.
Hi Sun, would you mind to add a open source license to this example? Ideally something liberal like MIT or Apache 2. |
This comment has been minimized.
This comment has been minimized.
Here's a version that's pip installable https://github.com/tianhuil/SimpleHTTPAuthServer and freely licensable version or just run: Installationpip install git+git://github.com/tianhuil/SimpleHTTPAuthServer.git@master Usagepython -m SimpleHTTPAuthServer -h |
This comment has been minimized.
This comment has been minimized.
How do you stop it? :D |
This comment has been minimized.
This comment has been minimized.
This is great, thank you! |
This comment has been minimized.
This comment has been minimized.
Please let me know the update schedule, from python2 to python3. When will it be updated? |
This comment has been minimized.
This comment has been minimized.
I updated to python 3 but it is not handled properly, Traceback (most recent call last): |
This comment has been minimized.
This comment has been minimized.
how would this work with multiple user names and passwords? |
This comment has been minimized.
This comment has been minimized.
I did not try the 2to3 converted version, but by modifying the 95 line of SimpleHTTPAuthServer/main.py to
can make the program start. View here for more info. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Is any chance to support post request as well, probably implement it by BaseHTTPRequestHandler? |
This comment has been minimized.
This works perfect. Can you migrate this to github and add two more features?