如何将代码添加到Google AppEngine的Webapp 2框架中

时间:2016-03-08 11:46:38

标签: python google-app-engine webapp2

很抱歉,如果这似乎是一个非常愚蠢的问题,但我并非严格来自编程背景,而且我在使用Google AppEngine的webapp2框架来整合我的代码时遇到了很多困难。

我的代码用于登录我自己的电子邮件帐户并获取最新的电子邮件,然后返回内容和发件人等字段。我测试了这段代码,它工作正常。但是在使用webapp 2框架时我遇到了问题(错误500)。

有人可以告诉我如何在这种情况下使用webapp2框架吗?

我的python代码:

import easyimap

host = "imap.gmail.com"
user = my_username
password = my_password
mailbox = "inbox"
imapper = easyimap.connect(host, user, password, mailbox)

mail1, mail2 = imapper.listup(2) # take latest 2 emails
mail1.uid #uid is email id
email_body = mail1.body
email_sender = mail1.from_addr
email_id = mail1.uid
email_receipt = mail1.to
email_cc = mail1.cc

email_list = [email_body, email_title,email_sender, email_id,
        email_cc,email_receipt]
print(email_list)

Webapp2 main.py

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello Natalie!')

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

1 个答案:

答案 0 :(得分:0)

类似的东西:

# File: mailer.py - next to "main.py"
import easyimap
import webapp2

class MailerHandler(webapp2.RequestHandler):
    def get(self):
        host = "imap.gmail.com"
        user = my_username
        password = my_password
        mailbox = "inbox"
        imapper = easyimap.connect(host, user, password, mailbox)

        mail1, mail2 = imapper.listup(2) # take latest 2 emails
        mail1.uid #uid is email id
        email_body = mail1.body
        email_sender = mail1.from_addr
        email_id = mail1.uid
        email_receipt = mail1.to
        email_cc = mail1.cc

        email_list = [email_body, email_title,email_sender, email_id,
                email_cc, email_receipt]
        self.response.write(', '.join(email_list))

和main.py:

import MailerHandler from mailer
import webapp2

app = webapp2.WSGIApplication([
    ('/mail-handler', MailerHandler)
], debug=True)

然后在浏览器http://[SERVER:PORT]/mail-handler

中调用

如果您收到错误500,则需要查看登录控制台并询问具体问题。

确保您已经复制了" easyimap"到项目文件夹(main.py旁边),并且该库不使用套接字,并且是用纯Python编写的。阅读本节有关添加3d方库的信息:https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring