Python Assync - Echo server

# pecho.py from socket import * import play loop = play.Loop() async def echo_server(address): sock = socket(AF_INET, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(address) sock.listen(5) sock.setblocking(False) while True: client, addr = await loop.sock_accept(sock) print('Connection from', addr) loop.create_task(echo_handler(client)) async def echo_handler(client): with client: while True: data = await loop.sock_recv(client, 10000) if not data: break await loop.sock_sendall(client, b'Got:'+data) print('Connection closed') loop.create_task(echo_server(('',25000))) loop.run_forever()
Live-coded examples from my PyCon Brasil 2015 Keynote

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.