1

我想将我的服务器作为应用程序运行。为此,我有一个MyServer(name, port, host, testMode=False)类(继承自DatagramProtocol对象)。

在另一个文件中,我创建了一些命令来创建和启动我的服务器。或多或少,它看起来像:

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, porthost我从数据库中读取。我启动我的服务器'twistd -y my_server_run.py',一切运行完美。

但是,我希望能够以模式启动我的服务器:测试模式和标准模式。因此,我想将从命令行读取的参数作为参数传递给我的对象。我找到了无法将它们解析为 sys.argv 的信息,但我必须实现usage.Options,所以我按照以下方式进行了操作:

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)

然后,我将服务器运行为:

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

但是,我收到错误:

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

我无法找出我做错了什么。任何建议都会非常有帮助。

4

1 回答 1

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.

于 2016-12-05T13:17:18.627 回答