您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關在Python如何是requests實現HTTP驗證登錄功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1、場景
1)用戶輸入完網址后,瀏覽器直接彈出需要輸入用戶名/密碼
PS:此時輸入用戶名密碼即可登錄,或者直接帶著用戶名密碼訪問網站。
假設url為http://xxx.yyy.zzz
用戶名為admin
密碼為123456
則訪問的網址應該為http://admin:123456@xxx.yyy.zzz【http://username:password@url】
直接訪問改網址即可
2)利用requests.get(url)返回狀態碼為401
# -*- encoding=utf-8 -*- import requests if __name__ == '__main__': url = 'http://xxxxx.yyyyyy' response = requests.get(url=url) status_code = response.status_code print status_code text = response.text print text
運行
401 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>401 - Unauthorized: Access is denied due to invalid credentials.</title> <style type="text/css"> <!-- body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} fieldset{padding:0 15px 10px 15px;} h2{font-size:2.4em;margin:0;color:#FFF;} h3{font-size:1.7em;margin:0;color:#CC0000;} h4{font-size:1.2em;margin:10px 0 0 0;color:#000000;} #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF; background-color:#555555;} #content{margin:0 0 0 2%;position:relative;} .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} --> </style> </head> <body> <div id="header"><h2>Server Error</h2></div> <div id="content"> <div class="content-container"><fieldset> <h3>401 - Unauthorized: Access is denied due to invalid credentials.</h3> <h4>You do not have permission to view this directory or page using the credentials that you supplied.</h4> </fieldset></div> </div> </body> </html>
2、HTTP基礎驗證
這是一種簡單的身份認證,它是通過http的authorization請求頭中,攜帶經過base64加密的用戶名和密碼而實現的一種認證
# -*- encoding=utf-8 -*- import requests from requests.auth import HTTPBasicAuth if __name__ == '__main__': url = 'http://xxx.yyy.zzz' user = 'admin' password = '123456' response = requests.get(url=url, auth=HTTPBasicAuth(user, password)) # 或者 # response = requests.get(url=url, auth=(user, password)) print response.status_code
3、摘要式身份認證
# -*- encoding=utf-8 -*- import requests from requests.auth import HTTPDigestAuth if __name__ == '__main__': url = 'http://xxx.yyy.zzz' user = 'admin' password = '123456' response = requests.get(url, auth=HTTPDigestAuth(user, password)) print response.status_code
如果2和3都不行,還是返回401,此時可以試試第4種
使用2和3依舊返回401,此時可以print出response.headers看一下
# -*- encoding=utf-8 -*- import requests from requests.auth import HTTPDigestAuth if __name__ == '__main__': url = 'http://xxx.yyy.zzz' user = 'admin' password = '123456' response = requests.get(url, auth=HTTPDigestAuth(user, password)) print response.status_code print response.headers
運行
401
{'Content-Length': '1293', 'X-Powered-By': 'ASP.NET', 'Server': 'Microsoft-IIS/7.5', 'Date': 'Fri, 05 Jun 2020 05:36:23 GMT', 'Content-Type': 'text/html', 'WWW-Authenticate': 'Negotiate, NTLM'}
打印后可看到headers中帶有字樣'WWW-Authenticate': 'Negotiate, NTLM',表示需要ntlm驗證,此時嘗試使用第4種[/code]
4、ntlm驗證
# -*- encoding=utf-8 -*- import requests from requests_ntlm import HttpNtlmAuth if __name__ == '__main__': url = 'http://xxx.yyy.zzz' user = 'admin' password = '123456' response = requests.get(url, auth=HttpNtlmAuth(user, password)) print response.status_code print response.headers
上述就是小編為大家分享的在Python如何是requests實現HTTP驗證登錄功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。