Python requests HTTP 인증 로그 인 실현 프로 세 스

1.장면
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
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기