Python 编码Basic Auth使用方法简单实例

yipeiwu_com6年前Python基础

本片博文主要介绍在Python3 环境下把用户名密码编码成字符串。

代码如下:

import base64
def get_basic_auth_str(username, password):
  temp_str = username + ':' + password
  # 转成bytes string
  bytesString = temp_str.encode(encoding="utf-8")
  # base64 编码
  encodestr = base64.b64encode(bytesString)
  # 解码
  decodestr = base64.b64decode(encodestr)

  return 'Basic ' + encodestr.decode()

调用样例:

print(get_basic_auth_str('admin', '123456'))

输出

Basic YWRtaW46MTIzNDU2

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

浅谈pandas中DataFrame关于显示值省略的解决方法

浅谈pandas中DataFrame关于显示值省略的解决方法

python的pandas库是一个非常好的工具,里面的DataFrame更是常用且好用,最近是越用越觉得设计的漂亮,pandas的很多细节设计的都非常好,有待使用过程中发掘。 好了,发完...

python之matplotlib学习绘制动态更新图实例代码

python之matplotlib学习绘制动态更新图实例代码

简介 通过定时器Timer触发事件,定时更新绘图,可以形成动态更新图片。下面的实例是学习《matplotlib for python developers》一文的笔记。 实现 实现代...

python批量修改图片尺寸,并保存指定路径的实现方法

如下所示: import os from PIL import Image filename = os.listdir("D:\\Work\\process\\样本处理\\pol...

python输出决策树图形的例子

windows10: 1,先要pip安装pydotplus和graphviz: pip install pydotplus pip install graphviz 2,www.g...

Python3实现将文件归档到zip文件及从zip文件中读取数据的方法

本文实例讲述了Python3实现将文件归档到zip文件及从zip文件中读取数据的方法。分享给大家供大家参考。具体实现方法如下: ''''' Created on Dec 24, 2...