import requests
import time
import random
import json
class switch(object):
def __init__(self, value):
self.value = value
self.fall =False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fallor not args:
return True
elif self.valuein args:# changed for v1.5, see below
self.fall =True
return True
else:
return False
def send(openid,token,appmsgid):
header ={
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9',
'Connection':'keep-alive',
'Content-Length':'11406',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':open('./cookies.txt',encoding='utf-8',mode='r').read(),
'Host':'mp.weixin.qq.com',
'Origin':'https://mp.weixin.qq.com',
'Referer':'https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid='+openid+'&token='+token+'&lang=zh_CN',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'X-Requested-With':'XMLHttpRequest',
}
url ='https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&f=json&token='+token+'&lang=zh_CN'
post_data ={
'token':token,
'lang':'zh_CN',
'f':'json',
'ajax':1,
'random':0.29628174341321656,
'smart_product':0,
'type':10,
'app_id':appmsgid,
'isMulti':1,
'multi_item':[],
'tofakeid':openid,
'appmsgid':appmsgid,
'imgcode':'',
}
try:
res = requests.post(url, data=post_data, headers=header)
ret = json.loads(res.text)['base_resp']['ret']
for casein switch(ret):
if case(200003):
print('invalid session')
break
if case(200040):
print('invalid csrf token')
break
if case(200002):# default, could also just omit condition or 'if True'
print("openid error")
break
if case(10705):# default, could also just omit condition or 'if True'
print("app msg id not exist")
break
if case(0):# default, could also just omit condition or 'if True'
print("message send success!!!")
break
if case():
print(res.text)
except Exception as e:
print(e)
if __name__ =='__main__':
file =open("./openid.txt",encoding='utf-8',mode='r').read().strip("\n")
if file !='':
openids = file.split("\n")
print(len(openids))
action =int(input("请输入0|1:\n"))
token =input("token:\n")
appmsgid =input("appmsgid|app_id:\n")
# app_id 图文管理查询
if action ==0:
for openidin openids:
if openid !='':
send(openid,token,appmsgid)
time.sleep(random.randint(60,120))
if action ==1:
send(input("请输入openid:\n"),token,appmsgid)