秒嘀短信(二)python获取账户余额

上一篇用curl获取:https://xyz.uscwifi.xyz/post/SourWa6vM/

这一篇用python来,其实说是python写,还是调用了request

python脚本

[root@tomcat1 ~]# cat miaodi_sms_query.py
#导入一些模块
from urllib import request
from urllib import parse

import urllib.request
import time
import hashlib
#打印提示
print ("python demo starting...")

#传递相关变量
url = "https://openapi.miaodiyun.com/distributor/user/query"
accountSid="2xxxxxxxxxxxxxxxxxxxxxxxe";
to="156xxxxxxxx";
templateid="239xx5";
param="1234";
auth_token="3xxxxxxxxxxxxxxxxxxc";

#使用time.time获取时间戳,round(t * 1000)将浮点数四舍五入为整数
#int()函数将一个字符串或数字转换为整型
#str()函数将对象转换为适于人阅读的形式
t = time.time();
timestamp = str((int(round(t * 1000))));
sig=accountSid+auth_token+timestamp;
m1 = hashlib.md5()
m1.update(sig.encode("utf-8"))
sig = m1.hexdigest()

data="accountSid="+accountSid+"&to="+to+"&templateid="+templateid+"&param="+param+"&timestamp="+timestamp+"&sig="+sig;
headers = {
    'Content-Type':'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36'
                }
data=str.encode(data);

#print("data sent to SMS server is:")
#print(data);
req = request.Request(url, headers=headers, data=data)  #POST方法
page = request.urlopen(req).read()
page = page.decode('utf-8')
print("response from SMS server is:")
print(page)

print ("python demo finished")

效果: