Python example analysis on how to use coding Basic Auth

黄舟
Release: 2017-05-31 14:42:17
Original
2234 people have browsed it

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()
Copy after login

Calling example:

print(get_basic_auth_str('admin', '123456'))
Copy after login

Output

Basic YWRtaW46MTIzNDU2
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template