http client sample(json+md5) in python

1、client

1) httpie

http -f POST example.org hello=World

http POST http://192.168.200.251:55101/Api/Client/Login? Account=zdd_1 Password=0192023a7bbd73250516f069df18b500 DeviceCode=20190806 > file

login.json

{
  "Account": "zdd_1",
  "Password": "0192023a7bbd73250516f069df18b500",
  "DeviceCode": "20190806"
}

http POST http://192.168.200.251:55101/Api/Client/Login?/login.json @$(pwd)/login.json

2) python requests

复制代码
import requests
import json
import datetime
import time
from hashlib import md5

#global var
url_base = 'http://192.168.200.251:55101'
account = "zdd_1"
passwd = "admin123"
device_code = "20190806"

#timeStamp
get_now_milli_time = lambda: int(time.time() * 1000)

#md5
def encrypt_md5(s):
    new_md5=md5()
    new_md5.update(s.encode(encoding='utf-8'))
    return new_md5.hexdigest()


########################################################

# login 
url = url_base + '/Api/Client/Login?'
login = {
  "Account": "",
  "Password": "",
  "DeviceCode": ""
}


login['Account'] = account
login['Password'] = encrypt_md5(passwd)
login['DeviceCode'] = device_code

r = requests.post(url, data = login)
print(r.status_code)
print(r.text)
d = json.loads(r.text)
print(d['Code'])
token = d['Result']['Token']
ID = d['Result']['Id']
print(token)
print(ID)

########################################################

# get reg info
url = url_base + '/Api/Client/GetDeviceRegister?'
get_reg_info = {
  "code": "20190806",
  "ssoToken": "",
  "stationId": "",
  "timeStamp": "",
  "accessSignature": ""
}

get_reg_info['ssoToken'] = token
get_reg_info['timeStamp'] = str(get_now_milli_time()) 
get_reg_info['stationId'] = ""
get_reg_info['code'] = device_code
s1 = encrypt_md5(get_reg_info['timeStamp']).upper()
print(s1)
s2 = encrypt_md5(ID + s1).upper()
print(s2)
get_reg_info['accessSignature'] = s2
r = requests.get(url,params=get_reg_info)
print(r.status_code)
print(r.text)
d = json.loads(r.text)
print(d)
复制代码
原文地址:https://www.cnblogs.com/dong1/p/5240598.html