-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnetclient.py
47 lines (35 loc) · 1.22 KB
/
netclient.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pickle
import StringIO
from twisted.internet import reactor, protocol
from twisted.protocols.basic import LineReceiver
from stats import Stats
from update import Update, nullUpdate
class TrotterSub(LineReceiver):
#def __init__(self):
# self.factory.transport = self.transport
def connectionMade(self):
self.factory.transport = self
self.sendLine(pickle.dumps(nullUpdate, 2))
def lineReceived(self, data):
stringIO = StringIO.StringIO(data)
up = pickle.Unpickler(stringIO)
s = up.load()
while s is not None:
self.factory.handler.handleUpdate(s)
try:
s = up.load()
except EOFError:
print "EOF when reading from socket"
break
def connectionLost(self, reason):
print "connection lost"
class TrotterSubFactory(protocol.ClientFactory):
protocol = TrotterSub
def __init__(self, handler):
self.handler = handler
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop()