Python Twisted:如何为本地主机连接使用不同的资源层次结构?

发布于 2024-11-08 17:01:15 字数 2057 浏览 0 评论 0原文

我想对本地主机连接使用一个资源层次结构,对所有其他连接使用另一种资源层次结构。我该怎么办呢?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佞臣 2024-11-15 17:01:15

不要绑定到第二个服务器的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文