twisted 1
2688 ワード
ハローワールドから
reactorプライマリイベントループをシミュレートするコード
Transportは接続を表します.
Transportの方法
1.writeWrite data to the physical connection in a nonblocking manner
非block方式、物理接続へのデータの書き込み
2.writeSequenceWrite a list of strings to the physical connection. Useful when working with lineoriented protocols
行に面したイベント処理ロジックは非常に役立ちます
3.loseConnectionWrite all pending data and then close the connection.
4.getPeerリモートホストのアドレスGet the remote address of the connection
5.getHost独自のアドレスLike getPeer,but returns the address of the local side of the connection
Protocolの方法
makeConnectionでは、protocolは新しい接続を作成することができます.connectionMadeCalled when a connection to another endpoint is made.dataReceivedCalled when data is received across a transport.connectionLostCalled when the connection is shut down.
Protocol FactoriesA new instance of our Echo protocol class is instantiated for every connection and goesaway when the connection terminates. This means that persistent configuration infor�\mation is not saved in the protocol.
protocolオブジェクトは、接続が確立されるにつれて生成され、接続が切断されるにつれて消滅します.
#-*- coding: utf-8 -*-
from twisted.internet import protocol, reactor
# , 。 。
class Echo(protocol. Protocol):
# :dataReceived
def dataReceived(self, data):
self. transport. write(data)
# , buildProtocol
class EchoFactory(protocol. Factory):
# :buildProtocol
def buildProtocol(self, addr):
return Echo()
# reactor 。listenTCP
reactor. listenTCP(8000, EchoFactory())
reactor. run()
#-*- coding: utf-8 -*-
from twisted.internet import reactor, protocol
class EchoClient(protocol. Protocol):
# 2 : connectionMade
def connectionMade(self):
self.transport.write("Hello, world!")
def dataReceived(self, data):
print "Server said:" , data
# transport
self.transport.loseConnection()
class EchoFactory(protocol. ClientFactory):
def buildProtocol(self, addr):
return EchoClient()
# 2 : clientConnectionFailed
def clientConnectionFailed(self, connector, reason):
print "Connection failed."
reactor.stop()
# 3 : clientConnectionLost
def clientConnectionLost(self, connector, reason):
print "Connection lost."
reactor.stop()
# client, server
reactor.connectTCP("localhost" , 8000, EchoFactory())
reactor.run()
# , ,reactor import reactor , 。
reactorプライマリイベントループをシミュレートするコード
while True:
timeout = time_until_next_timed_event()
events = wait_for_events(timeout)
events += timed_events_until(now())
for event in events:
event. process()
Transportは接続を表します.
Transportの方法
1.writeWrite data to the physical connection in a nonblocking manner
非block方式、物理接続へのデータの書き込み
2.writeSequenceWrite a list of strings to the physical connection. Useful when working with lineoriented protocols
行に面したイベント処理ロジックは非常に役立ちます
3.loseConnectionWrite all pending data and then close the connection.
4.getPeerリモートホストのアドレスGet the remote address of the connection
5.getHost独自のアドレスLike getPeer,but returns the address of the local side of the connection
Protocolの方法
makeConnectionでは、protocolは新しい接続を作成することができます.connectionMadeCalled when a connection to another endpoint is made.dataReceivedCalled when data is received across a transport.connectionLostCalled when the connection is shut down.
Protocol FactoriesA new instance of our Echo protocol class is instantiated for every connection and goesaway when the connection terminates. This means that persistent configuration infor�\mation is not saved in the protocol.
protocolオブジェクトは、接続が確立されるにつれて生成され、接続が切断されるにつれて消滅します.