Python requests HTTP 인증 로그 인 실현 프로 세 스
1)사용자 가 인터넷 주 소 를 입력 한 후 브 라 우 저 에서 사용자 이름/비밀 번 호 를 입력 해 야 합 니 다.
PS:이때 사용자 이름 비밀 번 호 를 입력 하면 로그 인하 거나 사용자 이름 비밀 번 호 를 가지 고 사 이 트 를 방문 할 수 있 습 니 다.
urlhttp://xxx.yyy.zzz
관리자
비밀 번 호 는 123456 입 니 다.
방문 한 사이트 주소 는http://admin:[email protected]【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;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{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"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
<h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
</fieldset></div>
</div>
</body>
</html>
2.HTTP 기초 검증이것 은 http 의 authorization 요청 헤더 에서 base 64 암호 화 된 사용자 이름과 비밀 번 호 를 가지 고 있 는 간단 한 인증 입 니 다.
# -*- 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 을 되 돌려 줍 니 다.이 때 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
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.