我正在编写一个连接到远程sevrer的简单套接字程序。我收到服务器的响应并尝试通过在ffunction中调用它来结束它到另一个函数。但是当我打印出那个响应时我什么都没得到 这是代码:
def Sendcm(self,command):
result ="ok"
try:
#print "start"
if command.startswith("#"):
#print result
return result
elif not command:
return result
# print 'yes'
result=self.sendWait(command)
print ('This is::')
print result \\blank
except Exception as e:
traceback.print_exc()
return result
sendWait功能
def sendWait(self,cmd):
try:
b=len(cmd)
s.send(struct.pack("!H",b))
s.send(cmd)
response=s.recv(1024)
print "The response received is:::"
print response
if RESP_OK in response:
return
elif RESP_SUCCESS in response:
return
elif RESP_Failure in response:
return
return response
except Exception:
traceback.print_exc()
答案 0 :(得分:0)
解决了它。问题是if语句中的return语句。由于代码永远不会返回响应.i将整个代码放在while循环中并使用break来打破循环并返回响应。现在它工作正常 变化:
while True:
s.send(cmd)
response=s.recv(1024)
print "The response received is:::"
print response
if RESP_OK in response:
break
elif RESP_SUCCESS in response:
break
elif RESP_FAILURE in response:
break
return response