簡體   English   中英

使用python HTTP登錄到路由器並請求模塊獲取異常

[英]http login to router using python and requests module getting 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()

這是錯誤

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