多线程发送微信消息

多线程发送微信消息

导入需要用的模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import json
import urllib.request
import urllib
import threading

定义一个类 和 run方法

#继承Thread,需要实现run方法
class SendWechat(threading.Thread):
    """send wechat"""
    def __init__(self,jobnumbers,message):
        self.jobnumbers = jobnumbers
        self.message = message
        threading.Thread.__init__(self)

    def run(self):
        nonce = "".join(random.sample('qwertyuiopasdfghjklzxcvbnm1234567890', 32))
        token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        wx_url = 'http://ip:port/Api/WeChat/SendTextMessage_V2?TCOA_Token=' + token + '&TCOA_Nonce=' + nonce
        wx_header = {"Content-Type": "application/json"}
        if True:
            send_data = json.dumps(
                {
                    "jsonrpc": "2.0",
                    "touser": self.jobnumbers,
                    "message": self.message + '\n''DFS自助平台地址(请用谷歌浏览器chrome打开): http://it.tcent.cn/dfs/',
                    "agentId": 54
                }).encode('utf-8')
            request = urllib.request.Request(wx_url, send_data)
            for key in wx_header:
                request.add_header(key, wx_header[key])
            try:
                result = urllib.request.urlopen(request)
            except Exception as e:
                print(e)

发送微信消息到同程旅游公众号(测试账号)

def sendwechat(jobnumbers,message):
    send_wechat = SendWechat(jobnumbers,message)
    send_wechat.start()

结果显示

发表评论

电子邮件地址不会被公开。 必填项已用*标注