durusmail: quixote-users: Re: setting the current running python script as the quixote publisher namespace
setting the current running python script as the quixote publisher namespace
2004-06-24
Re: setting the current running python script as the quixote publisher namespace
2004-06-25
Re: setting the current running python script as the quixote publisher namespace
Graham Fawcett
2004-06-25
Jon Dyte wrote:

> Don't ask why this has to be all in one script!!!!

You must have a pretty good reason, to willingly put yourself through
all these acrobatics... ;-)

Well, you could replace

     Server(sys.modules["__main__"],9999)

with

     Server('__main__', 9999)

and all should be fine. The Publisher will resolve '__main__' and find
your module without all the sys.modules hunting.


Or, if you know the name of your script, (e.g. 'foo.py'), you could
write it like this::

     if __name__ == '__main__':

         from twisted.internet import reactor
         from quixote.server.twisted_http import Server
         import foo
         s = Server(foo, 9999)
         reactor.run()

     elif __name__ == 'foo':

         _q_exports = []

         def _q_index(request):
             request.response.set_content_type("text/plain")
             return "This is the _q_index function!"


It's a sneaky way of defining a 'module within a script'. Well, sort of.

The MyApp class is unnecessary in the example (though perh. you need it
in your actual code): just instantiating a Server will register it with
the reactor, and that's all you really need to do.

BTW, enabling PTL seems a bit irrelevant if you're trying to pack
everything into one script, since PTL only comes into play when you
import a module.

-- Graham


reply