Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (c) Twisted Matrix Laboratories.
- # See LICENSE for details.
- """
- An example client. Run simpleserv.py first before running this.
- """
- from twisted.internet import reactor, protocol
- from struct import *
- # a client protocol
- PING = [
- 0x0, 0x0, 0x0, 0x0, 0x0
- ]
- class EchoClient(protocol.Protocol):
- """Once connected, send a message, then print the result."""
- def connectionMade(self):
- print "connection made"
- def dataReceived(self, data):
- "As soon as any data is received, write it back."
- #print "Server said:", data
- print "len=%s, last char=%s" % (len(data), data[len(data) - 1])
- if len(data) == 5:
- for c in data:
- pass
- #print hex(ord(c))
- #if c == 0x0:
- #self.transport.write(data)
- #print '%02x' % data
- #self.transport.loseConnection()
- def connectionLost(self, reason):
- print "connection lost"
- class EchoFactory(protocol.ClientFactory):
- protocol = EchoClient
- def clientConnectionFailed(self, connector, reason):
- print "Connection failed - goodbye!"
- reactor.stop()
- def clientConnectionLost(self, connector, reason):
- print "Connection lost - goodbye!"
- reactor.stop()
- # this connects the protocol to a server running on port 8000
- def main():
- f = EchoFactory()
- reactor.connectTCP("10.0.0.4", 27000, f)
- reactor.run()
- # this only runs if the module was *not* imported
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement