嗨,我想用柴油创建聊天服务器。我使用此代码运行简单的聊天服务器 :
from diesel import Application, Service, until_eol, fire, wait
def chat_server(addr):
my_nick = (yield until_eol()).strip()
while True:
my_message, other_message = yield (until_eol(), wait('chat_message'))
if my_message:
yield fire('chat_message', (my_nick, my_message.strip()))
else:
nick, message = other_message
yield "<%s> %s\r\n" % (nick, message)
app = Application()
app.add_service(Service(chat_server, 8000))
app.run()
但是当我尝试远程登录此服务器时,远程登录连接被外国主机关闭。
[nima@ca005 Desktop]$ telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
当我从代码中删除产量时,连接到服务器没有问题。
def chat_server(addr):
my_nick = until_eol().strip()
while True:
message = diesel.until_eol()
shouted_message = my_nick + ":" + message
diesel.send(shouted_message)
my_message = until_eol()
other_message = wait('chat_message')
if my_message:
fire('chat_message', (my_nick, my_message.strip()))
这段代码有什么问题?