python Tornado获取请求url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16637735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:14:01  来源:igfitidea点击:

python tornado get request url

pythonurlgettornado

提问by Lee_Prison

Here is my code:

这是我的代码:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(self.request.url)

def main():
    settings = {"template_path": "html","static_path": "static"}
    tornado.options.parse_command_line()
    application = tornado.web.Application([
       (r"/story/page1", MainHandler),
        ],**settings)

I want to get the string "/story/page1". how ?

我想得到字符串“/story/page1”。如何 ?

回答by stalk

You can get current url inside RequestHandlerusing self.request.uri:

您可以RequestHandler使用self.request.uri以下方法获取当前网址:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(self.request.uri)

回答by Kemal Ahmed

I think what you're looking for is self.request.path. Look at the functions available for HTTPServerRequest.

我想你要找的是self.request.path. 查看可用于HTTPServerRequest.

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write(self.request.path)

回答by Mike N

There is no method that gives you access to the full URL, but you can construct it using the protocol, host and uri, e.g.

没有任何方法可以让您访问完整的 URL,但您可以使用协议、主机和 uri 构建它,例如

url = '{}://{}{}'.format(self.request.protocol,self.request.host,self.request.uri)

回答by Andreas Gajdosik

Use self.request.full_url()if you want to access the whole URL of the request (protocol + subdomain + domain + path + query). It will return, for example: https://myserver.com/profiles?id=115.

使用self.request.full_url()如果您要访问的请求(协议+子域名+域名+路径+查询)的整个URL。它将返回,例如:https://myserver.com/profiles?id=115

Use self.request.uriif you want to access URI (path + query). It will give, for example: /profiles?id=115.

使用self.request.uri如果您要访问的URI(路径+查询)。它将给,例如:/profiles?id=115

Use self.request.pathif you want to access just the path without query string. It will give, for example: /profiles.

self.request.path如果您只想访问没有查询字符串的路径,请使用。它将给,例如:/profiles