-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignClientExample.py
58 lines (42 loc) · 1.42 KB
/
SignClientExample.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
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Basic client for the 'sign server'
periodically does a thing
4/20/2014 SDC
"""
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from SignQueue import SignMessage
import sys
class SignClient(LineReceiver):
counter = 0
def connectionMade(self):
self.sendLine('{"message":"Hello World"}')
self.looper = LoopingCall(self.spitMessage)
self.looper.start(5) # every 5
def spitMessage(self):
message = '{"message":"counter be: %s"}' % SignClient.counter
SignClient.counter = SignClient.counter + 1
self.sendLine(message)
def lineReceived(self, line):
print "receive:", line
if line==self.end:
self.transport.loseConnection()
class SignClientFactory(ClientFactory):
protocol = SignClient
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
factory = SignClientFactory()
reactor.connectTCP('localhost', 6666, factory)
reactor.run()
if __name__ == '__main__':
main()