SoFunction
Updated on 2024-07-15

How to set the number of failed retries when requesting http/https using Python

Python request http/https set failed retry counts

When using Python's requests library, there is no retry request on failure by default, which can be supported in the following way

Setting up retry rules on request

import requests
from  import HTTPAdapter

s = ()
a = HTTPAdapter(max_retries=3)
b = HTTPAdapter(max_retries=3)
# Mount retry rules to http and https requests
('http://', a)
('https://', b)

Request Url

Once the above is set up, failure retries can be supported by changing the session's request

r = ('/geocoder?location=39.90733345,116.391244079988&output=json')
# Status codes returned
r.status_code
# Response content, Chinese is utf8 encoded

# Response in string form, Chinese is unicode encoded.

# Encoding in the response header

# Response header information

python access to http and https

import 
import ssl
context = ssl._create_unverified_context()
# url = '/item/vivo'
url ="/"
request = (url)
response = (url=request,context=context)
print (().decode('utf-8'))
 

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.