当我在柴油中使用产量时,telnet连接关闭

时间:2013-07-13 07:54:40

标签: python chat

嗨我想用diesel创建聊天服务器。我使用此代码运行simple chat server

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()

但是当我尝试telnet这个服务器时,telnet Connection被外国主机关闭。

[nima@ca005 Desktop]$ telnet localhost 8000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

当我从代码中删除yield时,我没有连接到服务器的问题。

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()))

这段代码有什么问题?

2 个答案:

答案 0 :(得分:1)

也许是这样的。它可能不会立即起作用,因为我不熟悉柴油的工作原理。

import StringIO
import socket
import threading

from diesel import Application, Service, until_eol, fire, wait

class socket_thread(threading.Thread):

    def __init__(self, line_filter = None):
        threading.Thread.__init__(self)
        self.daemon = True
        self.lock = threading.Lock()
        self.event = threading.Event()
        self.event.clear()
        self.buffer = StringIO.StringIO()
        if(line_filter == None):
            self.line_filter = lambda x: x
        else:
            self.line_filter = line_filter

    def run(self):
        message = True
        while message:
            message = diesel.until_eol()

            self.lock.acquire()
            self.buffer.write(message)
            self.lock.release()
            self.event.set()

    def readlines(self):
        self.lock.acquire()

        self.buffer.seek(0)
        raw_lines = self.buffer.readlines()
        self.buffer.truncate(0)

        self.lock.release()

        lines = map(self.line_filter, raw_lines)
        return lines

def chat_server(addr):
    server_recv = socket_thread()
    my_nick = until_eol().strip()
    data = []
    server_recv.start()
    while True:
        server_recv.event.wait()
        data = server_recv.readlines()
        if(data):
            shouted_message = my_nick + ":" + data
            diesel.send(shouted_message)
        server_recv.event.clear()

        my_message = until_eol()
        other_message = wait('chat_message')
        if my_message:
            fire('chat_message', (my_nick, my_message.strip()))

app = Application()
app.add_service(Service(chat_server, 8000))
app.run()

答案 1 :(得分:1)

我找到了正确的代码here

# vim:ts=4:sw=4:expandtab
'''Simple chat server.

telnet, type your name, hit enter, then chat.  Invite
a friend to do the same.
'''
from diesel import Application, Service, until_eol, fire, first, send

def chat_server(addr):
    my_nick = until_eol().strip()
    while True:
        evt, data = first(until_eol=True, waits=['chat_message'])
        if evt == 'until_eol':
           fire('chat_message', (my_nick, data.strip()))
        else:
            nick, message = data
            send("<%s> %s\r\n"  % (nick, message))

app = Application()
app.add_service(Service(chat_server, 8000))
app.run()