简体   繁体   English

使用python HTTP登录到路由器并请求模块获取异常

[英]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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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