秒嘀短信(四)python发送短信

上一篇用curl命令发送了短信:

python脚本

qqq@mysql:~$ cat miaodi_sms.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/sendSMS"
accountSid="2xxxxxxxxxxxxxxxxxxxxxxxxe";
to="156xxxxxxxx";
templateid="239595";
param="0,2019.12.27 20:58,port 8080 is not listening,tomcat,warning,0";
auth_token="3xxxxxxxxxxxxxxxxxxxc";

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")

效果:

qqq@mysql:~$ python3.6 miaodi_sms.py
python demo starting...
data sent to SMS server is:
b'accountSid=xxxxxxxxxxxxxxxxxxxxxxxx0e&to=156xxxxxxxx&templateid=239595&param=0,2019.12.27 20:58,port 8080 is not listening,tomcat,warning,0&timestamp=1577451910526&sig=9xxxxxxxxxxxxxxx4'
response from SMS server is:
{"respDesc":"请求成功。","smsId":"1227210509004233919947","failCount":"0","failList":[],"respCode":"0000"}
python demo finished

平台上显示效果: