Skip to content

Instantly share code, notes, and snippets.

@millsy
Last active April 16, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millsy/ed1c54a033ac25e992cb0d873393193f to your computer and use it in GitHub Desktop.
Save millsy/ed1c54a033ac25e992cb0d873393193f to your computer and use it in GitHub Desktop.
Simple HTTP Server
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/json')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write('{"type":"GET"}')
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
print post_data;
self._set_headers()
self.wfile.write('{"type":"POST"}')
def run(server_class=HTTPServer, handler_class=S, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...' + str(port)
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment