我想用来select.select()
阻止循环,直到准备好从 telnet 会话列表中读取一些数据。但是,下面的代码不起作用,并且调用select.select()
立即返回,没有要读取的数据。我在 Ubuntu 10.4 下使用 python 2.7.3。
tn = telnetlib.Telnet(IP,23,TIMEOUT)
# [... logging and password omitted ..]
while True:
logging.info("%d - waiting on select for data" % self.id)
read_ready, write_ready, expt_ready = select.select([ tn.get_socket() ],[],[])
logging.info("%d - select returned" % self.id)
if len(read_ready) == 1:
data = tn.read_eager()
logging.info("%d read %d characters" % (self.id, len(data)))
# ... continue processing the data...
输出示例:
2013-08-23 10:37:28,634 - INFO: 1 - waiting on select for data
2013-08-23 10:37:28,672 - INFO: 1 - select returned
2013-08-23 10:37:28,673 - INFO: 1 read 0 characters
2013-08-23 10:37:28,833 - INFO: 1 - waiting on select for data
2013-08-23 10:37:28,874 - INFO: 1 - select returned
2013-08-23 10:37:28,874 - INFO: 1 read 0 characters
2013-08-23 10:37:29,029 - INFO: 1 - waiting on select for data
2013-08-23 10:37:29,073 - INFO: 1 - select returned
2013-08-23 10:37:29,074 - INFO: 1 read 0 characters
一个例子set_debuglevel(1)
:
2013-08-23 11:50:29,019 - INFO: 1 - waiting on select for data
2013-08-23 11:50:29,062 - INFO: 1 - select returned
Telnet(10.2.3.15,23): recv '\xff\xfe\x01'
Telnet(10.2.3.15,23): IAC DONT 1
2013-08-23 11:50:29,062 - INFO: 1 read 0 characters
2013-08-23 11:50:29,063 - INFO: 1 - waiting on select for data
2013-08-23 11:50:29,069 - INFO: 1 - select returned
Telnet(10.2.3.15,23): recv '\xff\xfe!'
Telnet(10.2.3.15,23): IAC DONT 33
2013-08-23 11:50:29,069 - INFO: 1 read 0 characters
2013-08-23 11:50:29,070 - INFO: 1 - waiting on select for data
2013-08-23 11:50:29,110 - INFO: 1 - select returned
Telnet(10.2.3.15,23): recv '\xff\xfc\x03'
Telnet(10.2.3.15,23): IAC WONT 3
2013-08-23 11:50:29,110 - INFO: 1 read 0 characters