34 lines
1.5 KiB
Python
34 lines
1.5 KiB
Python
import sys, json, hashlib
|
|
sys.stdout.reconfigure(encoding="utf-8")
|
|
from urllib.parse import urlencode
|
|
from urllib import request, error
|
|
|
|
key = "gozTyyHn8987"
|
|
customer = "2B4D1F0C10BC8D66676BC4AED9A78F60"
|
|
secret = "aee6832968234c6b99d97831df88adcc"
|
|
|
|
param = {"com": "shentong", "num": "76952929818011", "resultv2": "1"}
|
|
param_str = json.dumps(param, ensure_ascii=False, separators=(",", ":"))
|
|
|
|
# Method 1: MD5(param + key + customer) -- current code
|
|
sign1 = hashlib.md5((param_str + key + customer).encode("utf-8")).hexdigest().upper()
|
|
|
|
# Method 2: MD5(param + key + secret) -- what kuaidi100 new API expects
|
|
sign2 = hashlib.md5((param_str + key + secret).encode("utf-8")).hexdigest().upper()
|
|
|
|
# Method 3: MD5(param + secret + customer)
|
|
sign3 = hashlib.md5((param_str + secret + customer).encode("utf-8")).hexdigest().upper()
|
|
|
|
url = "https://poll.kuaidi100.com/poll/query.do"
|
|
|
|
for label, sign_val in [("key+customer (current)", sign1), ("key+secret", sign2), ("secret+customer", sign3)]:
|
|
body = urlencode({"customer": customer, "sign": sign_val, "param": param_str}).encode("utf-8")
|
|
req = request.Request(url=url, data=body, headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}, method="POST")
|
|
try:
|
|
with request.urlopen(req, timeout=15) as resp:
|
|
raw = resp.read().decode("utf-8")
|
|
print("{}: {}".format(label, raw))
|
|
except error.HTTPError as exc:
|
|
detail = exc.read().decode("utf-8", errors="ignore")
|
|
print("{}: HTTP {} {}".format(label, exc.code, detail[:200]))
|