本文實(shí)例講述了python實(shí)現(xiàn)京東訂單推送到測(cè)試環(huán)境,提供便利操作。分享給大家供大家參考,具體如下:
# -*- 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分鐘前的時(shí)間 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方法請(qǐng)求函數(shù) req_get = requests.get(api_url,api_data) result = req_get.json() return result def req_post_result(api_url,api_data):#post方法請(qǐng)求函數(shù) 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):#向測(cè)試環(huán)境推送一個(gè)訂單 api_url_dict = { "33060":"http://xx.xxx.xxx.com/jd/xxx1",# 用戶確認(rèn)收貨完成訂單 "32000":"http://xx.xxx.xxx.com/jd/xxx2", #創(chuàng)建新訂單 "10":"http://xx.xxx.xxx.com/jd/xxx3",#用戶申請(qǐng)售后 } 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) #對(duì)參數(shù)進(jìn)行排序,連接。 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
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
