当前位置:   article > 正文

python有认证考试吗_python 基本认证

python认证考试有哪些

# import requests

#

# response = requests.get('http://127.0.0.1:8080/manager/html', auth=('tomcat', 'tomcat'))

# print(response.content.decode())

import base64

import re

import urllib.request

from urllib.error import HTTPError

class BasicAuth(object):

def __init__(self, username, password, realm=''):

auth_str = f"{username}:{password}"

encode = base64.standard_b64encode(auth_str.encode()).decode()

auth_str = f'Basic {encode}'

self.auth = auth_str

self.realm = realm

def open(self, url):

request = urllib.request.Request(url)

try:

response = urllib.request.urlopen(request)

return response

except HTTPError as e:

auth_type = e.headers['www-authenticate']

auth_str = re.compile(

'(?P\w*)\s*realm=[\'"](?P[^\'"]*)[\'"]',

re.IGNORECASE)

match_ = auth_str.match(auth_type)

if match_:

match = match_.groupdict()

schema = match.get('schema')

if schema.lower() == 'basic':

request.add_header("Authorization", self.auth)

try:

response = urllib.request.urlopen(request)

print(response.read().decode())

return response

except HTTPError as e:

print(e.headers)

# base = BasicAuth('tomcat', 'tomcat')

# base.open('http://127.0.0.1:8080/manager/html')

basic_auth = urllib.request.HTTPPasswordMgrWithDefaultRealm()

basic_auth.add_password(None, 'http://127.0.0.1:8080/manager/html', 'tomcat', 'tomcat')

handler = urllib.request.HTTPBasicAuthHandler(basic_auth)

opener = urllib.request.build_opener(handler)

urllib.request.install_opener(opener)

response = urllib.request.urlopen('http://127.0.0.1:8080/manager/html')

print(response.read().decode())

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/167128
推荐阅读
相关标签
  

闽ICP备14008679号