A program to verify login: Python Writing flask architecture restful style-April 6, 2017

Source: Internet
Author: User
Tags base64

A program that verifies login

Excerpt from: Geek College-python RESTful API development tool Introduction and application http://www.jikexueyuan.com/course/623.html

ImportBase64Import TimeImportRandom fromFlaskImportFlask,requestapp= Flask (__name__) Users= {    "Magigo": ["123456"]}defGen_token (UID): Token= Base64.b64encode (':'. join ([STR (UID), str (Random.random ()), str (time.time () +7200) ]) users[uid].append (token)returntokendefVerify_token (token):#Print Token_token =Base64.b64decode (token)#Print Users.get (_token.split (': ') [0]) [-1]    if  notUsers.get (_token.split (':') [0]) [-1] = =token:return-1ifFloat (_token.split (':') [-1]) >=time.time ():return1Else:        return0@app.route ('/index', methods=['POST','GET'])defHello_world ():Printrequest.headersreturn 'Hello World'@app. Route ('/login', methods=['POST','GET'])deflogin (): UID, PW= Base64.b64decode (request.headers['Authorization'].split (' ') [-1]). Split (':')    ifUsers.get (UID) [0] = =PW:returnGen_token (UID)Else:        return 'Error'@app. Route ('/test', methods=['POST','GET'])defTest (): Token= Request.args.get ('token')    ifVerify_token (token) = = 1:        return 'Data'    Else:        return 'Error'if __name__=='__main__': App.run (Debug=true)
ImportREQUESTSR= Requests.get ('Http://127.0.0.1:5000/login', Auth= ('Magigo','123456'))PrintR.text Token='bwfnawdvojauotmxodyzmdiynja0oje0ote0nzk3odkumdc='R= Requests.get ('http://127.0.0.1:5000/test', params={'token': token})PrintR.text

A program to verify login: Python Writing flask architecture restful style-April 6, 2017

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.