简体   繁体   English

使用Python中的套接字无限等待服务器的回复

[英]Infinite wait for reply from server using sockets in Python

I'm trying to send a message to the API of a server in order to get a reply. 我试图将消息发送到服务器的API,以获取答复。 I am using the following code: 我正在使用以下代码:

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('h68.p.ctrader.com',5211)
sock.connect(server_address)

message = "8=FIX.4.4|9=87|35=0|49=theBroker.12345|56=cServer|57=QUOTE|50=BVN's Message|34=1|52=20180322-21:26:01|10=101"

sock.send(bytes(message,'utf-8'))
data = sock.recv(3)
print(data)
sock.close()

However, when executing it, the message is sent to the server, but at the moment of receiving the reply from the server [data = sock.recv (3)], the program does not go on. 但是,执行该消息时,该消息将发送到服务器,但是在接收到来自服务器的回复时[data = sock.recv(3)],该程序不会继续运行。 It keeps the cursor blinking, like in an infinite loop. 像无限循环一样,它使光标闪烁。 What is the cause of this problem? 这个问题是什么原因造成的? Was it the script? 是剧本吗? The message sent to the server? 消息发送到服务器了吗? The server itself? 服务器本身? How to solve the problem? 如何解决问题?

Note: This message is in a format required by the server API, which consists of "tag" = "value" | 注意:此消息采用服务器API要求的格式,该格式由“ tag” =“ value” | "tag" = "value" | “ tag” =“值” | "tag" = "value" ... “标签” =“值” ...

A quick look at the documentation for this API you're using turns up a couple of problems: 快速浏览一下您正在使用的该API的文档,就会发现一些问题:

  • You're missing a | 您缺少| at the very end of the message. 在消息的最后。
  • You shouldn't actually be sending | 您实际上不应该发送| s at all, that's just a human-readable translation of the actual message. 毕竟,这只是实际消息的人类可读翻译。 All of them need to be replaced with \\x01 or \ (depending on whether you're creating a byte string or a Unicode string), the actual tag separator character. 所有这些都需要替换为\\x01\ (取决于您创建的是字节字符串还是Unicode字符串),即实际的标签分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM