Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from jacobian/unicorn-socketserver.py
Created October 21, 2009 18:19
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 fogus/215316 to your computer and use it in GitHub Desktop.
Save fogus/215316 to your computer and use it in GitHub Desktop.
"""
Simple forking echo server built with Python's SocketServer library. A more
Pythonic version of http://gist.github.com/203520, which itself was inspired
by http://tomayko.com/writings/unicorn-is-unix.
"""
import os
import SocketServer
class EchoHandler(SocketServer.StreamRequestHandler):
def handle(self):
self.wfile.write('Child %s echo>' % os.getpid())
self.wfile.flush()
message = self.rfile.readline()
self.wfile.write(message)
print "Child %s echo'd: %r" % (os.getpid(), message)
if __name__ == '__main__':
server = SocketServer.ForkingTCPServer(('localhost', 4242), EchoHandler)
print "Server listening on localhost:4242..."
try:
server.serve_forever()
except KeyboardInterrupt:
print "\nbailing..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment