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

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

相关文章

Pyhton中防止SQL注入的方法

复制代码 代码如下: c=db.cursor() max_price=5 c.execute("""SELECT spam, eggs, sausage FROM breakfast &...

Python编程图形库之Pillow使用方法讲解

Python编程图形库之Pillow使用方法讲解

PIL vs Pillow PIL: Python Imaging Library,是python的图像处理库。由于PIL不兼容setuptools,再加上更新缓慢等因素,Alex Cl...

Python列表生成器的循环技巧分享

Python列表生成器的循环技巧分享

列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。 一个循环 在C语言等其他语言中,for循环一般是这样的 如果py...

python在回调函数中获取返回值的方法

python中有用到回调函数的时候,而回调函数又需要返回数值的时候,就需要先将所被传为回调函数的函数先赋值给一个变量,然后等回调结束之后,将这个变量取值回来就可以了。 如我用到到的调用x...

Python中的 ansible 动态Inventory 脚本

Python中的 ansible 动态Inventory 脚本

1.Ansible Inventory  介绍; Ansible Inventory 是包含静态 Inventory 和动态 Inventory 两部分的,静态 Invento...