import requests import hashlib import base64 import json # 请求url --正式地址 Url = "https://api.kdniao.com/api/EOrderService" # 用户ID,快递鸟提供,注意保管,不要泄漏 # 即用户ID,登录快递鸟官网会员中心获取 https://www.kdniao.com/UserCenter/v4/UserHome.aspx EBusinessID = "1311064" # API key,快递鸟提供,注意保管,不要泄漏 # 即API key,登录快递鸟官网会员中心获取 https://www.kdniao.com/UserCenter/v4/UserHome.aspx ApiKey = "9fae1305-3f0c-4d91-8312-e94fd74fa749" def md5(n): md5 = hashlib.md5() md5.update(str(n).encode("utf-8")) return md5.hexdigest() # md5加密 def getSign(n): md5Data = md5(json.dumps(n)+ApiKey) res = str(base64.b64encode(md5Data.encode("utf-8")), "utf-8") return res # 签名 def getParams(): # 请求接口指令 RequestType = "1007" # 组装应用级参数 RequestData = { "OrderCode": "ZTO012657018199", #自定义的订单编号 "ShipperCode": "ZTO", #快递公司编码 "CustomerName": "ZTO2061631234567890", #电子面单账号 "CustomerPwd": "W0123456", #电子面单密码 "PayType": 1, #运费支付方式:1-现付,2-到付,3-月结 "ExpType": 1, #1表示普通订单,2表示线下订单 "Sender": { "Name": "ceshi", "Mobile": "17600588787", "ProvinceName": "上海", "CityName": "上海市", "ExpAreaName": "青浦区", "Address": "明珠路" }, "Receiver": { "Name": "Yann", "Mobile": "15018442396", "ProvinceName": "北京", "CityName": "北京市", "ExpAreaName": "朝阳区", "Address": "三里屯街道" }, "Commodity": [ { "GoodsName": "鞋子", #物品名称 "Goodsquantity": 1, #物品数量 "GoodsWeight": 1.0 #物品重量 } ], "AddService": [ { "Name": "INSURE", #增值服务名称 "Value": "1000" #增值服务值 } ], "Weight": 1.0, "Quantity": 1, #包裹数量,默认设置为1即可 "IsReturnPrintTemplate": 0, #是否返回电子面单模板:0-不需要,1-需要 "Remark": "小心轻放" #物品备注 } # 组装系统级参数 data = { "RequestData": json.dumps(RequestData), "RequestType": RequestType, "EBusinessID": EBusinessID, "DataSign": getSign(RequestData), "DataType": 2 #返回数据类型均为JSON格式 } return data # 请求参数 def post(url, data): res = requests.post(url, data) return res.text # 发送post请求 def getResult(): # 以form表单形式提交post请求,post请求体中包含了应用级参数和系统级参数 result = post(Url, getParams()) print(result) # 输出结果 getResult()