基于Python制作短信发送程序

目录
  • 一、Python短信发送界面最后的效果
  • 二、准备:注册腾讯云账号并配置短信功能
  • 三.初始化短信发送程序窗口
    • 3.1初始化窗口菜单
    • 3.2初始化窗口控件
    • 3.3编写事件触发程序
  • 四、完整源代码

一、Python短信发送界面最后的效果

二、准备:注册腾讯云账号并配置短信功能

(1)注册腾讯云账号

登录腾讯云网址

(2)获取AppID、AppKey

在短信功能页面下,从应用管理>应用列表,获取ID、Key。

(3)创建签名

在短信功能页面下,进入国内短信>签名管理,创建签名。

(4)创建正文模板

在短信功能页面下,进入国内短信>正文模板管理,创建模版。并获取模板ID备用。

三.初始化短信发送程序窗口

3.1初始化窗口菜单

菜单具备打开手机号码文件、保存记录、查看版本等功能。

    menu=tkinter.Menu(root)
    submenu1 = tkinter.Menu(menu, tearoff=0)
    submenu1.add_command(label='打开', command=open_file)
    submenu1.add_command(label='保存', command=save_file)
    menu.add_cascade(label='文件',menu=submenu1)
    submenu3 = tkinter.Menu(menu, tearoff=0)
    submenu3.add_command(label='版本信息', command=Introduction)
    menu.add_cascade(label='帮助',menu=submenu3)
    root.config(menu=menu)

3.2初始化窗口控件

控件包括号码输入框、发送信息按钮,记录显示框。

    global text1,text2
    label1 = tkinter.Label(root, text="手机号码:", font=("微软雅黑", 18))
    label1.place(x=30,y=32)
    text1 = tkinter.Text(root, wrap = 'none', font=("微软雅黑", 18))
    text1.place(x=30+120,y=30, width=520-120-100, height=40)
    button=tkinter.Button(root, text='发送信息',width=10, height=20, bg='gray', fg='white', font=("微软雅黑", 12),command=send_Button)
    button.place(x=480,y=30,width=70, height=40)
    sx = tkinter.Scrollbar(root,orient = tkinter.HORIZONTAL)
    sx.pack(side = tkinter.BOTTOM,fill = tkinter.X)
    sy = tkinter.Scrollbar(root)
    sy.pack(side = tkinter.RIGHT,fill = tkinter.Y)
    text2 = tkinter.Text(root, yscrollcommand = sy.set, xscrollcommand = sx.set, wrap = 'none', font=("微软雅黑", 10))
    text2.place(x=30,y=100, width=520, height=400)
    text2.config(wrap=tkinter.WORD)
    text2.see(tkinter.END);
    sx.config(command = text2.xview)
    sy.config(command = text2.yview)

3.3编写事件触发程序

3.3.1文件打开

def open_file():
    global file_path,phone_numbers,flag
    file_path = filedialog.askopenfilename()
    if file_path is not "":
        data=pandas.read_excel(file_path)
        phone = data['号码'].tolist()
        for i in range(len(phone)):
            phone_numbers.append(str(phone[i]))
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"打开文件成功!"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"文件路径为:"+file_path+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"文件内容如下:"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,data, '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"\n", '\n')
        text2.see(tkinter.END);
        flag = 1
    else:
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"您未打开文件!"+"\n", '\n')
        text2.see(tkinter.END);
        flag = 0

3.3.2文件保存

def save_file():
    file=open("recorde.txt","a+")
    content=str(text2.get("0.0", "end"))
    file.write(content)
    file.close()
    text2.insert(tkinter.END,"*********************************"+"\n", '\n')
    text2.see(tkinter.END);
    text2.insert(tkinter.END,"保存记录到recorde.txt成功!"+"\n", '\n')
    text2.see(tkinter.END);
    tkinter.messagebox.showinfo('提示','保存记录到recorde.txt成功!')
    text2.see(tkinter.END);

3.3.3帮助菜单

def Introduction():
    text2.insert(tkinter.END,"*********************************"+"\n", '\n')
    text2.see(tkinter.END);
    text2.insert(tkinter.END,"版本信息:短信息通知程序 V1.0"+"\n", '\n')
    text2.see(tkinter.END);
    tkinter.messagebox.showinfo('版本信息' ,'短信息通知程序 V1.0')
    text2.see(tkinter.END);

3.3.4发送按钮

def send_Button():
    global flag,phone_numbers
    appid = "你的appid"
    appkey = "你的appkey"
    template_id = "你的模板ID"
    sms_sign = "你的公众号名称"
    params = []
    ssl._create_default_https_context = ssl._create_unverified_context
    ssender = SmsSingleSender(appid, appkey)
    txt1 = str(text1.get("0.0", "end")).replace('\n', '')
    if flag==0:
        if ',' in txt1:
            phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
        elif ',' in txt1:
            phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
        else:
            phone_numbers=[]
            phone_numbers.append(txt1)
    else:
        flag = 0
    count=0
    for l in phone_numbers:
        count=count+len(str(l))
    if count%11==0:
        result = ""
        for i in range(len(phone_numbers)):
            try:
                result = ssender.send_with_param(86, phone_numbers[i],template_id, params, sign=sms_sign, extend="", ext="")
            except HTTPError as e:
                result=e
            except Exception as e:
                result=e
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"信息发送至手机号:"+"\n"+str(phone_numbers[i])+"\n")
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"信息发送返回结果:"+"\n")
            text2.see(tkinter.END);
            text2.insert(tkinter.END,str(result)+"\n", '\n')
            text2.see(tkinter.END);
            if result['errmsg']=='OK':
                text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】成功!"+"\n")
                text2.see(tkinter.END);
            else:
                text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】失败!"+"\n")
                text2.see(tkinter.END);
    else:
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"手机号码格式不正确"+"\n", '\n')
        text2.see(tkinter.END);

四、完整源代码

import tkinter
import tkinter.messagebox
from tkinter import filedialog
import pandas
import ssl
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError 

def open_file():
    global file_path,phone_numbers,flag
    file_path = filedialog.askopenfilename()
    if file_path is not "":
        data=pandas.read_excel(file_path)
        phone = data['号码'].tolist()
        for i in range(len(phone)):
            phone_numbers.append(str(phone[i]))
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"打开文件成功!"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"文件路径为:"+file_path+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"文件内容如下:"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,data, '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"\n", '\n')
        text2.see(tkinter.END);
        flag = 1
    else:
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"您未打开文件!"+"\n", '\n')
        text2.see(tkinter.END);
        flag = 0

def save_file():
    file=open("recorde.txt","a+")
    content=str(text2.get("0.0", "end"))
    file.write(content)
    file.close()
    text2.insert(tkinter.END,"*********************************"+"\n", '\n')
    text2.see(tkinter.END);
    text2.insert(tkinter.END,"保存记录到recorde.txt成功!"+"\n", '\n')
    text2.see(tkinter.END);
    tkinter.messagebox.showinfo('提示','保存记录到recorde.txt成功!')
    text2.see(tkinter.END);

def Introduction():
    text2.insert(tkinter.END,"*********************************"+"\n", '\n')
    text2.see(tkinter.END);
    text2.insert(tkinter.END,"版本信息:短信息通知程序 V1.0"+"\n", '\n')
    text2.see(tkinter.END);
    tkinter.messagebox.showinfo('版本信息' ,'短信息通知程序 V1.0')
    text2.see(tkinter.END);

def send_Button():
    global flag,phone_numbers
    appid = "你的appid"
    appkey = "你的appkey"
    template_id = "你的模板ID"
    sms_sign = "你的公众号名称"
    params = []
    ssl._create_default_https_context = ssl._create_unverified_context
    ssender = SmsSingleSender(appid, appkey)
    txt1 = str(text1.get("0.0", "end")).replace('\n', '')
    if flag==0:
        if ',' in txt1:
            phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
        elif ',' in txt1:
            phone_numbers=str(text1.get("0.0", "end")).replace('\n', '').split(',')
        else:
            phone_numbers=[]
            phone_numbers.append(txt1)
    else:
        flag = 0
    count=0
    for l in phone_numbers:
        count=count+len(str(l))
    if count%11==0:
        result = ""
        for i in range(len(phone_numbers)):
            try:
                result = ssender.send_with_param(86, phone_numbers[i],template_id, params, sign=sms_sign, extend="", ext="")
            except HTTPError as e:
                result=e
            except Exception as e:
                result=e
            text2.insert(tkinter.END,"*********************************"+"\n", '\n')
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"信息发送至手机号:"+"\n"+str(phone_numbers[i])+"\n")
            text2.see(tkinter.END);
            text2.insert(tkinter.END,"信息发送返回结果:"+"\n")
            text2.see(tkinter.END);
            text2.insert(tkinter.END,str(result)+"\n", '\n')
            text2.see(tkinter.END);
            if result['errmsg']=='OK':
                text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】成功!"+"\n")
                text2.see(tkinter.END);
            else:
                text2.insert(tkinter.END,"信息发送至【"+str(phone_numbers[i])+"】失败!"+"\n")
                text2.see(tkinter.END);
    else:
        text2.insert(tkinter.END,"*********************************"+"\n", '\n')
        text2.see(tkinter.END);
        text2.insert(tkinter.END,"手机号码格式不正确"+"\n", '\n')
        text2.see(tkinter.END);

def init_frame(root):
    menu=tkinter.Menu(root)
    submenu1 = tkinter.Menu(menu, tearoff=0)
    submenu1.add_command(label='打开', command=open_file)
    submenu1.add_command(label='保存', command=save_file)
    menu.add_cascade(label='文件',menu=submenu1)
    submenu3 = tkinter.Menu(menu, tearoff=0)
    submenu3.add_command(label='版本信息', command=Introduction)
    menu.add_cascade(label='帮助',menu=submenu3)
    root.config(menu=menu)
    global text1,text2
    label1 = tkinter.Label(root, text="手机号码:", font=("微软雅黑", 18))
    label1.place(x=30,y=32)
    text1 = tkinter.Text(root, wrap = 'none', font=("微软雅黑", 18))
    text1.place(x=30+120,y=30, width=520-120-100, height=40)
    button=tkinter.Button(root, text='发送信息',width=10, height=20, bg='gray', fg='white', font=("微软雅黑", 12),command=send_Button)
    button.place(x=480,y=30,width=70, height=40)
    sx = tkinter.Scrollbar(root,orient = tkinter.HORIZONTAL)
    sx.pack(side = tkinter.BOTTOM,fill = tkinter.X)
    sy = tkinter.Scrollbar(root)
    sy.pack(side = tkinter.RIGHT,fill = tkinter.Y)
    text2 = tkinter.Text(root, yscrollcommand = sy.set, xscrollcommand = sx.set, wrap = 'none', font=("微软雅黑", 10))
    text2.place(x=30,y=100, width=520, height=400)
    text2.config(wrap=tkinter.WORD)
    text2.see(tkinter.END);
    sx.config(command = text2.xview)
    sy.config(command = text2.yview)
    root.update()

if __name__=="__main__":
    global flag
    flag = 0
    global phone_numbers
    phone_numbers = []
    root = tkinter.Tk()
    root.title("短信息发送程序")
    root.geometry('600x520')
    init_frame(root)
    root.mainloop()

以上就是基于Python制作短信发送程序的详细内容,更多关于Python短信发送的资料请关注我们其它相关文章!

(0)

相关推荐

  • python如何使用腾讯云发送短信

    腾讯云方面的申请和流程都比较简单,基本都是可视化操作的,这里就不在赘述了.这篇文章着重讲解怎么用python实现调用. 我假设你已经满足了以下几个前提 + 已经开通了腾讯云短信业务 + 创建好了短信签名 + 也已经审核过了短信正文模板 + 并且已经知道自己的SDK AppID.签名ID.短信模板ID Python 相关需要安装腾讯云提供的模块或SDK 我们以qcloudsms_py模块为准,首先 pip install qcloudsms_py 发送短信我们需要用到的模块有下面2个 from q

  • 教你用Python实现短信验证码的发送

    目录 1. 短信API平台 2. 使用官方提供的SDK实现短信发送 2.1 安装SDK 2.2 使用官方的测试用例进行测试 2.3 参数及其参数的查看 3. 单例模式实现短信发送 总结 1. 短信API平台 使用的短信API平台为:容联云(https://www.yuntongxun.com/) 开发者文档:http://doc.yuntongxun.com/pe/5a531a353b8496dd00dcdfe2 2. 使用官方提供的SDK实现短信发送 Python SDK 文档 Python

  • python网络爬虫实现发送短信验证码的方法

    前言:今天要总结的是如何用程序来实现短信发送功能.但是呢,可能需要我们调用一些api接口,我会详细介绍.都是自己学到的,害怕忘记,所以要总结一下,让写博客成为一种坚持的信仰.废话不多说,我们开始吧! 网络爬虫实现发送短信验证码 在实现我们目标的功能之前,我们要有自己的思路,否则你没有方向,又如何实现自己的代码功能呢? 我们要发送短信,那么我们其实是需要分析的.我们可以去分析一个可以发送短信的网站页面. 我们来到这里如下: 可以看到这是一个注册界面,我们在注册时会被要求需要填写手机号码的·,其实还

  • python twilio模块实现发送手机短信功能

    前排提示:这个模块不是用于对陌生人进行短信轰炸和电话骚扰的,这个模块也没有这个功能,如果是抱着这个心态来的,可以关闭网页了 语言:python 步骤一:安装twilio模块 pip install twilio 步骤二:进入官网注册 https://www.twilio.com 注册完毕之后,会有一个调查,问你准备做什么项目,在这里先选择短信项目 项目创建之后,申请一个试用号码 在上方图片中有用的信息有三个:试用号码,账号SID,和验证令牌,后两个用右边的复制按钮进行复制 步骤三:绑定一个手机号

  • python实现发送和获取手机短信验证码

    首先为大家分享python实现发送手机短信验证码后台方法,供大家参考,具体内容如下 1.生成4位数字验证码 def createPhoneCode(session): chars=['0','1','2','3','4','5','6','7','8','9'] x = random.choice(chars),random.choice(chars),random.choice(chars),random.choice(chars) verifyCode = "".join(x) s

  • 基于Python制作短信发送程序

    目录 一.Python短信发送界面最后的效果 二.准备:注册腾讯云账号并配置短信功能 三.初始化短信发送程序窗口 3.1初始化窗口菜单 3.2初始化窗口控件 3.3编写事件触发程序 四.完整源代码 一.Python短信发送界面最后的效果 二.准备:注册腾讯云账号并配置短信功能 (1)注册腾讯云账号 登录腾讯云网址 (2)获取AppID.AppKey 在短信功能页面下,从应用管理>应用列表,获取ID.Key. (3)创建签名 在短信功能页面下,进入国内短信>签名管理,创建签名. (4)创建正文模

  • 基于Python制作天眼查小程序的示例代码

    目录 界面搭建 整体布局 界面美化 天眼查爬虫 获取信息 代码编写 结果展示 今天我们一起来制作一个天眼查GUI程序,开宗明义,我们先来看下最终的效果 这次的GUI程序,我们使用的框架是PyQt5,该框架拥有比tkinter更为丰富的内置组件,在界面美化方面,貌似也更胜一筹! 从上图也可以看出,我们的目标还是蛮远大的,最终我们希望可以完成一个工具集合,把我们日常当中常用的功能都集成的该GUI程序中,比如天眼查公司信息,知乎用户知识图谱,B视频弹幕抓取等等. 好了,今天我们先完成天眼查的功能吧~

  • IOS程序开发之跳转短信发送界面实现发送短信功能

    项目需求:在程序开发中,我们需要在某个程序里面发送一些短信验证(不是接收短信验证,关于短信验证,传送门:http://www.cnblogs.com/wolfhous/p/5096774.html 项目实现: 新建demo,直接看我源码标志. 源码截图 真机截图 就是如此简单,如您有任何问题/建议或者更好的实现方法,联系本人. 可以看我折叠的源码 /** 点击发送短信按钮*/ - (IBAction)sendMessageBut:(id)sender { /** 如果可以发送文本消息(不在模拟器

  • 利用python实现短信和电话提醒功能的例子

    有时候,我们需要程序帮我们自动检测某些事件的发生 这个需求是广泛存在的 因此,这里整理了利用python实现短信和电话提醒功能的方法 主要需要完成以下4个步骤: - 安装核心库:twilio - 注册账号及配置 - 发送短信示例 - 电话提醒示例 twilio twilio是我们需要的核心库,我们要利用其提供的api完成所需的功能 首先安装twilio模块(我使用的是python3) pip3 install twilio 注册账号及配置 首先访问https://www.twilio.com/并

  • 基于Python制作一键桌面整理工具

    目录 前言 效果展示 开发思路 完整代码 前言 我承认我不是一个爱整理桌面的人,因为我觉得乱糟糟的桌面,反而容易找到文件. 哈哈,可是最近桌面实在是太乱了,自己都看不下去了,几乎占满了整个屏幕.虽然一键整理桌面的软件很多,但是对于其他路径下的文件,我同样需要整理,于是我想到使用Python,完成这个需求. 效果展示 我一共为将文件分为9个大类,分别是图片.视频.音频.文档.压缩文件.常用格式.程序脚本.可执行程序和字体文件. # 不同文件组成的嵌套字典 file_dict = { '图片': [

  • python调用短信猫控件实现发短信功能实例

    python调用短信猫控件实现发短信功能实例代码如下所示: #! /usr/bin/env python #coding=gbk import sys import win32com.client ocxname='ShouYan_SmsGate61.Smsgate' axocx=win32com.client.Dispatch(ocxname) axocx.CommPort=8#设置COM端口号 axocx.SmsService='+8613800100500'#设置短信服务号码 axocx.

  • 基于PHP实现短信验证码接口(容联运通讯)

    自己也是刚刚研究,希望对也在研究的伙伴有帮助. 步骤: 1.登录荣联运通讯注册获取ACCOUNT SID.AUTH TOKEN.Rest URL(生产).AppID(默认): 2.注册测试用手机号码(先注册测试号码方可使用): 3.下载demo示例,并将代码放到项目中(最好单独建文件夹存储). 代码区: 一.新建test.app.php(测试用控制器) <?php /* * 短信接口测试 */ class TestApp extends ShoppingbaseApp{ public funct

  • Android Mms之:短信发送流程(图文详解)

    信息的发送,对于Mms应用程序来讲主要就是在信息数据库中创建并维护一条信息记录,真正的发送过程交由底层(Frameworks层)函数来处理. 总体的来讲,当信息创建完成后,对于信息通常有三个去处,一个是放弃这个信息,也就是用户不想要此信息,一旦选择,信息将不会被保存:第二个去处就是保存为草稿:最后一个去处就是发送此信息. 当点击了发送后,UI层暂不会有变化,UI层要监听负责发送的各个类的回调信息和数据库的变化信息来更新UI.信息发送的第一站是WorkingMessage,它会先处理一下信息的相关

  • Vue组件开发之LeanCloud带图形校验码的短信发送功能

    有15万开发者使用LeanCloud服务,其中不乏知乎.懂球帝.爱范儿.拉卡拉等知名应用,LeanCloud提供了数据存储.即时消息--等一站式服务,并从常用的用户管理需求出发,提供了邮箱验证.短信验证--等用户账户相关的服务. 为防止攻击者恶意发送海量短信造成用户账户损失并影响正常业务,LeanCloud推出了免费图形校验码服务,并且可以在应用设置中设置"强制短信验证服务使用图形校验码". Vue是目前使用较广泛的三大前端框架之一,其数据驱动及组件化的特性使得前端开发更为快捷便利.

随机推荐