Created
May 18, 2011 06:03
-
-
Save progrium/978057 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 asyncore, socket, sys | |
| class MyClient(asyncore.dispatcher): | |
| def __init__(self, host, port, name): | |
| asyncore.dispatcher.__init__(self) | |
| self.name = name | |
| self.create_socket(socket.AF_INET, socket.SOCK_STREAM) | |
| self.connect( (host, port) ) | |
| print 'Started to connect.' | |
| def handle_connect(self): | |
| print "Connected." | |
| self.send("%s\n" % self.name) | |
| print "Sent name." | |
| def handle_close(self): | |
| self.close() | |
| print 'Lost connection.' | |
| def handle_read(self): | |
| print ">>>", self.recv(1024) | |
| script, host, port, name = sys.argv | |
| client = MyClient(host, int(port), name) | |
| asyncore.loop() |
This file contains hidden or 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 sys | |
| from twisted.internet import reactor | |
| from twisted.internet.protocol import Protocol, ClientFactory | |
| class MyProtocol(Protocol): | |
| def connectionMade(self): | |
| self.transport.write("%s\n" % self.factory.name) | |
| print "Sent name." | |
| def dataReceived(self, data): | |
| print ">>>", data | |
| class MyClientFactory(ClientFactory): | |
| def __init__(self, name): | |
| self.name = name | |
| def startedConnecting(self, connector): | |
| print 'Started to connect.' | |
| def buildProtocol(self, addr): | |
| print 'Connected.' | |
| p = MyProtocol() | |
| p.factory = self | |
| return p | |
| def clientConnectionLost(self, connector, reason): | |
| print 'Lost connection.' | |
| script, host, port, name = sys.argv | |
| reactor.connectTCP(host, int(port), MyClientFactory(name)) | |
| reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment