Infinite wait per server response using python sockets

Asked

Viewed 130 times

0

I am trying to send a message to a server API in order to get a response. 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 when 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 probable cause of this problem? Is it the script? The message sent to the server? The server itself? How to resolve the issue?

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

1 answer

1


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

In a system for production, you must set the socket timeout, for, if the answer does not arrive, the socket raise an exception: this exception you treat in your program with the proper procedure: in general doing some retentatives, with different time intervals, and generating the log and error and appropriate failure message.

Apart from that: if no message comes back in the test, it is because either the server is not working, or there is some error in your call - this second case is much more likely. That you have the documentation in hand, read it carefully, and see if there is any other interpretation on how this request can be mounted - go through testing until you get a response. It can be something as simple as including a line advance 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 d chmdas for the same API that work.

  • Taking one more look at the API, I noticed that I was forgetting to put a '|' at the end of the message and that I had to exchange '|' for ' u0001'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.