简体   繁体   中英

http login to router using python and requests module getting exception

When I try to log in to my router it works fine but when the password or username is wrong I get the exception. How to handle this exception?

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

here is the error

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',))    
    try:
       r = requests.get(url,  auth=auth)
    except requests.exceptions.ContentDecodingError as e: # handling the ContentDecoding exception
           print('wrong password')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM