python获取post请求_在python中获取POST请求的连续响应

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 = "" % (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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值