短信接口更换,看了服务商提供的文档,感觉简单的方式就是通过 python 发送HTTP请求(json格式)来 调用api发送短信,如下,已完成,是python3的脚本,python2 给了示例,其实基本一致,就是函数变了下。
#!/usr/local/bin/python
#-*- coding:utf-8 -*-
import urllib.parse
import urllib.request
from datetime import datetime
import json
url = "http://88fly.blog.163.com"
userName = "herb"
userPassword = "herb"
tradeNo = datetime.now().strftime('%Y%m%d%H%M%S%f')
print (tradeNo)
def send_sms(text, mobile):
body = json.dumps({'userName': userName, 'phones': mobile, 'content': text, 'userPassword': userPassword, 'tradeNo': tradeNo}) ## 转化为json
print (body)
request = urllib.request.Request(url, body.encode('utf-8'))
request.add_header("Content-type", "application/json; charset=utf-8")
response = urllib.request.urlopen(request)
return response.read()
if __name__ == '__main__':
mobile = "15158000000"
text = "您的验证码是:121338。请不要把验证码泄露给其他人。"
send_sms(text, mobile)
print(send_sms(text, mobile))
示例(python2) json格式的post请求
import urllib2
import json
def http_post():
url="http://88fly.blog.163.com"
values ={"user": herb,"pwd": "www","content": "test"}
jdata = json.dumps(values) # 对数据进行JSON格式化编码
req = urllib2.Request(url, jdata) # 生成页面请求的完整数据
response = urllib2.urlopen(req) # 发送页面请求
return response.read()
#############################################
import httplib
import json
test_data = {"user": herb,"pwd": "www","content": "test"}
requrl = "http://88fly.blog.163.com"
headerdata = {"Content-type": "application/json"}
conn = httplib.HTTPConnection("88fly.blog.163.com",80)
conn.request('POST',requrl,json.dumps(test_data),headerdata)
response = conn.getresponse()
res= response.read()
print res
转载请注明:IT笔记分享 » Python/Shell » python发送短信(post json)
版权声明
本站《作品展示》类文章均为原创,转载必须注明出处,技术分享类文章部分来源于网络,版权归原作者所有,若侵权请留言。