Python Twisted:如何为本地主机连接使用不同的资源层次结构?
我想对本地主机连接使用一个资源层次结构,对所有其他连接使用另一种资源层次结构。我该怎么办呢?
publicSite = Site(File("/var/www/"))
localhostSite = Site(File("/var/localhost/"))
publicServer = TCPServer(80, publicSite, interface="0.0.0.0")
localhostServer = TCPServer(80, localhostSite, interface="127.0.0.1")
好像不能同时听两个不同的接口。
我收到以下错误:
Traceback (most recent call last):
File "/usr/bin/twistd", line 21, in <module>
run()
File "/usr/lib/python2.5/site-packages/twisted/scripts/twistd.py", line 27, in run
app.run(runApp, ServerOptions)
File "/usr/lib/python2.5/site-packages/twisted/application/app.py", line 379, in run
runApp(config)
File "/usr/lib/python2.5/site-packages/twisted/scripts/twistd.py", line 23, in runApp
_SomeApplicationRunner(config).run()
File "/usr/lib/python2.5/site-packages/twisted/application/app.py", line 158, in run
self.postApplication()
File "/usr/lib/python2.5/site-packages/twisted/scripts/_twistd_unix.py", line 213, in postApplication
startApplication(self.config, self.application)
File "/usr/lib/python2.5/site-packages/twisted/scripts/_twistd_unix.py", line 174, in startApplication
service.IService(application).privilegedStartService()
File "/usr/lib/python2.5/site-packages/twisted/application/service.py", line 228, in privilegedStartService
service.privilegedStartService()
File "/usr/lib/python2.5/site-packages/twisted/application/internet.py", line 68, in privilegedStartService
self._port = self._getPort()
File "/usr/lib/python2.5/site-packages/twisted/application/internet.py", line 86, in _getPort
return getattr(reactor, 'listen'+self.method)(*self.args, **self.kwargs)
File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 467, in listenTCP
p.startListening()
File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 733, in startListening
raise CannotListenError, (self.interface, self.port, le)
twisted.internet.error.CannotListenError: Couldn't listen on 127.0.0.1:80: (98, 'Address already in use').
1
I'd like to use one resource hierarchy for localhost connections and another resource hierarchy for all other connections. How would I go about this?
publicSite = Site(File("/var/www/"))
localhostSite = Site(File("/var/localhost/"))
publicServer = TCPServer(80, publicSite, interface="0.0.0.0")
localhostServer = TCPServer(80, localhostSite, interface="127.0.0.1")
It seems that you can't listen to two different interfaces at the same time.
I get the following error:
Traceback (most recent call last):
File "/usr/bin/twistd", line 21, in <module>
run()
File "/usr/lib/python2.5/site-packages/twisted/scripts/twistd.py", line 27, in run
app.run(runApp, ServerOptions)
File "/usr/lib/python2.5/site-packages/twisted/application/app.py", line 379, in run
runApp(config)
File "/usr/lib/python2.5/site-packages/twisted/scripts/twistd.py", line 23, in runApp
_SomeApplicationRunner(config).run()
File "/usr/lib/python2.5/site-packages/twisted/application/app.py", line 158, in run
self.postApplication()
File "/usr/lib/python2.5/site-packages/twisted/scripts/_twistd_unix.py", line 213, in postApplication
startApplication(self.config, self.application)
File "/usr/lib/python2.5/site-packages/twisted/scripts/_twistd_unix.py", line 174, in startApplication
service.IService(application).privilegedStartService()
File "/usr/lib/python2.5/site-packages/twisted/application/service.py", line 228, in privilegedStartService
service.privilegedStartService()
File "/usr/lib/python2.5/site-packages/twisted/application/internet.py", line 68, in privilegedStartService
self._port = self._getPort()
File "/usr/lib/python2.5/site-packages/twisted/application/internet.py", line 86, in _getPort
return getattr(reactor, 'listen'+self.method)(*self.args, **self.kwargs)
File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 467, in listenTCP
p.startListening()
File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 733, in startListening
raise CannotListenError, (self.interface, self.port, le)
twisted.internet.error.CannotListenError: Couldn't listen on 127.0.0.1:80: (98, 'Address already in use').
1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要绑定到第二个服务器的
0.0.0.0
,而是绑定到服务器上的一个或多个公共地址。 Twisted 不提供任何 API 来枚举主机上的地址,也不提供区分公共地址和私有地址的 API(因为也许您的“公共”地址是 10.xxx,并且防火墙正在转发来自“实际上”的流量) '公共地址)。如果您需要发现您的地址而不是让它们通过配置提供,那么在 POSIX 上,这个 ioctl 示例可能会有所帮助。
Instead of binding to
0.0.0.0
for the second server, bind to one or more public addresses on your server. Twisted doesn't provide any APIs for enumerating the addresses on a host, nor for differentiating between public and private addresses (because perhaps your "public" address is a 10.x.x.x and a firewall is forwarding traffic from an '''actually''' public address).If you need to discover your addresses instead of letting them be supplied by configuration, then on POSIX, this ioctl example might help.