Azure Python函数错误--grpcMaxMessageLength 2147483647

问题描述

我正在编写一个Azure python函数,该函数可以读取电子邮件并将其写入Azure SQL数据库。该代码可以正常运行,直到处理带有正文的电子邮件为止。然后程序停止并返回消息:

Starting worker process:py  "C:\ProgramData\chocolatey\lib\azure-functions-core-tools-3\tools\workers\python\3.8/WINDOWS/X64/worker.py" --host 127.0.0.1 --port 65101 --workerId 773c7b91-5c39-4d22-8509-2baea30124bb --requestId 0260fedc-f842-4dba-a2e9-6c7630f7228a --grpcMaxMessageLength 2147483647

Azure python函数会挂在电子邮件类型上并停止工作。我尝试构建 Try and Exception with Continue ,以确保它提取下一封电子邮件以及对email_body长度的检查。但是,这些变通办法都不起作用。

我的问题是我如何解决/跳过此问题,以便它也可以处理/识别在电子邮件正文中具有图片的电子邮件。

ReadEmail.py

   import logging
import email
import imaplib
import pyodbc
from datetime import datetime
from raFunctions import uitvullenDatum

# def readEmail(EMAIL_ADRES,EMAIL_PASSWORD,EMAIL_SERVER,DB_SERVER,DB_DATABASE,DB_USER,DB_PASSWORD):
logging.info('ReadEmail start')
# logging.info('ReadEmail pars:' + EMAIL_ADRES  + ' ' + EMAIL_PASSWORD + ' ' + EMAIL_SERVER  + ' ' + DB_SERVER  + ' ' + DB_DATABASE  + ' ' +  DB_USER + ' ' + DB_PASSWORD )

#Configurations
EMAIL = 'xxxxxxx'
PASSWORD = 'xxxxxx'
SERVER = 'xxxxxx'

server = 'xxxxxx'
database = 'xxxxx'
username = 'xxxx'
password = 'xxxxx'
driver= '{ODBC Driver 17 for SQL Server}'

# Connection settings to Database and Email Server
mail = imaplib.IMAP4_SSL(SERVER)
mail.login(EMAIL,PASSWORD)
# cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password +';Authentication=ActiveDirectoryPassword')
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password +'; autocommit=True' )
cursor = cnxn.cursor()
cursor_max_mail_date = cnxn.cursor()
max_mail_date = ""

try:
    _sql_max_datum = "select format(max(o.ReceivingDate),'dd-MMM-yyyy')  from wp_ra.ontvangenmail o "
    cursor_max_mail_date.execute(_sql_max_datum)
    rows  = cursor_max_mail_date.fetchall()
    for row in rows:
        datum = str(row[0])
    max_mail_date = datum
except Exception as e: 
    print('Max_Mail_Date Error:')
    logging.error(str(e))   

    
try:
    mail.select('inbox')
    # status,data = mail.search(None,'ALL')
    logging.info(max_mail_date)
    status,'(SINCE "19-May-2020")')   
    mail_ids = []
    for block in data:
        try:       
            mail_ids += block.split()
        except:
            print('Error in block in data')
            continue 

    for i in mail_ids:
        try:
            status,data = mail.fetch(i,'(RFC822)')
            for response_part in data:
                if isinstance(response_part,tuple):
                    try:
                        message = email.message_from_bytes(response_part[1])
                        mail_from = message['From']
                        mail_subject = message['Subject']          
                        mail_date = message['Date']

                        print('mail_date: ' + mail_date + 'mail_subject: ' + mail_subject)

                        if message.is_multipart():
                            mail_content = ''

                            for part in message.get_payload():

                                if part.get_content_type() == 'text/plain':
                                    mail_content += part.get_payload()
                                
                                else:
                                    mail_content = part.get_payload()   
                                    
                        else:
                            mail_content = message.get_payload()
                        
                        maildate = email.utils.parsedate(mail_date)
                        maildate = uitvullenDatum(str(maildate[0]),str(maildate[1]),str(maildate[2]),str(maildate[3]),str(maildate[4]))
            
                        print("Check Mail Content")
                        
                        if(len(mail_content) >= 1147483647 ):
                            print("Niet in te lezen email:  maildate: " + str(mail_date) +  ' mail subject: ' + mail_subject )
                        
                        else:
                            values = (maildate,mail_from,mail_subject,mail_content)
                            sql = "EXEC wp_ra.invoerenmail ?,?,?"
                            cursor.execute(sql,(values))
                            cursor.commit()
                            logging.info("Data saved on the database")
                    except Exception as e:
                        print('Error reading mail:')
                        print(str(e))
                        continue
        except:
            print('Error i in mail_ids')
            continue
        

except:
    print("Fout bij het inlezen van de mail!")
    continue

解决方法

如果您停止在某些电子邮件中,则可以尝试在输出当前异常后使用“继续”来跳过它以处理其他电子邮件:

for i in s :
    #Some logic here
    try:
        #Do something here
    except:
        print('print something in this place.')
        continue

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...