python每天定时发送短信脚本
最近业务上需要每天解析txt文本或者excel文件,读取内容发送短信,发送的时间段可控,用python实现
安装pip依赖
pip install -r requirement.txt
xlrd
PyYAML
yaml配置
#读取文件路径
file_path: /Users/user/
send_mode:
# 早于开始时间则于日常开始时间发送,始末时间之间则立即发送,晚于结束时间不发送
begin_time: "08:30:00"
end_time: "17:30:00"
代码
# coding: utf-8
from socket import *
from threading import Timer
import sys
import string
import datetime
import time
import os
import xlrd
import yaml
import chardet
def read_txt(send_path):
file = open(send_path, 'r')
phones = file.read().splitlines()
list = []
for i in range(0, len(phones)):
# 去掉头尾的双引号
if phones[i].find("\"") >= 0:
phones[i] = phones[i][1:-1]
array = phones[i].split()
obj = {
"phno": array[0],
"text": array[1]
}
print obj
list.append(obj)
file.close()
return list
def read_excel(send_path):
sheet1 = xlrd.open_workbook(send_path).sheet_by_index(0)
list = []
for i in range(0, sheet1.nrows):
obj = {
"phno": str(int(sheet1.cell_value(i, 0))),
"text": sheet1.cell_value(i, 1)
}
list.append(obj)
return list
# 从文本中获取发送信息,txt,excel
def get_data():
file_list = os.popen('ls ' + FILE_PATH).read().split()
if len(file_list) > 0:
today = datetime.date.today()
send_time = today - datetime.timedelta(days=1)
send_path = ""
for i in range(0, len(file_list)):
if file_list[i].find(str(send_time)) >= 0:
filename = file_list[i]
send_path = os.path.join(FILE_PATH, filename)
print send_path
break
if send_path != "":
if os.path.exists(send_path):
# 获取文件名后缀
type = os.path.splitext(send_path)[-1][1:].lower()
if type == 'txt':
list = read_txt(send_path)
elif type == 'xlsx':
list = read_excel(send_path)
return list
else:
print "file not exist"
else:
print 'location not exist'
return []
# 宁波短信接口
def send_nb(list):
# 业务参数
datatype = '0000'
inst = '000000'
cate = '00'
sendtime = "0" * 20
expa = "0"
for i in range(0, len(list)):
# 手机号码
phno = list[i]['phno']
# 短信内容
text = list[i]['text']
strphno = phno.ljust(20)
message = datatype + inst + cate + sendtime + expa + strphno + text
message = str(len(message) + 4).zfill(4) + message
# 短信接口请求ip
s = socket(AF_INET, SOCK_STREAM)
s.connect((ip,port))
s.send(message.decode('utf-8').encode("gbk"))
while True:
data = s.recv(1024)
if not data:
break
else:
print data
break
s.close()
print 'send finished'
if __name__ == "__main__":
reload(sys)
sys.setdefaultencoding('utf-8')
current_path = os.path.abspath(os.path.dirname(__file__))
yaml_path = os.path.join(current_path, "config.yaml")
with open(yaml_path, 'r') as f:
config = yaml.load(f)
FILE_PATH = config['file_path']
begin_time = config['send_mode']['begin_time']
end_time = config['send_mode']['end_time']
list = get_data()
if len(list) > 0:
# 发送时间判断,早于开始时间则于日常开始时间发送,始末时间之间则立即发送,晚于结束时间不发送
now_time = time.strftime("%H:%M:%S", time.localtime())
print begin_time + ' ' + end_time
print now_time
if now_time < begin_time:
begin_today = datetime.datetime.strptime(time.strftime("%Y-%m-%d", time.localtime()) + " " + begin_time,
"%Y-%m-%d %H:%M:%S")
now_unix = datetime.datetime.now()
# 间隔执行时间
diff_time = (begin_today - now_unix).seconds
print "send daily: " + str(diff_time) + " seconds"
t = Timer(diff_time, send_nb, (list,))
t.start()
elif now_time <= end_time:
print "send now"
send_nb(list)
else:
print "not send"
python每天定时发送短信脚本的更多相关文章
- quartz-job实现实时或定时发送短信任务
存放调度器(Job 和 Trigger)信息的xml配置文件: 这是某个指定的要实现的定时任务: <!-- 每天给项目经理发送短信避免短信服务挂了 定时每天08:30执行--> <j ...
- python_发送短信脚本
sendsms.py #!/usr/bin/env python # coding: utf-8 import sys import urllib import urllib2 "" ...
- 使用python移动飞信模块发送短信
作者:miaoo 1.应用场景 由于自己做的一个系统需要用到发送短信到自己手机的功能,于是搜索了一下,发现了一个通过移动飞信通道发送短信开源库:PyFetion PyFetion 模拟实现了飞信的通信 ...
- 使用Python往手机发送短信(基于twilio模块)
官网是https://www.twilio.com twilio的一句话介绍——提供SDK帮你连接世界上所有人,你可以很方便的调用他们提供的接口来给指定手机发短信,打电话. 首先在twilio的官网注 ...
- MonkeyRunner执行Python脚本实例——发送短信增强版
很久之前就写好的了,准备写个自动执行Monkey的脚本时才想到去找它,还是写在博客里找起来方便. 这次更新了批处理自动连接设备后执行Py脚本,结构如下图: 其中shotscreen为存放截图文件夹,s ...
- 脚本控制向Android模拟拨打电话,发送短信,定位设置功能
做行为触发的时候要向模拟器实现拨打电话,发送短信,定位设置的的功能,可以很方便通过telnet localhost 5554实现. 写个脚本很快的搞定了.网上资料很多,脚本的很少,也所积点德啦. 写 ...
- zabbix通过脚本发送短信
zabbix通过脚本发送短信 原则 和zabbix电子邮件是一样的,他们是action内部配置,司的api来完毕.当然网上有不少利用139邮箱来发的,这个事实上算调用email的一种,这里复述的是调用 ...
- python发送短信和发送邮件
先注册好 发短信脚本内容 #接口类型:互亿无线触发短信接口,支持发送验证码短信.订单通知短信等. #账户注册:请通过该地址开通账户http://sms.ihuyi.com/register.html ...
- Python发送短信提醒
Python发送短信可借助腾讯云平台提供的短信服务 发送短信需要的及格参数: 1.SDK_AppID和SDK_Key 2.签名: 3.模板ID 下面贴出源码DEMO: from qcloudsms_p ...
随机推荐
- NOIP模拟赛16
NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day2 期望得分:100+100+ =200+ 实际得分:100+40+70=210 T1天天寄快递 直接模拟,代码丢了...... T2天天和不 ...
- 重构改善既有代码设计--重构手法02:Inline Method (内联函数)& 03: Inline Temp(内联临时变量)
Inline Method (内联函数) 一个函数调用的本体与名称同样清楚易懂.在函数调用点插入函数体,然后移除该函数. int GetRating() { return MoreThanfiveLa ...
- JSON 为王,为什么 XML 会慢慢淡出人们的视野?
目前全球信息基础设施的特点是,拥有大量的数据交换格式.这一点也不奇怪.互联网几乎已经老了,而“物联网”及“大数据”正从概念走进现实.但我仍然相信,在这一领域还有一股较强的历史趋势,推动 JSON 数据 ...
- LintCode 510: Maximal Rectangle
LintCode 510: Maximal Rectangle 题目描述 给你一个二维矩阵,权值为False和True,找到一个最大的矩形,使得里面的值全部为True,输出它的面积 Wed Nov 2 ...
- 【洛谷P2015】二叉苹果树
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...
- 【译】Linux提权基础
英文原文: Basic Linux Privilege Escalation 在开始之前,我想指出 - 我不是专家. 据我所知,在这个巨大的领域没有一个“魔法”的答案. 这只是我的发现,写出来,共享而 ...
- 2017ACM暑期多校联合训练 - Team 1 1011 HDU 6043 KazaQ's Socks (找规律)
题目链接 Problem Description KazaQ wears socks everyday. At the beginning, he has n pairs of socks numbe ...
- Codeforces 665E. Beautiful Subarrays (字典树)
题目链接:http://codeforces.com/problemset/problem/665/E (http://www.fjutacm.com/Problem.jsp?pid=2255) 题意 ...
- 关于Java代码优化的35条建议
代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...
- python中的pydoc
在终端上输入pydoc会显示以下信息 pydoc - the Python documentation tool pydoc <name> ... Show text documentat ...