diff options
author | neodarz <neodarz@neodarz.net> | 2019-05-10 22:49:43 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-05-10 22:49:43 +0200 |
commit | 7f45025f126f3bee86a78c11d883694ed75656a1 (patch) | |
tree | 6b4d470e59175818e6df948c2ea9857cf365d384 | |
parent | fe0916de973bce38d1a58f72dde65b2fa81d33b3 (diff) | |
download | pyParrotZikTCP-7f45025f126f3bee86a78c11d883694ed75656a1.tar.xz pyParrotZikTCP-7f45025f126f3bee86a78c11d883694ed75656a1.zip |
Add simple client
-rwxr-xr-x | client.py | 43 |
1 files changed, 43 insertions, 0 deletions
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() |