Created
November 12, 2019 17:35
-
-
Save etigui/d7456ec8ec1730d1065e5a6933fb09ea to your computer and use it in GitHub Desktop.
Simple python web server (GET, POST, HEAD)
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/env python | |
""" | |
Very simple HTTP server in python (Updated for Python 3.7) | |
Usage: | |
./dummy-web-server.py -h | |
./dummy-web-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 | |
from urllib.parse import parse_qs | |
from cgi import parse_header, parse_multipart | |
import json | |
""" | |
from sys import version as python_version | |
from cgi import parse_header, parse_multipart | |
if python_version.startswith('3'): | |
from urllib.parse import parse_qs | |
from http.server import BaseHTTPRequestHandler | |
else: | |
from urlparse import parse_qs | |
from BaseHTTPServer import BaseHTTPRequestHandler | |
""" | |
class S(BaseHTTPRequestHandler): | |
def _set_headers(self): | |
self.send_response(200) | |
self.send_header("Content-type", "text/html") | |
self.end_headers() | |
def _html(self, message): | |
content = f"<html><body><h1>{message}</h1></body></html>" | |
return content.encode("utf8") | |
def do_GET(self): | |
self._set_headers() | |
self.wfile.write(self._html("GET REQUEST")) | |
def do_POST(self): | |
postvars = self.parse_POST() | |
print(json.loads(postvars)) | |
def do_HEAD(self): | |
self._set_headers() | |
def parse_POST(self): | |
ctype, pdict = parse_header(self.headers['content-type']) | |
print(self.headers) | |
if ctype == 'multipart/form-data': | |
postvars = cgi.parse_multipart(self.rfile, pdict) | |
if ctype == 'application/json': | |
length = int(self.headers['content-length']) | |
postvars = self.rfile.read(length).decode('utf8') | |
elif ctype != 'application/x-www-form-urlencoded': | |
length = int(self.headers['content-length']) | |
postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) | |
else: | |
postvars = {} | |
return postvars | |
def run(server_class=HTTPServer, handler_class=S, addr="127.0.0.1", port=7777): | |
server_address = (addr, port) | |
httpd = server_class(server_address, handler_class) | |
print(f"Starting httpd server on {addr}:{port}") | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Run a simple HTTP server") | |
parser.add_argument( | |
"-l", | |
"--listen", | |
default="127.0.0.1", | |
help="Specify the IP address on which the server listens", | |
) | |
parser.add_argument( | |
"-p", | |
"--port", | |
type=int, | |
default=7777, | |
help="Specify the port on which the server listens", | |
) | |
args = parser.parse_args() | |
run(addr=args.listen, port=args.port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment