#!/usr/bin/python """A very minimal multithreaded HTTP server in 12 lines using plain sockets. Python has an HTTP server library, so this is sort of pointless except to show how little computation is needed to implement a compatible HTTP server. """ import socket, thread, sys, cgi def main(): s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # avoid errno 98 s.bind(('', int(sys.argv[1]) if sys.argv[1:] else 8080)) s.listen(5) while True: thread.start_new_thread(serve, s.accept()) def serve(c, _): p = cgi.escape(c.makefile().readline().split()[1]) # escape to avoid XSS c.send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\nyeah, %s" % p) # the GC takes care of this on CPython: c.shutdown(socket.SHUT_RDWR) if __name__ == '__main__': main()