Home >Backend Development >Python Tutorial >Python example analysis on how to use coding Basic Auth

Python example analysis on how to use coding Basic Auth

黄舟
黄舟Original
2017-05-31 14:42:172244browse

This article mainly introduces the relevant information of Python Simple examples of how to use coding Basic Auth. Friends who need it can refer to it

This blog post mainly introduces the user in the Python3 environment The first name password is encoded into a string.

The code is as follows:

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()

Calling example:

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

Output

Basic YWRtaW46MTIzNDU2

The above is the detailed content of Python example analysis on how to use coding Basic Auth. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn