谷歌应用引擎(python27)获取请求网址

non*_*one -1 python google-app-engine webapp2

我一直在尝试获取请求URL如下

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        print self.request.get('url')

app = webapp2.WSGIApplication([('/.*', MainPage)], debug=True)
Run Code Online (Sandbox Code Playgroud)

当请求是

http://localhost:8080/index.html
Run Code Online (Sandbox Code Playgroud)

它给了我类似的东西

Status: 200 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Content-Length: 70
Run Code Online (Sandbox Code Playgroud)

我需要得到的是类似的东西

index.html
Run Code Online (Sandbox Code Playgroud)

编辑:这样我就可以检查字符串并相应地显示正确的html /模板文件.

我已经检查过Request文档并尝试了很多替代方案但我似乎无法找到解决方案.我对网络开发很陌生.我错过了什么?

asc*_*d00 6

你应该从这里开始:https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld

你没有使用模板或模板模块/引擎,所以它与你正在访问的路径无关,因为你正在映射所有内容 /.*

使用self.response.write()print.
如果您在签出请求类之前从头开始阅读文档,那么对您来说再好一点.

编辑:

如果你想在requesthandler中获取urlpath并根据urlpath提供不同的模板,那么执行以下操作:

def get(self):
    if self.request.path == '/foo':
        # here i write something out
        # but you would serve a template
        self.response.write('urlpath is /foo')
    elif self.request.path == '/bar':
        self.response.write('urlpath is /bar')
    else:
        self.response.write('urlpath is %s' %self.request.path)
Run Code Online (Sandbox Code Playgroud)

更好的方法是拥有多个ReqeustHandler并将它们映射到WSGIApplication:

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/foo', FooHandler),
                               ('/bar', BarHandler),
                               ('/.*', CatchEverythingElseHandler)], debug=True)
Run Code Online (Sandbox Code Playgroud)