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程序设计有所帮助。

相关文章

Python数组条件过滤filter函数使用示例

使用filter函数,实现一个条件判断函数即可。 比如想过滤掉字符串数组中某个敏感词,示范代码如下: #filter out some unwanted tags def pass...

python-pyinstaller、打包后获取路径的实例

python-pyinstaller、打包后获取路径的实例

使用pyinstaller可以把.py文件打包为.exe可执行文件,命令为: pyinstaller hello.py 打包后有两个文件夹,一个是dist,另外一个是build,可执行文...

解决django同步数据库的时候app models表没有成功创建的问题

问题描述: 在django中创建了一个app,而且在app中自定义创建了几个数据表,在同步的时候系统自带的表可以成功,但是models中的没有生效,而且进入对应app下的migratio...

pytorch 获取tensor维度信息示例

我就废话不多说了,直接上代码吧! >>> import torch >>> from torch.autograd import Variable...

Python实现按学生年龄排序的实际问题详解

前言 本文主要给大家了关于利用Python按学生年龄排序的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 问题:定义一个Class:包含姓名name、性别gen...