google应用程序引擎(python27)获取请求url

google应用程序引擎(python27)获取请求url,python,google-app-engine,webapp2,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) 当请求被拒绝时 http://localhost:8080/index.html 它给我的感觉是 Sta

我一直在尝试获取请求url,如下所示

import webapp2

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

app = webapp2.WSGIApplication([('/.*', MainPage)], debug=True)
当请求被拒绝时

http://localhost:8080/index.html
它给我的感觉是

Status: 200 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Content-Length: 70
index.html
我需要的是

Status: 200 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Content-Length: 70
index.html
编辑:以便检查字符串并相应地显示正确的html/模板文件

我已经检查并尝试了许多替代方案,但似乎找不到解决方案。我对网络开发还很陌生。我遗漏了什么?

你应该从这里开始:

您没有使用模板或模板模块/引擎,因此它与您访问的路径无关,因为您正在使用
/.*

使用
self.response.write()
not
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)
更好的方法是使用多个RequestHandler并将它们映射到
WSGIApplication

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/foo', FooHandler),
                               ('/bar', BarHandler),
                               ('/.*', CatchEverythingElseHandler)], debug=True)
而不是:

self.request.get('url')
使用:

您可能会发现其他有用的选项有:

self.request.path
self.request.referer
尝试这些更改以获得所需的结果。

您也可以使用此选项:

def get(self):
     self.request.GET['name']
     self.request.GET['sender']

我已经阅读了指南,但是那里没有与我的问题相关的内容。在我的玩具示例中,我访问的路径与此无关,但最终我计划根据请求的url使用模板或静态html页面。我认为你的问题真的不清楚。你说的
是什么意思?我需要的是index.html之类的东西?是否希望页面呈现模板?您想在页面中写入
index.html
?我建议你更新你的问题,特别是问你需要做什么,而不是“玩具例子”。看看这里,我告诉你我已经读过几遍了。该示例中只有一个模板。如果我有
index1.html
index2.html
并且我需要根据请求的url决定显示哪一个呢?如果您想要index1.html或index2.html输出,那么为什么要打印请求。您应该按照前面的人建议的文档检查请求对象。具体看一下如何与请求对象交互。从请求中获得url后,您可以确定要加载的模板等。当前获得的输出是打印请求或调用repr(请求)时获得的输出。