How to completly reset requests?

2019-08-11 02:54发布

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条回答
Bombasti
2楼-- · 2019-08-11 03:26

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()
查看更多
登录 后发表回答