deployment with twistd of simple web application
Hi, just doing my 1st dynamic web application with twisted. I have some working python modules, where the entry point is: --- resource = RootWeatherPage() meteoFactory = Site(resource) reactor.listenTCP(80, factory, interface='some IP4') reactor.listenTCP(80, factory, interface='some IP6') reactor.run() --- Now I want to use twistd web. How do I do that? Thanks Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
Hi Axel, You probably want to read this: https://twistedmatrix.com/documents/current/core/howto/application.html There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :) hth lvh
Am 05.11.2013 um 13:25 schrieb Laurens Van Houtven <_@lvh.io>:
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html This explains the echo service step by step, but I still do not know how to do it with twistd *web* . Looking at the source of the web plugin did not enlighten me. I tried:
meteo.tac: --- application = service.Application('meteo') meteoService = internet.TCPserver(80, meteoFactory(), interface=some IP4') # interface='some IP 6') meteoService.setServiceParent(application) --- and changed my main module to --- … resource = RootWeatherPage() meteoFactory = Site(resource) #reactor.listenTCP(80, factory, interface='91.216.35.82') #reactor.listenTCP(80, factory, interface='2a02:d40:2:2::82') #reactor.run()… --- twisted does not start up. Interrupting it, gives "exceptions.ImportError: cannot import name meteoFactory"
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
Hi Axel, Looks like you're missing a bunch of imports; where is meteoService coming from in the tac file? Are these really the files you're attempting to run? cheers lvh
Am 05.11.2013 um 13:47 schrieb Laurens Van Houtven <_@lvh.io>:
Looks like you're missing a bunch of imports; where is meteoService coming from in the tac file? Are these really the files you're attempting to run? This is the complete tac file:
from twisted.application import internet, service from meteo import meteoFactory application = service.Application('meteo') meteoService = internet.TCPserver(80, meteoFactory(), interface='91.216.35.82') # interface='2a02:d40:2:2::82') meteoService.setServiceParent(application) --- Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
Am 05.11.2013 um 13:43 schrieb Axel Rau <Axel.Rau@chaos1.de>:
--- twisted does not start up. Interrupting it, gives "exceptions.ImportError: cannot import name meteoFactory"
It seems to not find my modules: from meteo import meteoFactory I assumed twistd looks relative to the tac file. Giving twistd a --rundir option does not help either. Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
On Nov 5, 2013, at 5:19 AM, Axel Rau <Axel.Rau@Chaos1.DE> wrote:
Am 05.11.2013 um 13:43 schrieb Axel Rau <Axel.Rau@chaos1.de>:
--- twisted does not start up. Interrupting it, gives "exceptions.ImportError: cannot import name meteoFactory"
It seems to not find my modules: from meteo import meteoFactory I assumed twistd looks relative to the tac file. Giving twistd a --rundir option does not help either.
twistd is a python program, which means it finds modules based on the same rules Python uses for finding modules. There are numerous ways you could adjust sys.path; you can use virtualenv, or set PYTHONPATH, or just sys.path.append within the tac file itself. -glyph
On 12:25 pm, _@lvh.io wrote:
Hi Axel,
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
There's some confused language here. "twistd web" sounds a lot like the command line invocation of `twistd´ to use the `web´ plugin - which sets up an HTTP server using Twisted web. The Twisted plugin (twisted.plugin) system is used by twistd to implement its support for extensible command line parsing. "twistd web" is implemented using a twistd plugin (twisted.plugins.twisted_web.TwistedWeb and twisted.web.tap) which is itself implemented using twisted.plugin. I think the OP may have been asking how to use "twistd web" to accomplish what his Python snippet accomplished. Jean-Paul
Am 05.11.2013 um 13:25 schrieb Laufens Van Houtven <_@lvh.io>:
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
I try to write the plugin now: … from zope.interface import implements from twisted.application.service import IServiceMaker from twisted.application import internet from twisted.plugin import IPlugin from twisted.python import usage from meteo import meteoFactory 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.']] class MeteoServiceMaker(object): implements(IServiceMaker, IPlugin) 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'], interface=options['ipv4_address'], ## how do it set the ip6 address? meteoFactory()) serviceMaker = MeteoServiceMaker() --- --- relevant part of metro.py: resource = RootWeatherPage() meteoFactory = Site(resource) --- Currently, this gives following error: --- [meteo] [www4:py-venv/meteo/meteo] root# twistd --help Unhandled Error Traceback (most recent call last): File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/python/usage.py", line 450, in __str__ return self.getSynopsis() + '\n' + self.getUsage(width=None) File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/python/usage.py", line 486, in getUsage for (cmd, short, parser, desc) in self.subCommands: File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/application/app.py", line 631, in subCommands for plug in sorted(plugins, key=attrgetter('tapname')): File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/plugin.py", line 209, in getPlugins allDropins = getCache(package) --- <exception caught here> --- File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/plugin.py", line 167, in getCache provider = pluginModule.load() File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/python/modules.py", line 383, in load return self.pathEntry.pythonPath.moduleLoader(self.name) File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/python/_reflectpy3.py", line 266, in namedAny topLevelPackage = _importAndCheckStack(trialname) File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/python/_reflectpy3.py", line 205, in _importAndCheckStack return __import__(importName) exceptions.SyntaxError: invalid syntax (meteo_plugin.py, line 29) --- Line 29 is serviceMaker = MeteoServiceMaker() What is wrong there? Thanks, Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
On 12:33 pm, axel.rau@chaos1.de wrote:
Am 05.11.2013 um 13:25 schrieb Laufens Van Houtven <_@lvh.io>:
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
[snip] exceptions.SyntaxError: invalid syntax (meteo_plugin.py, line 29)
Unfortunately your code was mangled beyond recognition by your email client. Try attaching the code instead so that we can see what you actually have. Jean-Paul
Am 08.11.2013 um 15:53 schrieb exarkun@twistedmatrix.com:
On 12:33 pm, axel.rau@chaos1.de wrote:
Am 05.11.2013 um 13:25 schrieb Laufens Van Houtven <_@lvh.io>:
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
[snip] exceptions.SyntaxError: invalid syntax (meteo_plugin.py, line 29)
Unfortunately your code was mangled beyond recognition by your email client.
Try attaching the code instead so that we can see what you actually have.
Sorry. Here it comes: ..and the backtrace: Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
On 11/08/2013 06:19 PM, Axel Rau wrote:
Am 08.11.2013 um 15:53 schrieb exarkun@twistedmatrix.com:
On 12:33 pm, axel.rau@chaos1.de wrote:
Am 05.11.2013 um 13:25 schrieb Laufens Van Houtven <_@lvh.io>:
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
[snip] exceptions.SyntaxError: invalid syntax (meteo_plugin.py, line 29)
Unfortunately your code was mangled beyond recognition by your email client.
Try attaching the code instead so that we can see what you actually have.
Sorry. Here it comes:
return internet.TCPServer( int(options['port'], ^^^ You are missing a closing parentheses here.
Am 08.11.2013 um 18:23 schrieb Jonathan Ballet <jon@multani.info>:
On 11/08/2013 06:19 PM, Axel Rau wrote:
Am 08.11.2013 um 15:53 schrieb exarkun@twistedmatrix.com:
On 12:33 pm, axel.rau@chaos1.de wrote:
Am 05.11.2013 um 13:25 schrieb Laufens Van Houtven <_@lvh.io>:
You probably want to read this:
https://twistedmatrix.com/documents/current/core/howto/application.html
There's a number of ways you can use twistd web, for example with a tac file, but personally I always opt for using a Twisted plugin, so you instead do twistd mything :)
[snip] exceptions.SyntaxError: invalid syntax (meteo_plugin.py, line 29)
Unfortunately your code was mangled beyond recognition by your email client.
Try attaching the code instead so that we can see what you actually have.
Sorry. Here it comes:
return internet.TCPServer( int(options['port'], ^^^
You are missing a closing parentheses here.
You are right (-;, but now I come to my real question: --- [meteo] [www4:py-venv/meteo/meteo] root# twistd --pidfile=/var/run/twisted/meteo.pid -u www -g www -r kqueue meteo Traceback (most recent call last): File "/usr/local/py-venv/meteo/bin/twistd", line 14, in <module> run() File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/scripts/twistd.py", line 27, in run app.run(runApp, ServerOptions) File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/application/app.py", line 652, in run runApp(config) File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/scripts/twistd.py", line 23, in runApp _SomeApplicationRunner(config).run() File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/application/app.py", line 386, in run self.application = self.createOrGetApplication() File "/usr/local/py-venv/meteo/lib/python2.7/site-packages/twisted/application/app.py", line 446, in createOrGetApplication ser = plg.makeService(self.config.subOptions) File "/usr/local/py-venv/meteo/meteo/twisted/plugins/meteo_plugin.py", line 26, in makeService meteoFactory(), AttributeError: Site instance has no __call__ method --- makeService expects a protocol factory, but I have only meteoFactory = Site(resource) How do I interface the former to the http protocol? Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
Am 08.11.2013 um 19:43 schrieb Axel Rau <Axel.Rau@chaos1.de>:
makeService expects a protocol factory, but I have only meteoFactory = Site(resource) How do I interface the former to the http protocol?
After fixing my low-level Python errors (sorry for wasting you time), the attached plugin works out of the box with my original code: --- resource = RootWeatherPage() meteo_factory = Site(resource) --- Next, I have to find a way to define a 2nd interface (IPv6) for TCPServer. Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius
On 01:20 pm, axel.rau@chaos1.de wrote:
Am 08.11.2013 um 19:43 schrieb Axel Rau <Axel.Rau@chaos1.de>:
makeService expects a protocol factory, but I have only meteoFactory = Site(resource) How do I interface the former to the http protocol?
After fixing my low-level Python errors (sorry for wasting you time), the attached plugin works out of the box with my original code: --- resource = RootWeatherPage() meteo_factory = Site(resource) ---
If you want to run multiple services then the pattern to follow is just: def makeService(config): parent = MultiService() oneChild.setServiceParent(parent) anotherChild.setServiceParent(parent) ... return parent You may also want to consider using offering endpoints via the command- line configuration options. Instead of writing options to explicitly support IPv4 and IPv6, write an option that accepts an endpoint string description. You can see an example of this (sort of) in the implementation of "twistd mail" which accepts endpoints for several of its option (eg `--pop3`): https://twistedmatrix.com/trac/browser/trunk/twisted/mail/tap.py#L103 You can also read about endpoints here: https://twistedmatrix.com/documents/current/core/howto/endpoints.html and here: https://twistedmatrix.com/documents/current/core/howto/servers.html Jean-Paul
participants (6)
-
Axel Rau
-
Axel Rau
-
exarkun@twistedmatrix.com
-
Glyph
-
Jonathan Ballet
-
Laurens Van Houtven