Created
March 4, 2019 12:05
-
-
Save briehanlombaard/03ec5ce131da0891dfe960395faef106 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from twisted.internet import reactor, stdio, protocol | |
from twisted.protocols import basic | |
class Client(basic.LineReceiver): | |
delimeter = '\r\n'.encode('ascii') | |
def connectionMade(self): | |
print('Connection made.') | |
def dataReceived(self, data): | |
print(data.strip()) | |
class Factory(protocol.ClientFactory): | |
protocol = Client | |
def clientConnectionFailed(self, connector, reason): | |
print 'Connection failed:', reason.getErrorMessage() | |
reactor.stop() | |
def clientConnectionLost(self, connector, reason): | |
print 'Connection lost:', reason.getErrorMessage() | |
reactor.stop() | |
class IO(basic.LineReceiver): | |
delimiter = '\n' | |
def __init__(self, host, port): | |
self.c = reactor.connectTCP(host, port, Factory()) | |
def lineReceived(self, line): | |
self.c.transport.write(line) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('host') | |
parser.add_argument('port', type=int) | |
arguments = parser.parse_args() | |
stdio.StandardIO(IO(arguments.host, arguments.port)) | |
reactor.run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment