shoutclient.py

来自「大型多人在线游戏开发,该书光盘上附带的源码」· Python 代码 · 共 21 行

PY
21 行
字号
from twisted.internet.protocol import Protocol, ClientFactoryclass Shout(Protocol):             # Class designed to manage a connection.    stringToShout = "Twisted is great!" # a string to send the echo server    def connectionMade(self):      # Method called when connection established.        self.buf = ""              # create an empty buffer        print "Shouting:", repr(self.stringToShout)        self.transport.write(self.stringToShout)    def dataReceived(self, data):  # Method called when data is received.        self.buf += data           # buffer any received data        if self.buf == self.stringToShout: # if we've received the full message            self.transport.loseConnection() # then close the connection.            print "Echoed:", repr(self.stringToShout)            reactor.stop()class ShoutClientFactory(ClientFactory):    def buildProtocol(self, address):        return Shout()              # Build Protocols on incoming connectionsfrom twisted.internet import reactor# connect to local port 1234 and expect an echo.reactor.connectTCP("localhost", 1234, ShoutClientFactory())reactor.run() # run the main loop until the user interrupts it

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?