Python DEMO

文字短信发送

#文字短信DEMO
import urllib.request
import urllib
import json
import hashlib
import time

#参数配置

url = "http://www.lokapi.cn/smsUTF8.aspx"

rece = "json"
username = "******"
password = "******"
#验证码
tokenYZM = "******"
#参数
param = "17712345678|张三|2547"
templateid = "******"

def MD5(str):
    m = hashlib.md5()
    m.update(str.encode(encoding='UTF-8'))
    return m.hexdigest().upper()

#密码加密
passwd = MD5(password)

#时间戳
ticks =int(time.time() * 1000)

#构造发送主体
dict = {"action": "sendtemplate", "username": username,
        "password": passwd, "token": tokenYZM, "timestamp": ticks}
body = "action=sendtemplate&username={username}&password={password}&token={token}&timestamp={timestamp}".format(username=username,password=passwd,token=tokenYZM,timestamp=ticks
)
sign = MD5(body)
dict["sign"] = sign
dict["rece"] = rece
dict["templateid"] = templateid
dict["param"] = param

headers = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}
data = urllib.parse.urlencode(dict).encode('utf-8')
request = urllib.request.Request(url, data, headers)
text_rece = urllib.request.urlopen(request).read().decode('utf-8')
print("文字短信接收数据:"+ text_rece)

'''
视频短信、弹屏闪信、语音短信、国际短信模板发送与文字短信类似
'''

彩信发送

#彩信短信发送DEMO
import urllib.request
import urllib
import json
import hashlib
import time
import base64

#参数配置

url = "http://www.lokapi.cn/smsUTF8.aspx"

rece = "xml"
username = "******"
password = "******"
token = "******"
mobile = "177123456"
content = "祝你生日快乐"
title="祝福短信"

def MD5(str):
    m = hashlib.md5()
    m.update(str.encode(encoding='UTF-8'))
    return m.hexdigest().upper()


#密码加密
passwd = MD5(password)

#时间戳
ticks = int(time.time() * 1000)

#构造发送主体
dict = {"action": "sendimagetext", "username": username,
        "password": passwd, "token": token, "timestamp": ticks}
body = "action=sendimagetext&username={username}&password={password}&token={token}&timestamp={timestamp}".format(username=username, password=passwd, token=token, timestamp=ticks
                                                                                                                )
sign = MD5(body)
dict["sign"] = sign
dict["rece"] = rece
dict["title"] = title
dict["mobile"] = mobile
#文字
engb = content.encode(encoding='GB2312')
txt = base64.b64encode(engb)
#图片
extension = "jpg"

base64_data=""
with open("D:\\我的文档\\Pictures\\th.jpg", "rb") as f:
    base64_data = base64.b64encode(f.read())

message = "txt|{txt},{ext}|{base_data};".format(txt=txt, ext=extension,base_data=base64_data)
message = message.replace("%", "%25")
message = message.replace("&", "%26")
message = message.replace("+", "%2B")
dict["message"] = message

headers = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}
data = urllib.parse.urlencode(dict).encode('utf-8')
request = urllib.request.Request(url, data, headers)
text_rece = urllib.request.urlopen(request).read().decode('utf-8')
print("彩信短信接收数据:" + text_rece)

余额查询

import urllib.request
import urllib
import json
import hashlib
import time

#参数配置

url = "http://www.lokapi.cn/smsUTF8.aspx"
rece = "json"
username = "******"
password = "******"
#验证码
tokenYZM = "******"


def MD5(str):
    m = hashlib.md5()
    m.update(str.encode(encoding='UTF-8'))
    return m.hexdigest().upper()


#密码加密
passwd = MD5(password)

#时间戳
ticks = int(time.time() * 1000)

#构造发送主体
dict = {"action": "overage", "username": username,
        "password": passwd, "token": tokenYZM, "timestamp": ticks}
body = "action=overage&username={username}&password={password}&token={token}&timestamp={timestamp}".format(username=username, password=passwd, token=tokenYZM, timestamp=ticks
                                                                                                                )
sign = MD5(body)
dict["sign"] = sign
dict["rece"] = rece


headers = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}
data = urllib.parse.urlencode(dict).encode('utf-8')
request = urllib.request.Request(url, data, headers)
text_rece = urllib.request.urlopen(request).read().decode('utf-8')
print("文字短信余额查询接收数据:" + text_rece)

关键词检测

import urllib.request
import urllib
import json
import hashlib
import time

#参数配置

url = "http://www.lokapi.cn/checkWord.aspx"
rece = "json"
username = "******"
password = "******"
message="【乐讯通】您好,您的验证码是5846"

def MD5(str):
    m = hashlib.md5()
    m.update(str.encode(encoding='UTF-8'))
    return m.hexdigest().upper()


#密码加密
passwd = MD5(password)

#时间戳
ticks = int(time.time() * 1000)

#构造发送主体
dict = {"username": username, "message": message,
        "password": passwd,  "timestamp": ticks}
body = "username={username}&password={password}&timestamp={timestamp}".format(username=username, password=passwd, timestamp=ticks
                                                                                                           )
sign = MD5(body)
dict["sign"] = sign
dict["rece"] = rece


headers = {
    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}
data = urllib.parse.urlencode(dict).encode('utf-8')
request = urllib.request.Request(url, data, headers)
text_rece = urllib.request.urlopen(request).read().decode('utf-8')
print("文字短信关键词查询接收数据:" + text_rece)

results matching ""

    No results matching ""