Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1.  
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4.  
  5.  
  6. """
  7. An example client. Run simpleserv.py first before running this.
  8. """
  9.  
  10. from twisted.internet import reactor, protocol
  11. from struct import *
  12.  
  13.  
  14. # a client protocol
  15.  
  16. PING = [
  17. 0x0, 0x0, 0x0, 0x0, 0x0
  18. ]
  19.  
  20. class EchoClient(protocol.Protocol):
  21. """Once connected, send a message, then print the result."""
  22.  
  23. def connectionMade(self):
  24. print "connection made"
  25.  
  26. def dataReceived(self, data):
  27. "As soon as any data is received, write it back."
  28. #print "Server said:", data
  29. print "len=%s, last char=%s" % (len(data), data[len(data) - 1])
  30. if len(data) == 5:
  31. for c in data:
  32. pass
  33. #print hex(ord(c))
  34. #if c == 0x0:
  35. #self.transport.write(data)
  36. #print '%02x' % data
  37.  
  38. #self.transport.loseConnection()
  39.  
  40. def connectionLost(self, reason):
  41. print "connection lost"
  42.  
  43. class EchoFactory(protocol.ClientFactory):
  44. protocol = EchoClient
  45.  
  46. def clientConnectionFailed(self, connector, reason):
  47. print "Connection failed - goodbye!"
  48. reactor.stop()
  49.  
  50. def clientConnectionLost(self, connector, reason):
  51. print "Connection lost - goodbye!"
  52. reactor.stop()
  53.  
  54.  
  55. # this connects the protocol to a server running on port 8000
  56. def main():
  57. f = EchoFactory()
  58. reactor.connectTCP("10.0.0.4", 27000, f)
  59. reactor.run()
  60.  
  61. # this only runs if the module was *not* imported
  62. if __name__ == '__main__':
  63. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement