我在Google App Engine中使用Python 2.7,似乎无法正确设置我的app.yaml文件。
我的目标是如果我去http://localhost/carlos/
我得到一个执行的carlos.py
这是我的目录结构:
app\
\app.yaml
\main.py
\carlos.py
这是我当前的app.yaml文件:
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /carlos/.*
script: carlos.app
- url: .*
script: main.app
我的carlos.py文件是:
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Hello, Carlos!")
app = webapp2.WSGIApplication([('/carlos', MainHandler)],
debug=True)
但是我现在得到的是404 Not Found错误。有什么想法吗?
答案 0 :(得分:4)
我能够确定解决方案,并认为我会为任何人发布它。
在我的carlos.py文件中,我需要替换:
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
与
app = webapp2.WSGIApplication([('/carlos/', MainHandler)],
debug=True)
似乎WSGIApplication的第一个参数是指根网址的TOTAL路径,而不是最初定向它的INCREMENTAL路径。
我选择Littm提供的答案,因为我想继续使用WSGI
答案 1 :(得分:1)
它使用了以下修改:
1 - 将“carlos.app”替换为“carlos.py”,将“main.app”替换为yaml文件中的“main.py”。
2 - 在“carlos.py”文件中的“/ carlos”之后添加斜杠(“/”)。
3 - 在每个python文件的末尾添加以下代码部分(carlos.py和main.py)
def main():
app.run()
以下是修改文件的示例:
app.yaml:
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: no
handlers:
- url: /carlos/.*
script: carlos.py
- url: .*
script: main.py
carlos.py: import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Hello, Carlos!")
app = webapp2.WSGIApplication([('/carlos/', MainHandler)],
debug=True)
def main():
app.run()
main.py:
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Hello, MAIN!")
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
def main():
app.run()
您可以尝试导航到:
localhost:8080 / carlos /和localhost:8080 /查看结果
希望它有所帮助;)