在webapp2中的requesthandler中使用url编码

时间:2017-05-29 11:36:35

标签: python-2.7 webapp2

这是我到目前为止尝试的代码。

class MainPage(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

class Data(webapp2.RequestHandler):

    def get(self, url):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(url)


app = webapp2.WSGIApplication([
    (r'/', MainPage),
    (r'/data/(.*$)', Data),
], debug=True)

如果我用chrome打开这个网址:

http://127.0.0.1:8080/data/https://www.google.co.in/?gfe_rd=cr&ei=JAYsWerYJ8b08weOpqqAAQ&gws_rd=ssl;

我得https:/www.google.co.in/ 代替: https://www.google.co.in/?gfe_rd=cr&ei=JAYsWerYJ8b08weOpqqAAQ&gws_rd=ssl

我知道这一定是显而易见的,但我无法找到如何使其发挥作用。

1 个答案:

答案 0 :(得分:1)

你可以对网址进行编码和解码然后我会建议像http://127.0.0.1:8080/data/https:%2F%2Fwww.google.co.in%2F%3Fgfe_rd=cr&ei=JAYsWerYJ8b08weOpqqAAQ&gws_rd=ssl这样的内容,而不是打印完整的网址,如果你这样编码的话。

import urllib

class Data(webapp2.RequestHandler):

    def get(self, url):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(urllib.unquote(url))

如果可以的话,使用编码的网址,然后使用urrllib取消引用似乎可以解决问题。