Skip to content

Instantly share code, notes, and snippets.

@tomazas
Created June 29, 2023 13:27
Show Gist options
  • Save tomazas/357c7d018f1853bf3bcaa68ca92176ba to your computer and use it in GitHub Desktop.
Save tomazas/357c7d018f1853bf3bcaa68ca92176ba to your computer and use it in GitHub Desktop.
Simple Python 2 HTTP multi-threaded server
# Python 2 HTTP GET/POST multi-threaded server
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
hostName = "0.0.0.0"
serverPort = 8080
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
self.do_GET()
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write("{\"hello\":\"world\"}")
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
print "starting %s:%d"%(hostName, serverPort)
httpd = ThreadedHTTPServer((hostName, serverPort), Handler)
print "started. serving requests..."
httpd.serve_forever()
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment