[Twisted-web] Listening on 2 addresses
Tom Most
twm at freecog.net
Sat Mar 30 18:51:08 MDT 2019
Hi Axel,
makeService() returns an IService. twisted.internet.application.MultiService[1] is an IService that composes other services (it's an IServiceCollection). The implementation would be something like:
def makeService(self, options):
ipv4 = internet.TCPServer(int(options['port'], meteo_factory, interface=options['ipv4_address'])
ipv6 = internet.TCPServer(int(options['port'], meteo_factory, interface=options['ipv6_address'])
root = MultiService()
ipv4.setServiceParent(root)
ipv6.setServiceParent(root)
return root
You might prefer StreamServerEndpointService[2] to TCPServer. String endpoint descriptions[3] are pretty convenient for CLI use.
[1]: https://twistedmatrix.com/documents/current/api/twisted.application.service.MultiService.html
[2]: https://twistedmatrix.com/documents/current/api/twisted.application.internet.StreamServerEndpointService.html
[3]: https://twistedmatrix.com/documents/current/api/twisted.internet.endpoints.html#serverFromString
--
Tom Most
twm at freecog.net
On Fri, Mar 29, 2019, at 11:39 AM, Axel Rau wrote:
> I have a simple web application with the plugin below.
> It works fine with IPv4 or IPv6. The host has 2 addresses
> (IPv4 + IPv6 = dual stack).
> How can I extend the plugin to let my server listen on both
> addresses?
>
> Please advice,
> Axel
>
> PS: The plugin:
>
> from zope.interface import implementer
>
> from twisted.application.service import IServiceMaker
> from twisted.application import internet
> from twisted.plugin import IPlugin
> from twisted.python import usage
>
> from meteo import meteo_factory
>
> class Options(usage.Options):
> optParameters = [ ['port', 'p', 80, 'The port number to listen on.'],
> ['ipv4_address', '4', None, 'The IPv4 address to listen on.'],
> ['ipv6_address', '6', None, 'The IPv6 address to listen on.'],
> ['host', 'l', None, 'The hostname where to listen at']]
>
> @implementer(IServiceMaker, IPlugin)
> class MeteoServiceMaker(object):
> tapname = 'meteo'
> description = 'A web application for the meteo package'
> options = Options
>
> def makeService(self, options):
> """
> Constructs a TCP server from a factory defined in meteo.py
> """
> return internet.TCPServer( int(options['port']),
> meteo_factory,
> interface=options['ipv6_address'])
>
> serviceMaker = MeteoServiceMaker()
>
> ---
> PGP-Key:29E99DD6 ☀ computing @ chaos claudius
More information about the Twisted-web
mailing list