1 Celery 實現簡訊--郵件
1.1 容聯雲-簡訊
-
from ronglian_sms_sdk import SmsSDK accountSid = '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 預設為1 res = sdk.sendMessage('1', '18704992144', ['8888','5']) print(res) # 可參考demo中的介面呼叫檔案:SendTemplateSMS.py。
-
# import sys # sys.path.insert(0,'/root/opwf_project/celery_task') # from main import send_sms from 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
-
import os import sys import time from 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.task def 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
1.2 @163.com-郵件
-
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 <wbhL0478@163.com> To: To Person <wbhL0478@163.com> Subject: test email This 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")
-
from celery_task.main import send_email send_email.delay("wbhL0478@163.com",["wbhL0478@163.com",],"subject","this is content")
-
import os import sys import time from 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.task def 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")
1.3 Celery基本命令
-
1、啟動 Celery celery -A celery_task.main worker -l info 2、檢視有多少任務在訊息佇列中 127.0.0.1:6379 > llen celery 3、檢視worker狀態 celery -A main status 4、檢視有多少任務正在執行 celery -A main inspect active 5、檢視有多少任務接收了但還未執行 celery -A main inspect reserved 6、使用 flower 線上檢視 pip install flower celery -A main flower