Celery--短信與郵件

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。
    sms_test / sms.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就怎麼啓動
    
    #一、celery -A celery_task.main worker -l info => from celery_task.main import send_sms
    #二、celery -A main worker -l info => from main import send_sms
    sms_test / test_celery_sms.py
  • 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
    celery_task / main.py

     

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")
    email_test / email_test
  • from celery_task.main import send_email
    
    
    send_email.delay("wbhL0478@163.com",["wbhL0478@163.com",],"subject","this is content")
    email_test / test_celery_email.py
  • 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")
    celery_task / main.py

 

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
相關文章
相關標籤/搜索