mirror of
https://github.com/ldmud/ldmud
synced 2026-08-12 14:26:05 -04:00
27 lines
763 B
Python
27 lines
763 B
Python
|
|
# 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()
|