简体   繁体   English

Twistd - 使用带有Options的service.Application来允许参数解析

[英]Twistd - using service.Application with Options to allow argument parsing

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). 为此,我有一个MyServer(name, port, host, testMode=False)类(继承自DatagramProtocol对象)。

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. 值我从数据库中读取的name, porthost I start my server as 'twistd -y my_server_run.py' and everything runs perfect. 我启动我的服务器'twistd -y my_server_run.py' ,一切都运行完美。

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: 我找到了这些信息,我无法将它们解析为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)

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. 任何建议都会非常有帮助。

Unfortunately service.Application cannot be used with usage.Options as was already discussed in this question . 不幸的是, service.Application不能与usage.Options一起使用,正如本问题中已经讨论过的那样。

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). 或者如果你使用扭曲的插件(TAP文件)。

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. 因此可以将选项存储在某个配置文件或数据库中,并在启动期间读取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM