Bootstrap

python订单详细教程-python实现京东订单推送到测试环境,提供便利操作示例

本文实例讲述了python实现京东订单推送到测试环境,提供便利操作。分享给大家供大家参考,具体如下:

# -*- coding: utf-8 -*-

import hashlib

import time

import requests

from order30 import conf

app_key = conf.jd_appkey

appSecret = conf.jd_secret

token = conf.jd_token

def get_md5(string):#返回字符串md5加密后大写

hl = hashlib.md5()

hl.update(string.encode('utf-8'))

return hl.hexdigest().upper()

def get_timestr():#获取2分钟前的时间

time_now = int(time.time())-120

timestr = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time_now))

return timestr

def req_get_result(api_url,api_data):#get方法请求函数

req_get = requests.get(api_url,api_data)

result = req_get.json()

return result

def req_post_result(api_url,api_data):#post方法请求函数

req_post = requests.post(api_url,data=api_data)

result = req_post.json()

return result

def param_sort(param_dict):#传入字典,返回排序后并且连接好的字符串

keys_list = sorted(param_dict.keys())

rb_str = ''

for k in keys_list:

key_value = k + str(param_dict[k])

rb_str = rb_str + key_value

return rb_str

def op_jd_order(outer_order_id,optype):#向测试环境推送一个订单

api_url_dict = {

"33060":"http://xx.xxx.xxx.com/jd/xxx1",# 用户确认收货完成订单

"32000":"http://xx.xxx.xxx.com/jd/xxx2", #创建新订单

"10":"http://xx.xxx.xxx.com/jd/xxx3",#用户申请售后

}

api_url = api_url_dict[optype]

timestamp = get_timestr()

jd_parms = '{"billId":"%s","statusId":"%s","timestamp":"%s"}'%(outer_order_id,optype,timestamp)

api_data = {

'token':token,

'app_key':app_key,

'timestamp':timestamp,

'format':'json',

'v':'1.0',

'jd_param_json':jd_parms

}

sort_str = param_sort(api_data) #对参数进行排序,连接。

params_str = appSecret + sort_str + appSecret #首尾加上appSecret

sign = get_md5(params_str)#获得签名后的大写字符串

api_data['sign'] = sign

req = req_post_result(api_url,api_data)

return req

希望本文所述对大家Python程序设计有所帮助。

;