Import Requests Import JSON from the urllib Import the parse class HttpWayBillRquest: '' ' AWB gain change search ' '' DEF the access_token (Self): '' ' Get token ' '' URL = ' http://xxxxxxxxx.com ' username = ' 12333 ' password = ' 12,334,566 ' RES = requests.get (URL, the auth = (username, password)) # Print (' res.text result is: \ n ', res.text) res.text_json = json.loads(res.text) print('access_token的结果为:\n', res.text_json) datas_token = res.text_json['data'] # print( datas_token) token_data = datas_token['data']['access_token'] token_type = datas_token['data']['token_type'] return token_type,token_data def createwaybill(self, datas_create,token_type,token_data): '' ' To create a single or multiple application waybill ' '' create_url = ' http://xxxxxxxxx.com ' header = { ' the Authorization ' : token_type + ' ' + token_data, ' the Content-the Type ' : ' file application / JSON; charset = . 8-UTF ' } datas_json = json.dumps (datas_create) Print ( " application AWB request header: \ n- " , header) Print ( "AWB request parameters apply: \ n- ", Datas_json) RES = requests.post (= create_url URL, Data = datas_json, headers = header) Print ( ' Application AWB response header: \ n- ' , res.headers) Print ( ' Application AWB response body: \ n- ' , RES .text) DEF updatewaybill1 (Self, delivery_id, data_update1, token_type, token_data): '' ' modify a waybill information ' '' update_url1 = " http://xxxxxxxxx.com '' header = { ' the Authorization ' : token_type + ' ' + token_data, ' The Content-the Type ' : ' file application / JSON; charset = UTF-. 8 ' } datas_json = json.dumps (data_update1) RES = requests.put (URL = update_url1 + delivery_id, Data = datas_json, headers = header) Print ( ' Update a waybill response header: \ n- ' , res.headers) Print ( ' updated in response to a single operation thereof: \ n- ' , res.text) DEF updatewaybill2 (Self, data_update2, token_type, token_data): ' '' to update multiple waybill information '' ' update_url2 = ' http://xxxxxxxxx.com/ ' header = { ' the Authorization ' : token_type + ' ' + token_data, ' the Content-the Type ' : ' file application / JSON; charset = UTF-. 8 ' } datas_json = json.dumps (data_update2) RES requests.put = (= update_url2 URL, Data = datas_json, headers = header) Print ( ' updated in response to the first plurality of waybill: \ n ' , res.headers) Print ( ' updated in response to a plurality of body waybill: \ n',res.text) def findwaybill1(self,delivery_id,token_type,token_data): '''查询1条运单信息''' finds_url1 = 'http://test.xapi.xborderpay.com/v3/delivery/' header = { 'Authorization': token_type + ' ' + token_data, 'Content-Type': 'application/json; charset=UTF-8' } url_finds1 = finds_url1 + delivery_id print('Query a waybill request header: ' , header) Print ( ' Query an AWB request URL: ' , url_finds1) RES = requests.get (URL = url_finds1, headers = header) Print ( ' Query an AWB response header: \ n- ' , res.headers) Print ( ' query response to a single operation thereof: \ n- ' , res.text) DEF findwaybill2 (Self, find_data, token_type, token_data): ' '' multiple criteria to waybill information '' ' finds_url2 = " http://xxxxxxxxx.com '' header = { 'Authorization' : Token_type + ' ' + token_data, ' the Content-the Type ' : ' ; file application / charset = UTF-JSON. 8 ' } encode_data = parse.urlencode (find_data) url_finds2 = finds_url2 + ' ? ' + Encode_data Print ( ' many conditions AWB request header: ' , header) Print ( ' many conditions AWB request URL: ' , url_finds2) RES = requests.get (URL = url_finds2,headers=header) Print ( ' many conditions AWB response header: ' , res.headers) Print ( ' many conditions AWB response body: ' , res.text)
Main code: waybill.py
(1) get token
from waybill.WayBill import HttpWayBillRquest Waybillrquest = HttpWayBillRquest() Token = Waybillrquest.access_token() print('token为:{0}\ntoken_type为:{1}\n'.format(Token[1],Token[0]))
(2) create the waybill information
import random import time from waybill.WayBill import HttpWayBillRquest '''创建单笔''' datas_create =[{ 'order_id':'1571452339936063', 'delivery_type':'EMS', 'delivery_number':random.randint(10000000,9999999999999999), 'remarks':time.strftime("%Y-%m-%d",time.localtime(time.time())) }] # '''创建多笔''' # datas_create = [ # {'order_id': '1571298310721172', # 'delivery_type': 'EMS', # 'delivery_number': random.randint(10000000, 9999999999999999), # 'remarks': '2019-11-12', }, # {'order_id': '1571451634287981', # 'delivery_type': 'EMS', # 'delivery_number': random.randint(10000000, 9999999999999999), # 'remarks': '2019-11-12', } # ] Waybillrquest = HttpWayBillRquest() Token = Waybillrquest.access_token() Create = Waybillrquest.createwaybill(datas_create,Token[0],Token[1])
(3) a single waybill information update
import random import time from waybill.WayBill import HttpWayBillRquest delivery_id = '157387294036744' # 原始运单号 data_update1 = { 'order_id': '157145233993606', 'delivery_type': 'TNT', 'delivery_number': random.randint(10000000, 9999999999999999), 'remarks':time.strftime("%Y-%m-%d",time.localtime(time.time())) } Waybillrquest = HttpWayBillRquest() Token = Waybillrquest.access_token() Create = Waybillrquest.updatewaybill1(delivery_id, data_update1,Token[0],Token[1])
(4) to update multiple waybill information
import time from waybill.WayBill import HttpWayBillRquest data_update2 = [ {"delivery_id": "1573461404", "delivery_type": "EMS", "delivery_number": "1573461404", "remarks": time.strftime("%Y-%m-%d",time.localtime(time.time()))}, {"delivery_id": "1573461455", "delivery_type": "EMS", "delivery_number": "1573461455", "remarks": time.strftime("%Y-%m-%d",time.localtime(time.time()))}, {"delivery_id": "1573461530", "delivery_type": "EMS", "delivery_number": "1573461530", "remarks": time.strftime("%Y-%m-%d",time.localtime(time.time()))} ] Waybillrquest = HttpWayBillRquest() Token = Waybillrquest.access_token() Create = Waybillrquest.updatewaybill2(data_update2,Token[0],Token[1])
(5) Find a waybill information
from waybill.WayBill import HttpWayBillRquest delivery_id = "1573119358" Waybillrquest = HttpWayBillRquest() Token = Waybillrquest.access_token() Create = Waybillrquest.findwaybill1(delivery_id,Token[0],Token[1])
(6) multi-criteria to find the waybill information
from waybill.WayBill import HttpWayBillRquest find_data = { "is_checked": "1", "status": "1", "is_activated": "0", "start_date": "2019-10-09", "end_date": "2019-11-08" } Waybillrquest = HttpWayBillRquest() Token = Waybillrquest.access_token() Create = Waybillrquest.findwaybill2(find_data,Token[0],Token[1])