Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requests not responding on long wait from other IP rather than localhost

I'm supposed to raise a get request to our rest url which triggers some activity on server and finally returns a response. It may take more than 30 minutes to return response. I know that requests doesn't have default timeout. But, When I raise a get request and after 30 minutes even if the server sends back response, requests is not returning. I confirmed that the server sent response from log files. I even tried using request sessions so that my connection will be alive. Still it behaves the same.I'm using requests v.2.13.0. Is it the problem with version or with requests. If requests has problem how can i raise a get request which waits till server sends back response? The code is simple anyhow I'm adding for reference.

import requests
url="www.abcd/efg/hijkl"
response=requests.get(url) #It hangs at this point even if server sends back response.

I've tested the same using postman and google chrome, Both were able to get the response after 30 minutes. Also tried the same by updating the requests to 2.18.4 still same issue.

EDIT 1:

The same code works fine if the request is raised from the server to localhost. As per @randomir's suggestion it seems to be issue with network connection might getting dropped inbetween but how to handle this from client side as it difficult to implement any server side method.

like image 333
Mani Avatar asked Oct 14 '25 04:10

Mani


1 Answers

Your long-lasting zero-activity network connection might get dropped by intermediate proxies, firewalls, routers, NATs, etc. And the requests.get() never finds out about it because it's passively waiting for the response.

One way to prevent this, on a TCP layer, is to use the TCP keep-alive (which is usually off by default).

In Python, you can configure the TCP keep-alive directly via low-level socket.setsockopt, but since you're already using requests, the easier way is via TCPKeepAliveAdapter available from the requests_toolbelt package:

import requests
from requests_toolbelt.adapters.socket_options import TCPKeepAliveAdapter

url = '...'
session = requests.Session()
keep_alive = TCPKeepAliveAdapter(idle=120, count=20, interval=30)
session.mount(url, keep_alive)
session.get(url)
like image 151
randomir Avatar answered Oct 16 '25 17:10

randomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!