Python 编码Basic Auth使用方法简单实例
发布时间:2020-12-17 08:24:51 所属栏目:Python 来源:网络整理
导读:本片博文主要介绍在Python3 环境下把用户名密码编码成字符串。 代码如下: import base64def get_basic_auth_str(username,password): temp_str = username + ':' + password # 转成bytes string bytesString = temp_str.encode(encoding="utf-8") # base64
本片博文主要介绍在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 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 【python-leetcode202-快慢指针】快乐数
- python – Cumsum作为现有Pandas数据中的新列
- python – httplib2,如何设置多个cookie?
- Python基于smtplib实现异步发送邮件服务
- for(i = 0; True; i)在python中?
- python爬虫练习——爬取链家二手房信息(基础篇)
- 如何在Python / Jupyter笔记本中省略matplotlib打印输出?
- 如何在python数据类中定义日期时间字段?
- python – 将DataFrame的整数索引和生成的SciPy矩阵视为同一
- python – 用于将电子邮件解析为json的库,就像mailgun正在做