How to completly reset requests?

2019-08-11 02:34发布

问题:

I'm using requests to make many http requests, and some time, i get timeouts. When i restart the python program, it goes fine. I tried to replicate the "restart the program" with exception handling, but it doesn't works. When i run that :

import requests
session=requests.session()
while 1:
  try:
    session.get('..url..')
  except requests.Timeout:
    session=requests.session()

it doesn't do the same thing as restarting the program : i get stucked whith timeout, whereas when i restart the program, i don't get timeouts any more. What can i do ?

回答1:

not entirely sure, but try this:
this tells the server that you wish to close the connection.

  try:
    session.get(url=url, data=body, headers={'Connection':'close'})
  except requests.Timeout:
    session=requests.session()