【发布时间】:2019-12-14 11:21:47
【问题描述】:
我正在尝试向远程设备发送命令:E5071C ENA Vector Network Analyzer
这些是我的问题:
当我尝试从套接字发送和接收数据时,它“挂起”。
我不确定应该使用哪种类型的套接字。
对于其他命令,我使用了s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM),但我只需要在这些情况下发送数据。这里我需要发送和接收数据。我试过像这样使用while Trueloop:
while True:
s.settimeout(1)
print(sys.stderr, 'waiting for a connection')
connection, client_address = s.accept()
try:
print(sys.stderr, 'client connected:', client_address)
while True:
data = connection.recv(1024)
print(sys.stderr, 'received "%s"') % data
if data:
connection.sendall(data)
else:
break
finally:
connection.close()
我也无法通过 while 循环获得我想要的结果,所以这就是我目前所拥有的:
## import modules
## for loop iterations
## commands sent to remote device using UDP socket
def is_hanging(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
min_msg = ':CALCulate:SELected:MARKer:FUNCtion:TYPE MINimum'
s.send(min_msg.encode())
s.settimeout(1)
try:
s.recv(1024)
print("Received data!")
return True
except socket.timeout as e:
print(e)
return False
finally:
s.close()
if is_hanging('10.5.33.16',5025) is True:
pass
else:
raise Exception("Could not receive data.")
我不完全确定s.recv 是如何工作的,但我希望/期望的是我发送到远程设备的数据会生成一个回复给我的响应。
现在它只是挂起。
【问题讨论】:
标签: python python-3.x sockets tcp