在连接到服务器Python时获取stdin



我正在连接到IRC服务器,但当它等待数据时,我希望程序能够从终端获取输入,然后将其中继到服务器,所以本质上说是JOIN#foobar,程序发送JOIN#foobar。当前代码看起来像:

def receive(self):
    while True:
        raw = self.socket.recv(4096).decode()
        raw_split = raw.splitlines()
        if not raw:
            break
        for line in raw_split:
            #if line.find('MODE {0} :'.format(self.config['nick'])) > -1:
                # placeholder for perform
            data = line.split()
            if data[0] == 'PING':
                self.send('PONG {0}'.format(data[1]))
            color_print("-> {0}".format(data), 'yellow')
            #self.plugin.run(data)

有什么办法吗?

查看select模块。您可以使用它来等待多个类似文件的对象,包括套接字和stdin/stdout/stderr。

这个网站上有一些示例代码。

最新更新