How to completly reset requests?

爱⌒轻易说出口 提交于 2019-12-11 14:07:51

问题


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()


来源:https://stackoverflow.com/questions/34313084/how-to-completly-reset-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!