PHP前端开发

Python关于编码Basic Auth的使用方法的实例分析

百变鹏仔 5小时前 #Python
文章标签 使用方法

这篇文章主要介绍了 python 编码basic auth使用方法简单实例的相关资料,需要的朋友可以参考下

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

代码如下:

import base64def 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'))

输出

立即学习“Python免费学习笔记(深入)”;

Basic YWRtaW46MTIzNDU2