python twisted 建立一个简单连接tcpConnection

2022-05-14 10:29:34 浏览数 (3)

from twisted.internet import reactor,protocol

class QuickDisconnectedProtocol(protocol.Protocol): def connectionMade(self): print("Connected to %s."%self.transport.getPeer().host) print(self.transport.getPeer()) self.transport.loseConnection()

class BasicClientFactory(protocol.ClientFactory): protocol=QuickDisconnectedProtocol def clientConnectionLost(self,connector,reason): print('Lost connection: %s'%reason.getErrorMessage()) reactor.stop() def clientConnectionFailed(self,connector,reason): print('Connection failed: %s'%reason.getErrorMessage()) reactor.stop()

reactor.connectTCP('www.google.com',80,BasicClientFactory()) reactor.run()

0 人点赞