当我尝试登录我的路由器时,它工作正常,但是当密码或用户名错误时,我得到了例外。如何处理此异常?
from requests.auth import HTTPBasicAuth
import requests
def hts():
url = 'http://192.168.1.1/'
name = 'username'
passw = 'password'
auth = HTTPBasicAuth(name,passw)
r = requests.get(url, auth=auth)
try:
if r:
print(r.text)
else:
print("not found")
except requests.exceptions.ContentDecodingError as e:
print('wrong password')
hts()
这是错误
raise ContentDecodingError(e)
requests.exceptions.ContentDecodingError: ('Received response with content-encoding:
gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect
header check',))
答案 0 :(得分:0)
try:
r = requests.get(url, auth=auth)
except requests.exceptions.ContentDecodingError as e: # handling the ContentDecoding exception
print('wrong password')