Last active
November 13, 2024 16:11
-
-
Save bradmontgomery/2219997 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.
This file contains 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 | |
This code is available for use under the MIT license. | |
---- | |
Copyright 2021 Brad Montgomery | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | |
associated documentation files (the "Software"), to deal in the Software without restriction, | |
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial | |
portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | |
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import argparse | |
from http.server import HTTPServer, 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): | |
"""This just generates an HTML document that includes `message` | |
in the body. Override, or re-write this do do more interesting stuff. | |
""" | |
content = f"<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): | |
# Doesn't do anything with posted data | |
self._set_headers() | |
self.wfile.write(self._html("POST!")) | |
def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000): | |
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="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) |
Just tumbled into this myself. ! fantastic.
For python 3.7 on windows 10, I had to add decode to the post_data.
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
pd = post_data.decode("utf-8") # <-------- ADD this line
self._set_headers()
self.wfile.write(self._html("POST! "+pd))
I need help, for the 'Content-Length' its always 0, is there something i'm doing wrong?
Looks great! only missing multi-thread/daemon feature ;)
Great Simple Little Utility. For those asking about extra features like response codes etc. This is a VERY SIMPLE Server. It does as what's advertised.
This probably still needs a 404 somewhere to show how to send proper http status numbers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good start!