Created
June 10, 2013 20:07
-
-
Save meikj/5751811 to your computer and use it in GitHub Desktop.
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 asyncore | |
import socket | |
HOST = 'localhost' | |
PORT = 8080 | |
class EchoHandler(asyncore.dispatcher_with_send): | |
def handle_read(self): | |
data = self.recv(8192) | |
if data: | |
print('%s: recv: %s\tsend: %s' % (self.addr, data, data)) | |
self.send(data) | |
class EchoServer(asyncore.dispatcher): | |
def __init__(self, host, port): | |
asyncore.dispatcher.__init__(self) | |
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.set_reuse_addr() | |
self.bind((host, port)) | |
self.listen(5) | |
def handle_accept(self): | |
pair = self.accept() | |
if pair is not None: | |
sock, addr = pair | |
print('Incoming connection from %s' % repr(addr)) | |
handler = EchoHandler(sock) | |
handler.addr = addr | |
def main(): | |
server = EchoServer(HOST, PORT) | |
print('Server started on %s:%s' % (HOST, PORT)) | |
try: | |
asyncore.loop() | |
except KeyboardInterrupt: | |
pass | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment