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
Instead of binding to
0.0.0.0for 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.