Infinite wait for server response using sockets in python

0

I'm trying to send a message to the API of a server in order to get a response. I'm 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 you run it, the message is sent to the server, but at the time of receiving the response from the server [data = sock.recv (3)], the program does not continue. It keeps the cursor blinking, as if it were in an infinite loop. What is the likely cause of this problem? Was it the script? The message sent to the server? The server itself? How do I fix the problem?

Note: This message is in a format required by the server API, which consists of "tag"="value" | "tag"="value" | "tag"="value" ...

    
asked by anonymous 24.03.2018 / 02:35

1 answer

1

You order the program to receive 3 bytes on that socket, and they never arrive.

On a system for production, you should set the socket timeout, so if the answer does not arrive, the socket raises an exception: this exception you treat in your program with the proper procedure: usually doing a few retries with time intervals, and generating the log and error and appropriate failure message.

Apart from this: if the test does not get any message back, it is either because the server is not working, or there is an error in your call - this second case is much more likely. you have the documentation in hand, reread it carefully, and see if there is any other interpretation on how this request can be assembled - go testing until you get a response. It can be as simple as adding a linefeed character ("\ n") at the end of the message. It may be that some numeric fields have to be sent as bytes, instead of strings - etc ... look for examples of the same API calls that work.

    
24.03.2018 / 17:11