Celery--短信与邮件
1 Celery 实现短信--邮件
1.1 容联云-短信
from ronglian_sms_sdk import SmsSDKaccountSid = '8a216da8757784cd0175924caccd0b92'# 说明:主账号,登陆云通讯网站后,可在控制台首页中看到开发者主账号ACCOUNT SID。accountToken = '4173b5d8e6ae4126acb3211ab9f4c037'# 说明:主账号Token,登陆云通讯网站后,可在控制台首页中看到开发者主账号AUTH TOKEN。appId = '8a216da8757784cd0175924cadaa0b98'# 请使用管理控制台中已创建应用的APPID。serverIP = 'app.cloopen.com'# 说明:请求地址,生产环境配置成app.cloopen.com。serverPort = '8883'# 说明:请求端口 ,生产环境为8883.softVersion = '2013-12-26' # 说明:REST API版本号保持不变。sdk = SmsSDK(accountSid, accountToken, appId)# tid:模板id 默认为1res = sdk.sendMessage('1', '18704992144', ['8888','5'])print(res)# 可参考demo中的接口调用文件:SendTemplateSMS.py。
sms_test / sms.py
# import sys# sys.path.insert(0,'/root/opwf_project/celery_task')# from main import send_smsfrom celery_task.main import send_sms# send_sms('1','13355555555',('8888','5'))# delay的意思就是把后面的数据发到redis上面去 调用这个redis就会多一条消息多一个任务# 紧接着celery就会收到这个任务for i in range(0,5): send_sms.delay('1', '13355555555', ('8888', '5'))# 注意:celery是怎么导进来的 启动celery就怎么启动#1、celery -A celery_task.main worker -l info => from celery_task.main import send_sms#2、celery -A main worker -l info => from main import send_sms
sms_test / test_celery_sms.py
import osimport sysimport timefrom celery import Celery#执行celery命令时,需要进入CELERY_BASE_DIR目录执行#celery项目中的所有导包地址,都是CELERY_BASE_DIR为基础设定# CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__))# sys.path.insert(0,CELERY_BASE_DIR) #增加导包路径#定义celery实例,需要的参数 1,实例名app = Celery('wbh',broker='redis://192.168.56.100:6379/14',backend='redis://192.168.56.100:6379/15')@app.taskdef send_sms(tid, mobile, datas): time.sleep(10) from ronglian_sms_sdk import SmsSDK accountSid = '8a216da8757784cd0175924caccd0b92' # 说明:主账号,登陆云通讯网站后,可在控制台首页中看到开发者主账号ACCOUNT SID。 accountToken = '4173b5d8e6ae4126acb3211ab9f4c037' # 说明:主账号Token,登陆云通讯网站后,可在控制台首页中看到开发者主账号AUTH TOKEN。 appId = '8a216da8757784cd0175924cadaa0b98' # 请使用管理控制台中已创建应用的APPID。 sdk = SmsSDK(accountSid, accountToken, appId) # tid:模板id 默认为1 res = sdk.sendMessage(tid, mobile, datas) print(res) return res
celery_task / main.py
1.2 @163.com-邮件
import smtplibmail_host = 'smtp.163.com' #发送邮件服务器mail_user = 'wbhL0478@163.com' # 登录名mail_pwd = 'VPAUITGNHJFNRVLN' # 密码sender = 'wbhL0478@163.com' # 发送人邮箱receivers = ['wbhL0478@163.com'] # 接收人邮箱#邮件内容message = """From: From Person <wbhL0478@163.com>To: To Person <wbhL0478@163.com>Subject: test emailThis is a test e-mail message."""try: smtpObj = smtplib.SMTP(mail_host) #生成一个发送邮件服务器地址的对象 smtpObj.login(mail_user,mail_pwd) #连接上服务器登录 smtpObj.sendmail(sender, receivers, message) #发送邮件 print ("Successfully sent email")except Exception as e: print ("Error: unable to send email")
email_test / email_test
from celery_task.main import send_emailsend_email.delay("wbhL0478@163.com",["wbhL0478@163.com",],"subject","this is content")
email_test / test_celery_email.py
import osimport sysimport timefrom celery import Celery#执行celery命令时,需要进入CELERY_BASE_DIR目录执行#celery项目中的所有导包地址,都是CELERY_BASE_DIR为基础设定# CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__))# sys.path.insert(0,CELERY_BASE_DIR) #增加导包路径#定义celery实例,需要的参数 1,实例名app = Celery('wbh',broker='redis://192.168.56.100:6379/14',backend='redis://192.168.56.100:6379/15')@app.taskdef send_email(sender,receivers,subject,content): import smtplib mail_host = 'smtp.163.com' # 发送邮件服务器 mail_user = 'wbhL0478@163.com' # 登录名 mail_pwd = 'VPAUITGNHJFNRVLN' # 密码 # sender = 'wbhL0478@163.com' # 发送人邮箱 # receivers = ['wbhL0478@163.com'] # 接收人邮箱 # 邮件内容 message = """From: From Person %s To: To Person %s Subject: %s %s """%(sender,receivers[0],subject,content) try: smtpObj = smtplib.SMTP(mail_host) # 生成一个发送邮件服务器地址的对象 smtpObj.login(mail_user, mail_pwd) # 连接上服务器登录 smtpObj.sendmail(sender, receivers, message) # 发送邮件 return "Successfully sent email" except Exception as e: print("Error: unable to send email")
celery_task / main.py
1.3 Celery基本命令
1、启动 Celery celery -A celery_task.main worker -l info2、查看有多少任务在消息队列中 127.0.0.1:6379 > llen celery3、查看worker状态 celery -A main status4、查看有多少任务正在运行 celery -A main inspect active5、查看有多少任务接收了但还未执行 celery -A main inspect reserved6、使用 flower 在线查看 pip install flower celery -A main flower
赞 (0)