我有一個TCP客戶端在我的扭曲應用下面的代碼不同的日誌文件:打印到基於雙絞線中的客戶端應用程序連接
from twisted.internet import reactor, protocol
from twisted.python import log
import time
class EchoClient(protocol.Protocol):
def connectionMade(self):
if(self.factory.server_name == 'server1'):
log.startLogging(open('server1.log','w'))
else:
log.startLogging(open('server2.log','w'))
log.msg("Connected to "+self.factory.server_name)
def connectionLost(self,reason):
log.msg("Connection is lost " + reason.getErrorMessage())
def dataReceived(self,data):
log.msg("Server said: " + data)
class EchoFactory(protocol.ClientFactory):
def __init__(self,server_name):
self.server_name = server_name
def buildProtocol(self, addr):
client = EchoClient()
client.factory = self
return client
def clientConnectionFailed(self, connector, reason):
reactor.stop()
def clientConnectionLost(self, connector, reason):
time.sleep(10)
connector.connect()
conn1 = EchoFactory('server1')
conn2 = EchoFactory('server2')
reactor.connectTCP("localhost",8000,conn1)
reactor.connectTCP("localhost",8001,conn2)
reactor.run()
現在,當我運行程序我看到server1.log既有Connected to server1
和Connected to server2
線,而server2.log只有Connected to server2
。
我該如何解決這個問題?謝謝。