我在这张海报上遇到了类似的问题:
Python telnetlib: surprising problem
请注意下面的回复和我的回复。
基本上telnetlib在调用任何读取函数时都不会让我读取整个响应。
在while循环中使用select.select([telnetobject],[],[])之后调用telnet.read_very_eager()以确保读取可用时,我唯一能得到的就是几个字符。到目前为止,我能够提出的唯一解决方案是使用time.sleep(),但它太粗糙的解决方案,我正在寻找更适合的东西。任何帮助表示赞赏...
答案 0 :(得分:0)
我认为我有同样的问题,我希望这些信息会有所帮助。在我的情况下,我连接到Maya中的mel和python端口,所以我不知道这种体验对你和你的情况是否有用。
This answer告诉我,我根本不需要Telnet / telnetlib用于我尝试的I / O,并建议asynchat。
事实证明,并非服务器端出现的所有输出都可供客户端读取。我不知道这是一件可能发生的事情,直到我使用asynchat作为第二意见,看到我仍然没有收到“print'Complete Command'”的输出,无论我如何发送它。 (我试图写“打印'完整命令'”并读取结果以了解我的上一个命令何时完成。)
我最后调用了mel的警告命令,它确实产生了客户端可读的输出。将无辜数据作为警告发送是非常令人反感的,但幸运的是,这是一个内部工具。
一个样本,要用大量的盐摄取,因为我还在弄清楚这个:
class mayaClient(asynchat.async_chat) :
...
def __init__(self, sock, clientType):
asynchat.async_chat.__init__(self, sock=sock)
self.clientType = clientType
self.ibuffer = []
self.obuffer = ""
self.set_terminator("\r\n")
self.cumulativeResponse = ""
@classmethod
def withSocket(cls, host, clientType, log) :
melSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
portNumber = cls.MEL_PORT_NUMBER if clientType == cls.MEL else cls.PYTHON_PORT_NUMBER
melSocket.connect((host, portNumber))
return cls(melSocket, clientType, log)
#############################################################
# pushAndWait
#############################################################
def pushAndWait(self, command, timeout=60) :
"""
ACTION: Push a command and a "command complete" indicator
If command completed, return number of milliseconds it took
If command timed out without completing, return -1
INPUT: command as string
description (optional) as string for more informative logging
timeout in seconds as int after which to give up
OUTPUT: X milliseconds if command completed
-1 if not
"""
self.found_terminator()
incrementToSleep = .001
self.push("%s\r\n"%command)
responseToSeek = "Command Complete"
self.push('Whatever command is going to create readable output for %s`); \r\n'%responseToSeek)
timeSlept = 0
while responseToSeek not in self.currentBufferString() and timeSlept < timeout :
self.read()
timeSlept += incrementToSleep
time.sleep(incrementToSleep)
if responseToSeek in self.currentBufferString() :
return timeSlept
return -1
#############################################################
# read
#############################################################
def read(self) :
"""
ACTION: Reads the current available output
and stores it in buffer
"""
try :
self.collect_incoming_data(self.recv(1024))
except Exception, e :
# Don't worry about it -- just means there's nothing to read
#
pass
#############################################################
# currentBuffer
#############################################################
def currentBuffer(self) :
return self.ibuffer
#############################################################
# currentBufferString
#############################################################
def currentBufferString(self) :
return "".join(self.ibuffer)
#############################################################
# collect_incoming_data
#############################################################
def collect_incoming_data(self, data):
"""Buffer the data"""
self.ibuffer.append(data)
#############################################################
# found_terminator
#############################################################
def found_terminator(self):
print "Clearing --%s...-- from buffer"%self.currentBufferString()[0:20]
self.ibuffer = []
答案 1 :(得分:0)
可以参加聚会,但这可以通过telnetlib来完成。下面将读取所有行,直到没有剩余。 timeout=0.1
会花一些时间使响应到达。
try:
while True: # read until we stop
message = session.read_until(b'\n', timeout=0.1)
if message == b'': # check if there was no data to read
break # now we stop
print(message) # print message if not stopped
except EOFError: # If connection was closed
print('connection closed')
或者如果您始终想等待响应:
try:
message = session.read_until(b'\n') # Will not timeout until you have response
print(message)
while True: # read until we stop
message = session.read_until(b'\n', timeout=0.1)
if message == b'': # check if there was no data to read
break # now we stop
print(message) # print message if not stopped
except EOFError: # If connection was closed
print('connection closed')