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.