Twisted Framework Server将连接作为客户端?

时间:2015-07-12 08:57:52

标签: python sockets network-programming server twisted

首先,让我告诉你我的代码及其返回的错误:

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()

正如您所看到的,我创建了一些print语句来准确调试导致问题的导入。现在出错:

before import
after protocol
Traceback (most recent call last):
  File "C:\Users\Sa'id\Documents\Learning Programming\Python\Core Python Application Programming\Chapter 2 - Network Programming\Twisted\twisted_intro.py", line 9, in <module>
    from twisted.internet import reactor
  File "C:\Python27\lib\site-packages\twisted\internet\reactor.py", line 39, in <module>
    default.install()
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 196, in install
    reactor = SelectReactor()
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 72, in __init__
    posixbase.PosixReactorBase.__init__(self)
  File "C:\Python27\lib\site-packages\twisted\internet\base.py", line 499, in __init__
    self.installWaker()
  File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 286, in installWaker
    self.waker = self._wakerFactory(self)
  File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 81, in __init__
    client.connect(server.getsockname())
  File "C:\Python27\lib\socket.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()服务器的端口是端口转发的,所以端口可能不是问题。任何意见都会非常感激。

感谢。

1 个答案:

答案 0 :(得分:1)

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