簡體   English   中英

代碼可在localhost上運行,但在使用Google App Engine進行部署時無法運行

[英]Code working on localhost, but not working when deployed using Google App Engine

我寫了一個驗證表單的代碼。 它在我的本地主機上完美運行。 但是,當我將其部署到Internet上時,會出現錯誤。 這是完整的代碼以及與之相關的錯誤:

import webapp2
from calendar import *
import cgi

form = """
<form method = "post">
<label>Month<input type = "text" name = "mon" value = "%(month)s"></label>
<label>Day<input type = "text" name = "dy" value = "%(day)s"></label>
<label>Year<input type = "text" name = "yr" value = "%(year)s"></label>
<div style="color:red">%(error)s</div>
<br>
<br>
<input type = "submit">
</form>
"""

def escape_html(s):
    return cgi.escape(s, quote = True)
class MainHandler(webapp2.RequestHandler):
    error = ""
    def write_form(self, error, month, day, year):
        self.response.write(form %{"error":error, "month":escape_html(month), "day":escape_html(day), "year":escape_html(year)})
    def get(self):
    #self.response.headers['Content-Type'] = 'text/plain'
        self.write_form("", "", "", "")
    def post(self):
        mon = valid_month(self.request.get("mon"))  
        dy = valid_day(self.request.get("dy"))  
        yr = valid_year(self.request.get("yr"))
        month = self.request.get("mon")
        day = self.request.get("dy")
        year = self.request.get("yr")
        if not(mon and dy and yr):
            self.write_form("Please refill the form with correct data!", month, day, year)
        else:
            self.redirect("/thanks")

class ThanksHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write("Thanks for submitting your data!") 

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

calendar.py

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

def valid_month(month):
    temp = month.capitalize()
    for e in months:
        if e == temp:
            return e
    return None     

def valid_day(day):
    try:
        num = int(day)
    except:
        return None
    if num<=31 and num>0:
        return num
    else:
        return None

def valid_year(year):
    if year and year.isdigit():
        num = int(year)
        if num>1900 and num<2020:
            return num

這是我在互聯網上上傳后收到的錯誤:

內部服務器錯誤

服務器已出錯或無法執行請求的操作。

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~deploymentapp/1.370308128873516940/main.py", line 27, in post
    mon = valid_month(self.request.get("mon"))
NameError: global name 'valid_month' is not defined

誰能幫我這個? 我現在堅持了三天。 無論我嘗試如何處理代碼,我仍然無法使其在互聯網上運行。 提前致謝!

不熟悉GAE,但可能與導入包的順序有關。

嘗試改變

from calendar import *

from .calendar import *

在第一種情況下,它可能正在導入系統日歷包。 在第二種情況下,您要求它導入本地包(您的calendar.py)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM