Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Python 3 requests how to force use a new connection for each request?

I have written a pausable multi-thread downloader using requests and threading, however the downloads just can't complete after resuming, long story short, due to special network conditions the connections can often die during downloads requiring refreshing the connections.

You can view the code here in my previous question:

Python multi connection downloader resuming after pausing makes download run endlessly

I observed the downloads can go beyond 100% after resuming and won't stop (at least I haven't see them stop), mmap indexes will go out of bounds and lots of error messages...

I have finally figured out this is because the ghost of previous request, that makes the server mistakenly sent extra data from last connection that was not downloaded.

This is my solution:

  • create a new connection
s = requests.session()
r = s.get(
    url, headers={'connection': 'close', 'range': 'bytes={0}-{1}'.format(start, end)}, stream=True)
  • interrupt the connection
r.close()
s.close()
del r
del s

In my testing, I have found that requests have two attributes named session, one Titlecase, one lowercase, the lowercase one is a function, and the other is a class constructor, they both create a requests.sessions.Session object, is there any difference between them?

And how can I set keep-alive to False?

The method found here won't work anymore:

In [39]: s = requests.session()
    ...: s.config['keep_alive'] = False
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-497f569a91ba> in <module>
      1 s = requests.session()
----> 2 s.config['keep_alive'] = False

AttributeError: 'Session' object has no attribute 'config'

This method from here doesn't throw errors:

s = requests.session()
s.keep_alive = False

But I seriously doubt that it has any effect at all, it just adds a new boolean to the object and I don't think it is handled by the object.

I have seen a close method in requests.models.Response, does it have any effect in this case or I can just close the session?

And finally, using this method, is it guaranteed that the server will never send extra bytes from previous dead connections?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In general, with python, when there is some kind of 'handler' that is supposed to close after use, wrapping the use with with can limit the scope of the thing to a small code block.

ResponseData = None
with requests.get( Url, headers=Headers) as ResponseObject:
    ResponseData = ResponseObject.text.encode('utf-8')
    ResponseData = ResponseData.decode("utf-8") 

#code down here does not have any idea what "ReponseObject" is.
#For some reason python is able to kill it more reilably after `with`

Not sure if this is a cannonical answer but it might help for you. The snippet works for me, but I avoid creating a session entirely. This trick has worked for me for countless other things that were supposed to close, but did not.

EDIT: Following up on session: I guess you can double with and see if it works?

with requests.session() as s:
    with s.get(....) as r:
        #try stuff here
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!