1

I would like to run my server as a application. To do that, I have a MyServer(name, port, host, testMode=False) class (which inherits from DatagramProtocol object).

In another file I created a few commands to create and start my server. More or less, it looks like:

from twisted.application import service, internet

name, port, host = #read from database

server = MyServer(name, port, host)
udp_server = internet.UDPServer(port, server)
application = service.Application("MyServer")
udp_server.setServiceParent(application)

values name, port, and host I read from a database. I start my server as 'twistd -y my_server_run.py' and everything runs perfect.

However, I would like to be able to start my server in to modes: a testing mode and a standard one. Thus, I would like to pass an argument, which I read from the command line, to my object as an parameter. I found the information, that I cannot parse them as sys.argv, but I have to implement the usage.Options, so I did it as follows:

from twisted.application import service, internet
from twisted.python import usage

class Options(usage.Options):
    optParameters = [["test", "t", False, "The client test mode"]]


options = Options()
name, port, host = #read from database

try:
    options.parseOptions()
    server = MyServer(name, port, host, testMode=options['test'])
    udp_server = internet.UDPServer(port, server)
    application = service.Application("MyServer")
    udp_server.setServiceParent(application)

Then, I run my server as:

'twistd -y run_client.py --test True'

However, I'm getting error:

option -y not recognized
Unhandled Error
out: Traceback (most recent call last):
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 648, in run
out:     runApp(config)
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/scripts/twistd.py", line 25, in runApp
out:     _SomeApplicationRunner(config).run()
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 379, in run
out:     self.application = self.createOrGetApplication()
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 444, in createOrGetApplication
out:     application = getApplication(self.config, passphrase)
out: --- <exception caught here> ---
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 455, in getApplication
out:     application = service.loadApplication(filename, style, passphrase)
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/application/service.py", line 411, in loadApplication
out:     passphrase)
out:   File "/usr/local/lib/python2.7/dist-packages/twisted/persisted/sob.py", line 224, in loadValueFromFile
out:     value = d[variable]
out: exceptions.KeyError: 'application'
out: Failed to load application: 'application'
out: Could not find 'application' in the file. To use 'twistd -y', your .tac

I cannot find out what I'm doing wrong. Any suggestions would be very helpful.

Ziva
  • 3,181
  • 15
  • 48
  • 80

1 Answers1

0

Unfortunately service.Application cannot be used with usage.Options as was already discussed in this question.

Options can be used if you are launching your server via

python run_client.py --test True

or if you use twisted plugins (TAP files).

You use twisted application configuration file, it is supposed you configure your server in this file and it will be launched as a service on production system. So it is OK to store options in some config file or database and read them during launch.

Community
  • 1
  • 1
Sergey Shubin
  • 3,040
  • 4
  • 24
  • 36