From 7f45025f126f3bee86a78c11d883694ed75656a1 Mon Sep 17 00:00:00 2001 From: neodarz Date: Fri, 10 May 2019 22:49:43 +0200 Subject: Add simple client --- client.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 client.py (limited to 'client.py') diff --git a/client.py b/client.py new file mode 100755 index 0000000..4521e50 --- /dev/null +++ b/client.py @@ -0,0 +1,43 @@ +from twisted.internet import reactor, protocol + +import sys + +class EchoClient(protocol.Protocol): + def connectionMade(self): + self.transport.write(self.factory.data) + + def dataReceived(self, data): + print(data.decode('utf-8')) + self.transport.loseConnection() + + def connectionLost(self, reason): + pass + +class EchoClientFactory(protocol.ClientFactory): + protocol = EchoClient + + def __init__(self, data=None): + self.data = data + + def clientConnectionFailed(self, connector, reason): + print("Connection failed! Server Unvailable! Please check config!") + reactor.stop() + + def clientConnectionLost(self, connector, reason): + reactor.stop() + + +def main(): + data = None + if 1 < len(sys.argv) < 3: + data = sys.argv[1].encode('utf-8') + elif 2 < len(sys.argv) < 4: + data = (sys.argv[1] + " " + sys.argv[2]).encode('utf-8') + elif len(sys.argv) == 1: + data = "help".encode('utf-8') + f = EchoClientFactory(data) + reactor.connectTCP("localhost", 8000, f) + reactor.run() + +if __name__ == '__main__': + main() -- cgit v1.2.1