class ServerClient Task def __init__ self sock None super ServerClient

 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
class ServerClient(Task):
def __init__(self, sock=None):
super(ServerClient, self).__init__()
self.sock = sock
self.buffer = ProtoBuffer()
def connect(self, port):
self.sock = socket()
try:
self.sock.connect(('127.0.0.1', port))
except Exception, e:
return
self.sock.send(self.buffer.compile((0,'hello world')))
def handler(self):
while self.sock.connected:
try:
self.buffer.feed(self.sock.recv(1024))
for p in self.buffer:
if p[0] == 0:
print 'Ping %d' % p[1]
self.sock.send(self.buffer.compile((0,p[1])))
except socket.error:
break