Skip to content

Instantly share code, notes, and snippets.

@glarrain
Created December 21, 2015 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glarrain/d928ebfce51b77ad2481 to your computer and use it in GitHub Desktop.
Save glarrain/d928ebfce51b77ad2481 to your computer and use it in GitHub Desktop.
simplest HTTP (using WSGI) server ever
#!/usr/bin/env python3
from datetime import datetime
from wsgiref.simple_server import make_server
def my_application(environ, start_response):
path = environ['PATH_INFO']
method = environ['REQUEST_METHOD']
print("received a HTTP request. path: %s, method: %s" % (path, method))
response_str = "piece of cake! current datetime is %s" % datetime.now()
response_bytes = response_str.encode('utf-8')
status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]
start_response(status, headers)
return [response_bytes]
httpd = make_server('', 8000, my_application)
print("Serving HTTP on port 8000 until you kill me...")
# handle requests until process is killed
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment