shoutclient.py
Upload User: ghyvgy
Upload Date: 2009-05-26
Package Size: 547k
Code Size: 1k
Category:

Other Games

Development Platform:

Python

  1. from twisted.internet.protocol import Protocol, ClientFactory
  2. class Shout(Protocol):             # Class designed to manage a connection.
  3.     stringToShout = "Twisted is great!" # a string to send the echo server
  4.     def connectionMade(self):      # Method called when connection established.
  5.         self.buf = ""              # create an empty buffer
  6.         print "Shouting:", repr(self.stringToShout)
  7.         self.transport.write(self.stringToShout)
  8.     def dataReceived(self, data):  # Method called when data is received.
  9.         self.buf += data           # buffer any received data
  10.         if self.buf == self.stringToShout: # if we've received the full message
  11.             self.transport.loseConnection() # then close the connection.
  12.             print "Echoed:", repr(self.stringToShout)
  13.             reactor.stop()
  14. class ShoutClientFactory(ClientFactory):
  15.     def buildProtocol(self, address):
  16.         return Shout()              # Build Protocols on incoming connections
  17. from twisted.internet import reactor
  18. # connect to local port 1234 and expect an echo.
  19. reactor.connectTCP("localhost", 1234, ShoutClientFactory())
  20. reactor.run() # run the main loop until the user interrupts it