连接到服务器时获取 stdin Python
我正在连接到 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)
有什么想法如何做到这一点吗?
I'm connecting to an IRC server but while it's sitting waiting for data I'd like the program to be able to grab input from the terminal and then relay it to the server, so essentially say JOIN #foobar and the program send JOIN #foobar. The current code looks like:
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)
Any ideas how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下选择模块。您可以使用它来等待多个类似文件的对象,包括套接字和 stdin/stdout/stderr。
此站点上有一些示例代码。
Take a look at the select module. You can use it to wait on multiple file-like objects including a socket and stdin/stdout/stderr.
There's some example code at this site.