Skip to content

Instantly share code, notes, and snippets.

@methane
Created September 12, 2011 11:47
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 methane/1211084 to your computer and use it in GitHub Desktop.
Save methane/1211084 to your computer and use it in GitHub Desktop.
Echo server with tobikko
#!/usr/bin/env python
import tobikko
import tobikko.socket as socket
def handler(sock):
try:
print "start handler"
recv=sock.recv
send=sock.send
while 1:
buf = recv(4096)
if not buf:
return
send(buf)
finally:
sock.close()
def accepter(sock):
spawn = tobikko.spawn_n
h = handler
while 1:
client = sock.accept()
print "accept"
spawn(h, client)
def main():
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('',5000))
sock.listen(1024)
tobikko.spawn_n(accepter, sock)
tobikko.get_hub().run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment