我在学习Twisted的过程中,偶然发现了一些我不太喜欢的东西-- "Twisted Command Prompt“。我正在我的Windows机器上摆弄Twisted,并尝试运行"Chat“示例:
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 Command Prompt“运行它:
twistd -y chatserver.py有没有办法更改代码(设置Twisted配置设置等),以便我可以通过以下方式运行它:
python chatserver.py我已经用谷歌搜索过了,但搜索词似乎太模糊了,无法返回任何有意义的回复。
谢谢。
发布于 2009-12-14 07:25:31
我不知道这是不是最好的方法,但我要做的是:
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)希望它能帮上忙!
发布于 2009-12-14 06:56:13
不要把“扭曲”和"twistd“搞混了。当你使用"twistd“时,你就是在用Python运行程序。"twistd“是一个Python程序,它可以从.tac文件加载应用程序(就像您在这里所做的那样)。
"Twisted Command Prompt“是一个Twisted installer,它为Windows上的用户提供了方便。它所做的一切就是将%PATH%设置为包含包含"twistd“程序的目录。如果正确设置了% path %或使用完整路径调用它,则可以从正常的命令提示符处运行twistd。
如果你对此不满意,也许你可以扩展你的问题,包括你在使用"twistd“时遇到的问题的描述。
发布于 2009-12-14 06:23:27
在windows上,你可以用你的命令创建.bat文件,使用完整的路径,然后点击它来启动。
例如,我使用:
runfileserver.bat:
C:\program_files\python26\Scripts\twistd.py -y C:\source\python\twisted\fileserver.tachttps://stackoverflow.com/questions/1897939
复制相似问题