National Day holiday fourth day, 1, API code; 2, some concept code, etc.

Source: Internet
Author: User

1.1 Client Side

#!/usr/bin/env python
# _*_ Coding:utf-8 _*_

# @Time: 2017/10/02
# @File: auto_client/src/apiauthtest.py

Import hashlib,time,requests
def MD5 (ARG):
HS = HASHLIB.MD5 ()
Hs.update (Arg.encode (' Utf-8 '))
Return Hs.hexdigest ()

Key = ' sfahs454if34q595331dg4323shfiuerq112u ' # can be written in config file
CTime = str (time.time ())
Final_key = '%s|%s '% (key,ctime)
MD5_STR = MD5 (Final_key)
Print (MD5_STR)
Auth_header_val = '%s|%s '% (md5_str,ctime)
Print (Auth_header_val)

Response = Requests.get (' Http://127.0.0.1:8177/api/api_auth ',
headers={' Api-auth ': Auth_header_val})
# Be sure to note key keys in Hearders,
# The Word key connection cannot write _, such as ' Api_auth ',
# need to write ' Api-auth ', when the server to read the writing, request. Meta.get (' Http_api_auth ' 0
Print (Response.text)

1.2 Server Side
#!/usr/bin/env python
# _*_ Coding:utf-8 _*_

# @Time: 2017/10/2
# @File: auto_server/api/plugins/apiauth.py

Import Time,hashlib
From django.shortcuts import HttpResponse
From django.conf Import settings

Key = settings. Api_auth_key
Visited_key_dict = []
# This should be written to the database

def MD5 (ARG):
HS = HASHLIB.MD5 ()
Hs.update (Arg.encode (' Utf-8 '))
Return Hs.hexdigest ()

def Api_auth (func):
def innner (Request,*args,**kwargs):
Server_float_time = Time.time ()
Auth_hearder_val = Request. Meta.get (' Http_api_auth ')
# Get encrypted text and encryption time
Client_md5_str,client_ctime = Auth_hearder_val.split (' | ')
SERVER_MD5_STR = MD5 ('%s|%s '% (key,client_ctime))
Print ()
Client_float_ctime = float (client_ctime)

If Client_float_ctime +20 < server_float_time:
# validation plus password is valid for 20 seconds
Return HttpResponse (' Verify plus password exceeds time limit ')

If client_md5_str! = server_md5_str:
return HttpResponse (' cryptographic captcha error ')

If CLIENT_MD5_STR in Visited_key_dict:
Return HttpResponse (' Verification code is already in use ')

Visited_key_dict.append (CLIENT_MD5_STR)
return func (Request,*args,**kwargs)

Return Innner

2.1 Some concepts and codes
The most basic data structure in Python is the sequence. Python built-in sequence, list, Ganso, String. The data type of the dictionary is mapping (the data type of the only built-in mappings in Python). Sequences and mappings are two main types of containers. Python does not have a separate heap (data structure) type. There is only one module (HEAPQ) that contains some heap operation functions.

The environment variable is not part of the Python interpreter, it belongs to the operating system.
If you do not set the __all__,import* statement, the default import module has all the global names that do not begin with the underscore .

Map () function and lambda (omit some unnecessary for loop code, the following can be used as a list deduction ...) )









National Day holiday fourth day, 1, API code; 2, some concept code, etc.

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.