如何通过 Python(而不是通过 Twisted)运行 Twisted 应用程序?

发布于 2024-08-14 07:57:57 字数 1202 浏览 3 评论 0 原文

我正在努力学习 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 搜索过,但搜索词似乎太模糊,无法返回任何有意义的响应。

谢谢。

I am working my way through learning Twisted, and have stumbled across something I'm not sure I'm terribly fond of - the "Twisted Command Prompt". I am fiddling around with Twisted on my Windows machine, and tried running the "Chat" example:

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)

However, to run this application as a Twisted server, I have to run it via the "Twisted Command Prompt", with the command:

twistd -y chatserver.py

Is there any way to change the code (set Twisted configuration settings, etc) so that I can simply run it via:

python chatserver.py

I've Googled, but the search terms seem to be too vague to return any meaningful responses.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁月染过的梦 2024-08-21 07:57:57

我不知道这是否是最好的方法,但我所做的是:

application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)

你可以做:

from twisted.internet import reactor
reactor.listenTCP(1025, factory)
reactor.run()

总结如果你想有两个选项(twistd和python):

if __name__ == '__main__':
    from twisted.internet import reactor
    reactor.listenTCP(1025, factory)
    reactor.run()
else:
    application = service.Application("chatserver")
    internet.TCPServer(1025, factory).setServiceParent(application)

希望它有帮助!

I don't know if it's the best way to do this but what I do is instead of:

application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)

you can do:

from twisted.internet import reactor
reactor.listenTCP(1025, factory)
reactor.run()

Sumarized if you want to have the two options (twistd and python):

if __name__ == '__main__':
    from twisted.internet import reactor
    reactor.listenTCP(1025, factory)
    reactor.run()
else:
    application = service.Application("chatserver")
    internet.TCPServer(1025, factory).setServiceParent(application)

Hope it helps!

む无字情书 2024-08-21 07:57:57

不要将“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".

感性 2024-08-21 07:57:57

在 Windows 上,您可以创建包含命令的 .bat 文件,使用完整路径,然后只需单击它即可启动。

例如我使用:

runfileserver.bat:
C:\program_files\python26\Scripts\twistd.py -y C:\source\python\twisted\fileserver.tac

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:

runfileserver.bat:
C:\program_files\python26\Scripts\twistd.py -y C:\source\python\twisted\fileserver.tac
话少情深 2024-08-21 07:57:57

可能是 run 或 runApp 之一rel="nofollow noreferrer">twisted.scripts.twistd 模块将为您工作。如果确实如此,请告诉我,很高兴知道!

Maybe one of run or runApp in twisted.scripts.twistd modules will work for you. Please let me know if it does, it will be nice to know!

花海 2024-08-21 07:57:57

我自己没用过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.

ら栖息 2024-08-21 07:57:57

我成功地在 Windows 上使用简单的 Twisted Web 服务器作为 Flask 网站。
其他人是否也成功地在 Windows 上使用 Twisted 来验证该配置?

new_app.py

if __name__ == "__main__":
    reactor_args = {}

    def run_twisted_wsgi():
        from twisted.internet import reactor
        from twisted.web.server import Site
        from twisted.web.wsgi import WSGIResource

        resource = WSGIResource(reactor, reactor.getThreadPool(), app)
        site = Site(resource)
        reactor.listenTCP(5000, site)
        reactor.run(**reactor_args)

    if app.debug:
        # Disable twisted signal handlers in development only.
        reactor_args['installSignalHandlers'] = 0
        # Turn on auto reload.
        import werkzeug.serving
        run_twisted_wsgi = werkzeug.serving.run_with_reloader(run_twisted_wsgi)

    run_twisted_wsgi()


old_app.py

if __name__ == "__main__":
    app.run()

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?

new_app.py

if __name__ == "__main__":
    reactor_args = {}

    def run_twisted_wsgi():
        from twisted.internet import reactor
        from twisted.web.server import Site
        from twisted.web.wsgi import WSGIResource

        resource = WSGIResource(reactor, reactor.getThreadPool(), app)
        site = Site(resource)
        reactor.listenTCP(5000, site)
        reactor.run(**reactor_args)

    if app.debug:
        # Disable twisted signal handlers in development only.
        reactor_args['installSignalHandlers'] = 0
        # Turn on auto reload.
        import werkzeug.serving
        run_twisted_wsgi = werkzeug.serving.run_with_reloader(run_twisted_wsgi)

    run_twisted_wsgi()


old_app.py

if __name__ == "__main__":
    app.run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文