Twisted Framework 服务器作为客户端建立连接?

问题描述

首先,让我向您展示我的代码及其返回的错误:

So first off, let me show you my code and the error it returns:

print "before import"
from twisted.internet import protocol #  imports
print "after protocol"
from twisted.internet import reactor
print "after reactor"
from twisted.internet.endpoints import TCP4ServerEndpoint
print "after import"

class Echo(protocol.Protocol):
    """docstring for Echo"""
    def connectionMade(self):
        cADDR = self.clnt = self.transport.getPeer().host
        print "...Connection made with {0}".format(cADDR)
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    """docstring for EchoFactory"""
    def buildProtocol(self, addr):
        return Echo()


server = TCP4ServerEndpoint(reactor, 45002)
server.listen(EchoFactory())
reactor.run()

如您所见,我创建了一些打印语句来准确调试导致问题的导入.现在是错误:

As you can see, I created some print statements to debug exactly which import is causing the issue. Now for the error:

before import
after protocol
Traceback (most recent call last):
  File "C:UsersSa'idDocumentsLearning ProgrammingPythonCore Python Application ProgrammingChapter 2 - Network ProgrammingTwisted	wisted_intro.py", line 9, in <module>
    from twisted.internet import reactor
  File "C:Python27libsite-packages	wistedinterneteactor.py", line 39, in <module>
    default.install()
  File "C:Python27libsite-packages	wistedinternetselectreactor.py", line 196, in install
    reactor = SelectReactor()
  File "C:Python27libsite-packages	wistedinternetselectreactor.py", line 72, in __init__
    posixbase.PosixReactorBase.__init__(self)
  File "C:Python27libsite-packages	wistedinternetase.py", line 499, in __init__
    self.installWaker()
  File "C:Python27libsite-packages	wistedinternetposixbase.py", line 286, in installWaker
    self.waker = self._wakerFactory(self)
  File "C:Python27libsite-packages	wistedinternetposixbase.py", line 81, in __init__
    client.connect(server.getsockname())
  File "C:Python27libsocket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
>>> 

出于某种原因,我的 Twisted 服务器正在尝试建立连接,而实际上,它应该是等待连接的服务器,而不是建立连接.正如您从错误中看到的那样,它在 reactor 导入之前打印,但不在它之后,所以 reactor 确实是这里的问题.我已经在另一个网站上发布了这个但没有取得多大成功,但回复者说,这是因为反应堆试图设置一个 _SocketWaker 并且有东西阻止它设置它.他说关掉你的防火墙可以让它工作,但是在尝试之后,还是返回了同样的错误.请注意,我托管此 Echo() 服务器的端口是端口转发的,因此端口可能不是问题.任何意见将不胜感激.

For some reason, my Twisted server is trying to make connections, when in reality, it should be the one waiting for the connections, not making them. And as you can see from the error, it prints right before the reactor import, but not after it, so the reactor is really the issue here. I've posted this on another website without much success, but the replier said that, it was because the reactor was trying to setup a _SocketWaker and something was blocking it from setting it up. He said that turning off your firewall would make it work, but after trying it, the same error was returned. Just a note, the port I am hosting this Echo() server on is port forwarded, so the port is probably not the issue. Any input would be much appreciated.

谢谢.


解决方案

在 UNIX 上,Twisted 使用管道设置线程唤醒文件描述符.但是,在 Windows 上,匿名管道有几个实现问题和不同 Windows 版本之间的差异,因此它使用套接字对.创建此套接字对涉及连接回 localhost,因此,某些过于激进的防火墙配置可能会触发此区域.

On UNIX, Twisted sets up a thread-waker file descriptor using a pipe. However, on Windows, anonymous pipes have several implementation issues and discrepancies between different Windows versions, so it uses a socket pair. Creating this socket pair involves connecting back to localhost, so, certain overly-aggressive firewall configurations can trigger this area.

相关文章