代码之家  ›  专栏  ›  技术社区  ›  Mike Trpcic

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

  •  19
  • Mike Trpcic  · 技术社区  · 15 年前

    我正在努力学习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
    

    python chatserver.py
    

    我在谷歌上搜索过,但搜索词似乎太模糊,无法返回任何有意义的回复。

    谢谢

    5 回复  |  直到 15 年前
        1
  •  23
  •   chaos.ct    15 年前

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

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

    你可以做:

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

    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)
    

    希望有帮助!

        2
  •  15
  •   Glyph    13 年前

    不要把“扭曲”和“扭曲”混为一谈 twistd ". 当你使用“ 扭曲 ”“你呢 用Python运行程序。” 扭曲 .tac 文件(正如您在这里所做的)。

    “Twisted命令提示符”是一个Twisted安装程序,为帮助Windows用户提供了便利。它所做的只是设置 %PATH% 要包括包含“”的目录,请执行以下操作: 扭曲 “计划。如果正确设置%PATH%或使用完整路径调用它,则可以从普通命令提示符运行twistd。

    如果您对此不满意,也许您可以扩展您的问题,包括对您在使用时遇到的问题的描述“ 扭曲 ".

        3
  •  2
  •   stefanB    15 年前

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

    例如,我使用:

    runfileserver.bat:
    C:\program_files\python26\Scripts\twistd.py -y C:\source\python\twisted\fileserver.tac
    
        4
  •  2
  •   Alok Singhal    15 年前

    也许是其中之一 run runApp 在里面 twisted.scripts.twistd 模块将为您工作。如果有,请告诉我,很高兴知道!

        5
  •  1
  •   frogstarr78    15 年前

    我没有用过扭曲自己。但是,您可以尝试查看twistd本身是否是python文件。我猜这只是管理从正确路径加载适当的twisted库。

        6
  •  0
  •   Howard Rothenburg    5 年前

    我正在Windows for Flask网站上成功使用simple Twisted Web服务器。 其他人是否也成功地在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()