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

时间:2014-11-06 16:13:16

标签: python python-requests

当我尝试登录我的路由器时,它工作正常,但是当密码或用户名错误时,我得到了例外。如何处理此异常?

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',))    

1 个答案:

答案 0 :(得分:0)

    try:
       r = requests.get(url,  auth=auth)
    except requests.exceptions.ContentDecodingError as e: # handling the ContentDecoding exception
           print('wrong password')