如何通过 Python(而不是通过 Twisted)运行 Twisted 应用程序?
我正在努力学习 Twisted,并且偶然发现了一些我不确定自己是否非常喜欢的东西 - “Twisted 命令提示符”。我正在 Windows 计算机上摆弄 Twisted,并尝试运行“聊天”示例:
from twisted.protocols import basic
class MyChat(basic.LineReceiver):
def connectionMade(self):
print "Got new client!"
self.factory.clients.append(self)
def connectionLost(self, reason):
print "Lost a client!"
self.factory.clients.remove(self)
def lineReceived(self, line):
print "received", repr(line)
for c in self.factory.clients:
c.message(line)
def message(self, message):
self.transport.write(message + '\n')
from twisted.internet import protocol
from twisted.application import service, internet
factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)
但是,要将此应用程序作为 Twisted 服务器运行,我必须通过“Twisted 命令提示符”运行它,使用以下命令
twistd -y chatserver.py
:有任何方法可以更改代码(设置 Twisted 配置设置等),以便我可以简单地通过以下方式运行它:
python chatserver.py
我已经用 Google 搜索过,但搜索词似乎太模糊,无法返回任何有意义的响应。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道这是否是最好的方法,但我所做的是:
你可以做:
总结如果你想有两个选项(twistd和python):
希望它有帮助!
I don't know if it's the best way to do this but what I do is instead of:
you can do:
Sumarized if you want to have the two options (twistd and python):
Hope it helps!
不要将“Twisted”与“
twistd
”混淆。当您使用“twistd
”时,您正在使用Python 运行该程序。 “twistd
”是一个 Python 程序,除其他外,它还可以从.tac
文件加载应用程序(正如您在此处所做的那样)。“Twisted 命令提示符”是 Twisted 安装程序提供的便利,可以帮助 Windows 上的用户。它所做的只是设置
%PATH%
以包含包含“twistd
”程序的目录。如果正确设置%PATH% 或使用完整路径调用它,则可以从普通命令提示符运行twistd。如果您对此不满意,也许您可以扩展您的问题以包括使用“
twistd
”时遇到的问题的描述。Don't confuse "Twisted" with "
twistd
". When you use "twistd
", you are running the program with Python. "twistd
" is a Python program that, among other things, can load an application from a.tac
file (as you're doing here).The "Twisted Command Prompt" is a Twisted installer-provided convenience to help out people on Windows. All it is doing is setting
%PATH%
to include the directory containing the "twistd
" program. You could run twistd from a normal command prompt if you set your %PATH% properly or invoke it with the full path.If you're not satisfied with this, perhaps you can expand your question to include a description of the problems you're having when using "
twistd
".在 Windows 上,您可以创建包含命令的 .bat 文件,使用完整路径,然后只需单击它即可启动。
例如我使用:
On windows you can create .bat file with your command in it, use full paths, then just click on it to start up.
For example I use:
可能是 run 或
runApp
之一rel="nofollow noreferrer">twisted.scripts.twistd 模块将为您工作。如果确实如此,请告诉我,很高兴知道!Maybe one of
run
orrunApp
in twisted.scripts.twistd modules will work for you. Please let me know if it does, it will be nice to know!我自己没用过twisted。但是,您可以尝试查看twistd本身是否是python文件。我猜测它只是管理从正确的路径加载适当的扭曲库。
I haven't used twisted myself. However, you may try seeing if the twistd is a python file itself. I would take a guess that it is simply managing loading the appropriate twisted libraries from the correct path.
我成功地在 Windows 上使用简单的 Twisted Web 服务器作为 Flask 网站。
其他人是否也成功地在 Windows 上使用 Twisted 来验证该配置?
I am successfully using the simple Twisted Web server on Windows for Flask web sites.
Are others also successfully using Twisted on Windows, to validate that configuration?