[Twisted-Python] Sending arguments to application

Ian Duggan ian at ianduggan.net
Wed May 25 11:19:21 MDT 2005


On Wednesday 25 May 2005 00:15, Roland Hedberg wrote:
> I using the variant "twistd -y foo.py" and I would like to be able to
> send arguments to foo.py in the same command.
>
> The preferable way would be something like
>
> "twistd -y foo.py arg1 arg2"
>
> But that doesn't work, twistd complains.
>
> If it can be done some other way, how ?

I wanted the functionality of twistd as well as the ability to pass arguments 
to my script too. I managed to do something that worked for my by turning 
things inside out and having my script execute twistd as needed. 

The trick was to use an if/else around the __name__ == __main__ statement.

from twisted.python import usage
import twisted.scripts.twistd as td

# setup command parsing here

if __name__ == '__main__':
    #run twistd here
    tdcmds = ["-o", "-y", __file__]
    config = parse_options()

    # add more options to tdcmds
    ....

    # all
    tdoptions = td.ServerOptions()
    tdoptions.parseOptions(tdcmds)
    td.runApp(tdoptions)

else:
    # setup the application object -- gets run when twistd loads this file
    application = service.Application('sampleserver')
    serviceCollection = service.IServiceCollection(application)
    internet.UDPServer(config['port'], SampleProtocol()
                                        ).setServiceParent(serviceCollection)


--Ian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sample.py
Type: application/x-python
Size: 3059 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20050525/3cb4cd90/attachment-0002.bin>


More information about the Twisted-Python mailing list