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

yipeiwu_com6年前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 PyQt5标准对话框用法示例

Python PyQt5标准对话框用法示例

本文实例讲述了Python PyQt5标准对话框用法。分享给大家供大家参考,具体如下: 很全的Qt的标准对话框,包含QInputDialog、QColorDialog、QFontDial...

python读取有密码的zip压缩文件实例

python读取有密码的zip压缩文件实例

今天试了一下用zipfile模块读取有密码的zip压缩文件。 今天用winrar 5.6将一个名字为1.xlsx的excel文件打包成1.zip压缩包。采用默认的压缩算法(没有勾选传统加...

详解Python with/as使用说明

with/as 使用open打开过文件的对with/as都已经非常熟悉,其实with/as是对try/finally的一种替代方案。 当某个对象支持一种称为"环境管理协议"的协议时,就...

python实现机器学习之元线性回归

python实现机器学习之元线性回归

一、理论知识准备 1.确定假设函数 如:y=2x+7 其中,(x,y)是一组数据,设共有m个 2.误差cost 用平方误差代价函数 3.减小误差(用梯度下降)...

django-allauth入门学习和使用详解

django-allauth是集成的Django应用程序,用于解决网站身份验证,用户的注册登录及账户管理,以及第三方(社交)账户的身份验证。 既然你知道并准备使用django-allau...