Skip to content

Instantly share code, notes, and snippets.

@Twigonometry
Forked from bradmontgomery/dummy-web-server.py
Last active October 10, 2020 21:53
Show Gist options
  • Save Twigonometry/a619bc5234d39296737e8d00899f23af to your computer and use it in GitHub Desktop.
Save Twigonometry/a619bc5234d39296737e8d00899f23af to your computer and use it in GitHub Desktop.
a minimal http server in python. Responds to GET, HEAD, POST requests, but will fail on anything else.
#!/usr/bin/env python
"""
Very simple HTTP server in python (Updated for Python 3.7)
Code based on implementation by bradmontgomery (https://gist.github.com/bradmontgomery/2219997)
Usage:
./simple-python-server.py -h
./simple-python-server.py -l localhost -p 8000
If "no module named http.server", run:
python3 simple-python-server.py -h
python3 simple-python-server.py -l localhost -p 8000
Send a GET request:
curl http://localhost:8000
Send a HEAD request:
curl -I http://localhost:8000
Send a POST request:
curl -d "foo=bar&bin=baz" http://localhost:8000
"""
import argparse
from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def end_headers (self):
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
def _html(self, message):
"""This just generates an HTML document that includes `message`
in the body. Override, or re-write this do do more interesting stuff.
"""
content = "<html><body><h1>" + message + "</h1></body></html>"
return content.encode("utf8") # NOTE: must return a bytes object!
def do_GET(self):
self._set_headers()
self.wfile.write(self._html("hi!"))
def do_HEAD(self):
self._set_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
self._set_headers()
self.wfile.write(b'<html><body><h1>POST!</h1><pre>' + bytes(post_data) + b'</pre></body></html>')
print(post_data.decode("utf-8"))
def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
server_address = (addr, port)
httpd = server_class(server_address, handler_class)
print("Starting httpd server on " + addr + ":" + str(port))
httpd.serve_forever()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run a simple HTTP server")
parser.add_argument(
"-l",
"--listen",
default="localhost",
help="Specify the IP address on which the server listens",
)
parser.add_argument(
"-p",
"--port",
type=int,
default=8000,
help="Specify the port on which the server listens",
)
args = parser.parse_args()
run(addr=args.listen, port=args.port)
@Twigonometry
Copy link
Author

Forked from bradmontgomery/dummy-web-server.py, with additional code for handling post requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment