-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.py
52 lines (39 loc) · 1.5 KB
/
client.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
from twisted.internet import reactor, protocol
from twisted.internet.protocol import ReconnectingClientFactory as ClFactory
from twisted.internet.endpoints import TCP4ClientEndpoint
from sys import stderr
import json
class Client(protocol.Protocol):
def __init__(self):
print("Created")
reactor.callInThread(self.message_input)
@staticmethod
def __encode_json(**kwargs):
return json.dumps(kwargs)
def send_message(self, **kwargs):
self.transport.write(self.__encode_json(**kwargs).encode("utf-8"))
def message_input(self):
while True:
self.send_message(value=input("value:"), type=input("type:"))
def dataReceived(self, data):
try:
data = json.loads(data.decode("utf-8"))
except UnicodeDecodeError or json.JSONDecodeError:
print("Something went wrong :(", file=stderr)
return
if data['type'] == 'error':
print(data.get('value', "Unknown error"), file=stderr)
else:
print(data.get('value', "No value in the message"))
class ClientFactory(ClFactory):
def clientConnectionLost(self, connector, unused_reason):
self.retry(connector)
def clientConnectionFailed(self, connector, reason):
print(reason)
self.retry(connector)
def buildProtocol(self, addr):
return Client()
if __name__ == '__main__':
endpoint = TCP4ClientEndpoint(reactor, 'localhost', 12345)
endpoint.connect(ClientFactory())
reactor.run()