python http post json 加密发送

post 采用http协议发送加密的json报文

根据密钥进行md5和base64 加密 发送。

简单实现如下


#!/usr/bin/env python
#coding=utf-8
#post-http-json md5-base64
import json
import hashlib
import base64
import httplib
timeQuotaJsonList=[{"date":"2017-4-7","deliveryCount":"4279753","orderCount":"3590000","pickUpCount":"3381621","signCount":"3114494","time":"0:00:00"},{"date":"2017-4-8","deliveryCount":"4024256","orderCount":"3437090","pickUpCount":"3213663","signCount":"2467296","time":"0:00:00"},{"date":"2017-4-9","deliveryCount":"3949397","orderCount":"3483226","pickUpCount":"3255135","signCount":"1483267","time":"0:00:00"},{"date":"2017-4-10","deliveryCount":"4439157","orderCount":"3857595","pickUpCount":"3319213","signCount":"419318","time":"0:00:00"},{"date":"2017-4-11","deliveryCount":"1738925","orderCount":"3211012","pickUpCount":"1101647","signCount":"6987","time":"0:00:00"}]

def json_md5_base64(timeQuotaJsonList):
    d=dict()
    SECRETKEY= "db290a1b871a4eb589df2a185b21c3d7"
    args1=json.dumps(timeQuotaJsonList)
    Str1=args1+SECRETKEY #添加密钥进行加密
    m = hashlib.md5() #md5加密
    m.update(Str1)
    psw = m.hexdigest()
    args2=base64.b64encode(bytes(psw)) #base64加密
    d["timeQuotaJsonList"]=args1
    d["sign"]=args2
    # print(d)
    return json.dumps(d).encode(encoding='UTF8')
 
def jsonPost():
    conn = httplib.HTTPConnection("127.0.0.1:8081")
    headers = {'Content-Type': 'application/json'}
    data=json_md5_base64(timeQuotaJsonList)
    conn.request("POST", "/hbase-web-in/tt/timeQuota.action", data, headers)
    response = conn.getresponse()
    result = response.read()
    if response.status==200:
        print("success")
        print(result)
    else:
        print("fail" + result)
    conn.close()
 
if __name__== '__main__':
    res=jsonPost()


    
    
    


你可能感兴趣的:(大数据)