Created
September 12, 2011 11:47
-
-
Save methane/1211084 to your computer and use it in GitHub Desktop.
Echo server with tobikko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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