Python:hashlib encryption module, FLASK module Write login interface

Source: Internet
Author: User

Hashlib Module

Mainly used for cryptographic related operations, (such as encrypted strings) in the Python3 version, instead of the MD5 and SHA modules, mainly provide SHA1, sha224, SHA256, sha384, SHA512, MD5 these encryption methods

Import Hashlib

m = Hashlib.md5 () #用md5加密的方式 (MD5 cannot be decrypted after encryption), creating an MD5 object

M.update (b "Hello") #b代表二进制字节bytes, turn the string Hello into bytes, then encrypt it, and use B to convert a variable unknown origin

M.update (bytes (' Hello ', encoding= ' Utf-8 ')) #与上面结果一样, just switch to bytes function, add encoding= ' Utf-8 '

Print (M.digest ()) #2进制格式的加密后的字符串

Print (Len (M.hexdigest ())) #16进制格式的加密后的字符串

PS: Different encryption methods, the use of the same ibid, the MD5 change the line, the following SHA1 as an example:

# ######## SHA1 Encryption Method ########

hash = HASHLIB.SHA1 ()

Hash.update (' admin ')

Print (Hash.hexdigest ())

Flask module:

is a web framework that can develop interfaces

Below write a login interface, login requires URL username passwd

Import Flask

From flask import request,jsonify #引入请求, to request data, to introduce JSON

Server=flask. Flask (__name__) #创建一个服务, consider the current Python file as a service

User= ' admin '

p= ' houning123 '

server.config["Json_as_ascii"]=false #这句话是使返回值的汉字在浏览器里显示正常

@server. Route ('/login ', methods=[' get ') #flask自带的装饰器, this step is to turn the normal function into a service, in parentheses is the path and the Fetch method

def login ():

Username=request.values.get (' username ') #指获取到传的key: The value of "username", assigns the value to the left username

Passwd=request.values.get (' passwd ')

If username and passwd: #判断用户名密码是否为空

If Username==user and passwd==p:

res={"code": +, "MSG": "Login Successful"}

Else

res={"code": $, "MSG": "Account or password Error"}

Return Jsonify (RES)

Else

Return jsonify ({"Code": 1999, "MSG": "Required parameter not filled"}) #把字典转化成json串格式

Server.run (debug=true) #启动服务, debug=true refers to the debug mode, if the code is changed, the service will automatically restart

Python:hashlib encryption module, FLASK module Write login interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.