Get continuous response of POST request in python

2024/11/16 7:50:52

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 eself.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.

Answer

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.

https://en.xdnf.cn/q/118951.html

Related Q&A

DataType of Pandas Multiindex [duplicate]

This question already has answers here:get the dtype of a pandas multiindex(3 answers)Closed 6 years ago.import pandas as pd index = pd.MultiIndex.from_tuples([(1,2, None), (1,2,3)]) print(index.get_le…

Is there a way to see the internal representation of float?

In python tutorial for floating point, In base 2, 1/10 is the infinitely repeating fraction0.0001100110011001100110011001100110011001100110011...How do I get python interpreter to print this intenal re…

Create function from try-except

Based on the example provided in this answer, how can I create a function from:from collections import Counter s = [0, 0, 2, 1, 1, 0, 0, 0] try:print(next(t[0] for t in Counter(s).most_common(2) if t[…

Bad request error flask with POST request

I have the following route that is getting values from a submitted form@app.route(/authenticate, methods=["POST"]) def authenticate():username = request.form[username]print(username, file = s…

How to use matplotlib/numpy to plot heatmap given three irregular lists of values in Python

Im wondering if there is a way to use matplotlib and numpy to plot the heatmap of three lists. My grid is not regular, it is oddly shaped so this does not work for me: Plotting a Heat Map X,Y,Intensity…

How to set custom color for symbols like*,#,etc in python tkinter

How to set specific color for certain symbols like ,#,etc example if I type "" its color should be blue and other stay remain same. typedecker sir i am binding you function like this but thi…

How can I get only heading names.from the text file

I have a Text file as below:Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgdTechnical skills : java,j2ee etc.,work done: oaugafiuadgkfjwgeuyrfvskjdfviysdvfhsdf,aviysdvw…

Concatenate list elements that fall between list elements of certain value

Imagine I have the following list:>>> mylist[(a, uDT),(Satisfactory, uJJ),(tracing, uVBG),(with, uIN),(a, uDT),(fairly, uRB),(persistent, uJJ),(with, uIN)]How do I concatenate list items that …

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?