forked from philbowles/H4AsyncTCP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathechoserver.py
32 lines (25 loc) · 817 Bytes
/
echoserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import asyncore
import socket
class EchoHandler(asyncore.dispatcher_with_send):
def handle_read(self):
data = self.recv(32768)
print("RX ",len(data))
#print(data)
if(data):
print("TX ",len(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)
server = EchoServer('0.0.0.0', 8007)
asyncore.loop()