Get continuous response of POST request in python - python

I'm writing a script which does a POST request to a server and gets blocked the server keeps sending the response whenever a specific event is triggered. I have to take a cookie for post request with earlier login request and pass it as data to POST, each cookie lasts for 10 mins after which I've to run keep-alive request.
Whenever some event is triggered I want to log that event in a file, I tried async, unirest requests they generate the post request but I don't have control over output, I tried sessions also but of no use. I want to do following things in same order
1]Login (can do only once)
2]Post the request to server
3]Keep monitoring output of step 2 eternally whenever there is some output log it into a file
4]Keep the session alive by another request to server.
Let me know if you need more explanation.
Below is code, it does not work though
while True:
try:
xmldata = "<eventSubscribe cookie=\"%s\" />" % (self.cookie)
r = requests.post(post_url,data=xmldata,stream=False,verify=False,timeout=10)
write_to_file('Ok',r.text)
unsubevents()
logout()
except Exception as e:
print e
self.write_to_file('Ok',"")
self.login()
So in above code the post call I make here is blocking and continuous, It streams the output continuously so the post call never really gets completed.
But it receives output in xml format, server sends these responses every time an event is triggered.
PS: I don't want to do logout and login again,this works in curl where it keeps printing output on stdout, I have to run this code for several servers like 200.

I've fixed this problem with two level threading and reading chunks instead of content or read_lines().
1] First threads will be created which will spawn second thread and run keepalive when timeout hits.
2]Second thread subscribes to event with POST request and then keeps on listening to chunks of size 1024 everytime a response is received it is parsed and respective data is updated. Here I used requests with Stream=True; This wasn't working for me earlier because cookie used to expire before reading response and session used to close.
If someone has better way to do this please update here.

Related

Delay in receiving first message from a websocket connection

I am writing a code in Python to send three POST requests consecutively if certain conditions are met. The POST requests are sent to the FTX Exchange (which is a crypto exchange) and each request is a 'buy' order.
The second order is triggered as soon as the first is filled, and the third as soon as the second is filled. In order to speed up the code (I need the orders to be executed very close to each other in time), I am sending all POST requests to a subprocess (with multiprocessing.Process()) and, instead of waiting for the request response, I wait for an update from a websocket connection to the walletchannel that notifies each new filled order. This websocket connection is opened at the very beginning of the code, in a subprocess.
So, the timeline of the code is the following
Open Websocket connection to the wallet channel
Loop until conditions are met
If True, exit loop and send first order through POST request
Wait until the first order is filled (i.e. update from the websocket)
Send second order through POST request
Wait until the second order is filled (i.e. update from the websocket)
Send third order through POST request
Wait until the third order is filled (i.e. update from the websocket)
Return "Orders submitted and filled"
I have the small problem that in step (4) the update from the websocket takes too much time to arrive (of the order of 1 second), while steps (6) and (8) are pretty fast (of the order of milliseconds).
It looks like the websocket connection is somehow sleeping before the steps (3)-(4) and it takes some time to receive messages but, as soon as the first message is received, all the subsequent messages arrive very fast. I am not a network expert... how can I avoid such delay in receiving the first message from the websocket?
I am pinging the websocket connection every 20 seconds and waiting for a pong within 10 seconds.
I think it is not the problem with your code but the problem with the service that you are using.
For example, if the server is getting a simple request such as pinging, it takes very little time to process the request and respond. However, if it is a complex request such as buying crypto, it may take some time at the server to process your request, and this is what I believe creates some delay with your first request.
I think that happens with the only first request because the server has some caching that saves the recent user.

Django - strange browser behavior cause broken pipe

I noticed a strange behavior for a long time that cause my server do extra work. For Safari browser. Whenever you touch the address bar and start to edit the existing URL, the browser sends the same get request to the server and close the connection before the server return the response. When you finish editing the address and hit enter it will send the new request.
This behavior can happen multiple times while you edit your URL in the address bar.
This cause the server to fully process the response and when it return the result it through Broken pipe.
This happen on both cases for DEBUG = True/False. So I can see it on local debug server and I can see a request happening on my NGINX production server.
Is there a way to identify this request so to not serve results and save the server processing power?
Thanks

Does setting socket timeout cancel the initial request

I have a request that can only run once. At times, the request takes much longer than it should.
If I were to set a default socket timeout value (using socket.setdefaulttimeout(5)), and it took longer than 5 seconds, will the original request be cancelled so it's safe to retry (see example code below)?
If not, what is the best way to cancel the original request and retry it again ensuring it never runs more than once.
import socket
from googleapiclient.discovery import build
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
#retry(
retry=retry_if_exception_type(socket.timeout),
wait=wait_fixed(4),
stop=stop_after_attempt(3)
)
def create_file_once_only(creds, body):
service = build('drive', 'v3', credentials=creds)
file = service.files().create(body=body, fields='id').execute()
socket.setdefaulttimeout(5)
create_file_once_only(creds, body)
It's unlikely that this can be made to work as you hope. An HTTP POST (as with any other HTTP request) is implemented by sending a command to the web server, then receiving a response. The python requests library encapsulates a lot of tedious parts of that for you, but at the core, it's going to do a socket send followed by a socket recv (it may of course require more than one send or recv depending on the size of the data).
Now, if you were able to connect to the web server initially (again, this is taken care of for you by the requests library but typically only takes a few milliseconds), then it's highly likely that the data in your POST request has long since been sent. (If the data you are sending is megabytes long, it's possible that it's only been partially sent, but if it is reasonably short, it's almost certainly been sent in full.)
That in turn means that in all likelihood the server has received your entire request and is working on it or has enqueued your request to work on it eventually. In either case, even if you break the connection to the server by timing out on the recv, it's unlikely that the server will actually even notice that until it gets to the point in its execution where it would be sending its response to your request. By that point, it has probably finished doing whatever it was going to do.
In other words, your socket timeout is not going to apply to the "HTTP request" -- it applies to the underlying socket operations instead -- and almost certainly to the recv part on the tail end. And just breaking the socket connection doesn't cancel the HTTP request.
There is no reliable way to do what you want without designing a transactional protocol with the close cooperation of the HTTP server.
You could do something (with the cooperation of the HTTP server still) that could do something approximating it:
Create a unique ID (UUID or the like)
Send a request to the server that contains that UUID along with the other account info (name, password, whatever else)
The server then only creates the account if it hasn't already created an account with the same unique ID.
That way, you can request the operation multiple times, but know that it will only actually be implemented once. If asked to do the same operation a second time, the server would simply respond with "yep, already did that".

HTTP status code 200 vs 202

I have a Python+requests script.
Steps that script should execute:
send file to DB;
approve this file (change file state in DB);
download file.
The constraint:
Only approved file could be downloaded
My code:
requests.post(url_to_create, files={"file": open(path_to_file)})
requests.post(url_to_approve, data={'id': file_id})
requests.get(url_to_download, data={'id': file_id})
The problem:
This code works almost perfectly, but sometimes I get no file. I found that the first and the third requests return 200 status code while the second returns 202. As I understand (tell me if I wrong) status 202: Accepted means that server accept request and return status code without actual request completion
The question:
Does it mean that request to download could be send even if request to approve hasn't been already completed and, if it is so, how can I wait till approval-request completed before send download-request?
It depends on your server implementation and your server decides how 202 will be processed.
202 Accepted
The request has been accepted for processing, but the processing has
not been completed. The request might or might not eventually be acted
upon, as it might be disallowed when processing actually takes place.
There is no facility for re-sending a status code from an asynchronous
operation such as this.
The 202 response is intentionally non-committal. Its purpose is to
allow a server to accept a request for some other process (perhaps a
batch-oriented process that is only run once per day) without
requiring that the user agent's connection to the server persist until
the process is completed. The entity returned with this response
SHOULD include an indication of the request's current status and
either a pointer to a status monitor or some estimate of when the user
can expect the request to be fulfilled.
If response body is empty, makes sense to check response headers that should have additional information.
Reference - https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

What happens if a HTTP connection is closed while AppEngine is still running

The real question is if Google App Engine guarantees it would complete a HTTP request even if the connection is no longer existed (such as terminated, lost Internet connection).
Says we have a python script running on Google App Engine:
db.put(status = "Outputting")
print very_very_very_long_string_like_1GB
db.put(status = "done")
If the client decides to close the connection in the middle (too much data coming...), will status = "done" be executed? Or will the instance be killed and all following code be ignored?
If the client breaks the connect, the request will continue to execute. Unless it reaches the deadline of 60 seconds.
GAE uses Pending Queue to queue up requests. If client drops connection and request is already in the queue or being executed, then it will not be aborted. Afaik all other http servres behave the same way.
This will be a real problem when you make requests that change state (PUT, POST, DELETE) on mobile networks. On Edge networks we see about 1% of large requests (uploads, ~500kb) dropped in the middle of request executing (exec takes about 1s): e.g. server gets the data and processes it, but client does not receive response, triggering it to retry. This could produce duplicate data in the DB, breaking integrity of this data.
To alleviate this you will need to make your web methods idempotent: repeating the same method with same arguments does not change state. The easiest way to achieve this would be one of:
Hash relevant data and compare to existing hashes. In you case it would be the string you are trying to save (very_very_very_long_string_like_1GB). You can do this server side.
Client provides unique request-scoped ID, and sever checks if this ID was already used.

Categories

Resources