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

yipeiwu_com7年前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相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

linux下python使用sendmail发送邮件

本文实例为大家分享了python使用sendmail发送邮件的具体代码,供大家参考,具体内容如下 参考链接:How do I send mail from a Python script...

完美解决python遍历删除字典里值为空的元素报错问题

exam = { 'math': '95', 'eng': '96', 'chn': '90', 'phy': '', 'chem': '' } 使用下列遍历的方法删除: 1. for...

python+matplotlib实现鼠标移动三角形高亮及索引显示

python+matplotlib实现鼠标移动三角形高亮及索引显示

Trifinder事件实例 实例展示Trifinder对象对的使用。当鼠标移动到一个被分割的三角形上,这个三角形高亮显示,并且它的标签在图标题显示。 展示下演示结果: 完整代码:...

Python统计列表中的重复项出现的次数的方法

本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴。具体方法如下: 对一个列表,比如[1,2,2,2,2,3,3,3,4,4...

Python logging模块用法示例

本文实例讲述了Python logging模块用法。分享给大家供大家参考,具体如下: logging模块 函数式简单配置 import logging logging.debug('...