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<schema>\w*)\s*realm=[\'"](?P<realm>[^\'"]*)[\'"]',
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())
python 基本认证的更多相关文章
- Python api认证
本节内容: 基本的api 升级的api 终极版api 环境:Djanao, 项目名:api_auto, app:api 角色:api端,客户端,黑客端 1.基本的api [api端] #api_aut ...
- python http认证
Requests 库有一个auth 模块专门用来处理HTTP 认证: import requestsfrom requests.auth import AuthBasefrom requests.au ...
- python 自动认证登录
import urllib import base64 import urllib2 def auto_login(urllink,username,password): authstr = 'Bas ...
- CentOS下用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境
安装 系统环境:CentOS 6.5 安装依赖 yum -y install gcc gcc-c++ make git patch openssl-devel zlib-devel readline- ...
- Django用openLDAP做认证
前言 之前有需求要做一个django+ldap用户管理的简单接口,研究了好几个模块,最后终于能实现django用ldap做用户认证了.也是自己的水平有限吧,做了好长时间,现在就和大家分享一下这个过程吧 ...
- 用pyenv和virtualenv搭建单机多版本python虚拟开发环境
作为主流开发语言, 用python 开发的程序越来越多. 方便的是大多linux系统里面都默认集成了python, 开发可以随时随地开始. 但有时候这也成为了一个短板, 比如说有时候我们需要开发和调试 ...
- 用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境
作为主流开发语言, 用python 开发的程序越来越多. 方便的是大多linux系统里面都默认集成了python, 开发可以随时随地开始. 但有时候这也成为了一个短板, 比如说有时候我们需要开发和调试 ...
- 使用kafka-python客户端进行kafka kerberos认证
之前说过python confluent kafka客户端做kerberos认证的过程,如果使用kafka python客户端的话同样也可以进行kerberos的认证,具体的认证机制这里不再描述,主要 ...
- drf-Authentication认证
drf-Authentication认证 ## 源码分析 ```python """ 1)APIView的dispath(self, request, *args, ** ...
随机推荐
- patch 28729262
打补丁最后出个error OPatch found the word "error" in the stderr of the make command.Please look a ...
- asp.net core 学习资料整理
net上海俱乐部 白手套大神分享 广州一位大佬总结的系列文章 https://www.cnblogs.com/viter/p/10474091.html 汪宇杰 http://edi.wan ...
- 射线法(1190 - Sleepwalking )
题目:http://lightoj.com/volume_showproblem.php?problem=1190 参考链接:https://blog.csdn.net/gkingzheng/arti ...
- java 生产者消费者简单实现demo
第一种方式 使用BlockingQueue 阻塞队列 public class Threads { public static void main(String[] args) { final Arr ...
- MTV 和 MVC
MTV:(Django中用) M:models T:templates V:views MVC M:models V:views C:control(urls+views)
- 前端开发【第1篇:HTML】
HTML初识 1.什么是HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都 ...
- 单点登录前戏(未使用jwt版本)
建表 from django.db import models import jwt # Create your models here. # 角色表 class RoleTable(models.M ...
- css 自制一些小特效
Github地址 位于gh-pages分支上 https://github.com/a1115040996/MyHTML/ 3D 卡片特效 地址: https://a1115040996.github ...
- linux 服务器常用命令整理
linux 服务器常用命令整理 目录 网络分析 - tcpdump \ telnet \ (netstat \ ss \ lsof) \ nload 网络传输 - scp \ rsync \ (rz ...
- windows使用pyecharts报错 No module named 'pyecharts_snapshot
下载此文件后,使用命令 pip install pyecharts_snapshot-0.1.8-py2.py3-none-any.whl 安装完成即可 链接地址:https://pypi.org/p ...