ldmud/doc/examples/python/echo.py

27 lines
763 B
Python
Raw Permalink Normal View History

2016-01-24 18:36:40 +01:00
# This is just the example from the asyncore documentation,
# a simple echoing server.
import asyncore, asyncore_patch, socket
class EchoHandler(asyncore.dispatcher_with_send):
def handle_read(self):
data = self.recv(8192)
if 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_accepted(self, sock, addr):
print('Incoming connection from %s' % repr(addr))
handler = EchoHandler(sock)
server = EchoServer('localhost', 8080)
def on_reload():
server.close()