Skip to content

Instantly share code, notes, and snippets.

@bmcculley
Forked from bradmontgomery/dummy-web-server.py
Last active November 17, 2022 04:02
Show Gist options
  • Save bmcculley/e716d7326d6a7b0edfd6a33feef6840e to your computer and use it in GitHub Desktop.
Save bmcculley/e716d7326d6a7b0edfd6a33feef6840e to your computer and use it in GitHub Desktop.
a minimal http server in python (2/3). Responds to GET, HEAD, POST requests, but will fail on anything else.
#!/usr/bin/env python
"""
Very simple HTTP server in python.
Usage::
./dummy-web-server.py [<port>]
Send a GET request::
curl http://localhost
Send a HEAD request::
curl -I http://localhost
Send a POST request::
curl -d "foo=bar&bin=baz" http://localhost
"""
import sys
if sys.version_info[0] < 3:
# python 2 import
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
else:
# python 3 import
from http.server import BaseHTTPRequestHandler, HTTPServer
class BaseServer(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("<html><body><p>hello, world!</p></body></html>"
.encode('utf-8'))
def do_HEAD(self):
self._set_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
self._set_headers()
self.wfile.write("<html><body><p>POST!</p><p>%s</p></body></html>"
.encode('utf-8') % post_data)
def run(server_class=HTTPServer, handler_class=BaseServer, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('HTTP server running on port %s'% port)
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
@efimovvl
Copy link

efimovvl commented Aug 27, 2017

Hey, just an FYI the POST function does not work in python 3, but works fine in 2.7


Exception happened during processing of request from ('127.0.0.1', 42534)
Traceback (most recent call last):
  File "/usr/lib/python3.4/socketserver.py", line 305, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.4/socketserver.py", line 331, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.4/socketserver.py", line 344, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.4/socketserver.py", line 669, in __init__
    self.handle()
  File "/usr/lib/python3.4/http/server.py", line 398, in handle
    self.handle_one_request()
  File "/usr/lib/python3.4/http/server.py", line 386, in handle_one_request
    method()
  File "dummy-web-server.py", line 46, in do_POST
    .encode('utf-8') % post_data)
TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes'

@OneCricketeer
Copy link

OneCricketeer commented Aug 30, 2017

Could use

self.wfile.write("<html><body><p>POST!</p><p>{}</p></body></html>".format(post_data).encode("utf-8"))

@vitalNohj
Copy link

if sys.version_info[0] < 3:
    self.wfile.write("<html><body<p>POST!</p><p>%s</p></body></html>".encode('utf-8') % post_data)
else:
    self.wfile.write("<html><body<p>POST!</p><p>{}</p></body></html>".format(post_data).encode('utf-8'))

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