在Google Apps Engine中使用Python处理POST请求

0 投票
2 回答
2142 浏览
提问于 2025-04-17 15:03

我在Google Apps Engine上托管我的网站,使用Python,并且我正在尝试处理一个简单的联系表单。

这是我的HTML代码:

<form method="post" action="/email" id="contactForm">  
    <h2>Let's get in touch!</h2>
    Name:<br/>
    <input size=35 name="name" placeholder="Feature coming soon!"><br/>
    Email:<br/>
    <input size=35 name="email" placeholder="Feature coming soon!"><br/>
    Subject:<br/>
    <input size=35 name="subject" placeholder="Feature coming soon!"><br/>
    Message:<br/>
    <textarea name="message" rows=15 cols=50 placeholder="Feature coming soon!"></textarea><br/>
    <input type="submit" name="send" value="Submit">
</form>

在我的app.yaml文件中:

  • url: /email
    script: email.py

这是我的email.py代码:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail

class SendEmail(webapp.RequestHandler):
    def post(self):
        name = self.request.post("name")
        email = self.request.post("email")
        tempSubject = self.request.post("subject")
        msg = self.request.post("body")

        if name is None:
            self.response.out.write("Error: You did not enter a name.")
        elif email is None:
            self.response.out.write("Error: You did not enter an email.")
        elif tempSubject is None:
            self.response.out.write("Error: You did not enter a subject.")
        elif msg is None:
            self.response.out.write("Error: You did not enter a message.")
        else:
            _subject = "Msg from: " + name + "Re: " + tempSubject

            message = mail.EmailMessage(sender = "alexyoung1992@alexyoung.us", to = "alexyoung1992@gmail.com", subject = _subject, body = msg, reply_to = email)
            message.send()

application = webapp.WSGIApplication([('/email', SendEmail)], debug=True)
run_wsgi_app(application)
self.redirect('/')

我遇到了500服务器错误:

错误:服务器错误

服务器遇到错误,无法完成你的请求。如果问题持续存在,请报告你的问题,并提到这个错误信息和导致它的查询。

补充说明:我更新了我的Python脚本,并在本地机器上调试了一切。运行得很顺利,但一旦我尝试在服务器上运行,就又出现了同样的错误。你们也可以试着自己运行这个脚本,看看是否会有不同的结果。

2 个回答

0

原来,服务器没有识别我的一些变量,因为我在某些地方用了空格来缩进,而在其他地方用了制表符(Tab)。下面是最终的代码:

import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail

class SendEmail(webapp.RequestHandler):
    def post(self):
        name = self.request.get('name', '')
        email = self.request.get('email', '')
        tempSubject = self.request.get('subject', '')
        msg = self.request.get('message', '')

        if name is None:
            self.response.out.write("Error: You did not enter a name.")
        elif email is None:
            self.response.out.write("Error: You did not enter an email.")
        elif tempSubject is None:
            self.response.out.write("Error: You did not enter a subject.")
        elif msg is None:
            self.response.out.write("Error: You did not enter a message.")
        else:
            _subject = "Message from: " + name + ", Re: " + tempSubject
            msg += "\n\nI can be reached at "
            msg += email

            message = mail.EmailMessage(sender = "foo@bar.com", to = "bar@foo.com")
            message.subject = _subject
            message.body = msg
            message.send()

            self.redirect('/')

def runApp():
    application = webapp.WSGIApplication([('/email', SendEmail)], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    runApp()
2

在Python中,使用的是“and”,而不是&&。

比如,像这样写:Name != None and Email != None

在本地环境中调试会简单很多,这样可以更清楚地看到错误的来源。

撰写回答