Created
December 29, 2011 19:29
-
-
Save adrianratnapala/1535780 to your computer and use it in GitHub Desktop.
Tiny web servers in python
This file contains hidden or 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
#!/usr/bin/python3 | |
# small web server that instruments "GET" but then serves up files | |
# to serve files using zero lines of code, do | |
# | |
# python -m http.server 8080 # python 3 | |
# | |
# or | |
# | |
# python -m SimpleHTTPServer 8080 # python 2 | |
# | |
# I shamelessly snarfed from Gary Robinson | |
# http://www.garyrobinson.net/2004/03/one_line_python.html | |
# | |
import http.server | |
class Handler(http.server.SimpleHTTPRequestHandler) : | |
# A new Handler is created for every incomming request tho do_XYZ | |
# methods correspond to different HTTP methods. | |
def do_GET(s) : | |
print('-----------------------') | |
print('GET %s (from client %s)' % (s.path, s.client_address)) | |
print(s.headers) | |
super(Handler, s).do_GET() #inherited do_GET serves dirs&files. | |
s = http.server.HTTPServer( ('', 8080), Handler ) | |
s.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment