龙卷风不解释URL

Wil*_*uks 0 python regex url tornado

我一直在尝试使用Tornado Framework创建一个Web服务.该系统应该处理如下URL:

IP:Port/?user=1822&catid=48&skus=AB1,FS35S,98KSU1

首先我创建了这段代码来阅读网址:

#!/usr/bin/env python

from datetime import date
import tornado.httpserver
import tornado.escape
import tornado.ioloop
import tornado.web

class WService(tornado.web.RequestHandler):
    def get(self, url):
        self.write("value of url: %s" %(url))

application = tornado.web.Application([
    (r"/([^/]+)", WService)])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(9000)
    tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)

并输入网址:

IP:Port/hello_world

导致:

value of url: hello_world

除了"?"之外,URL中使用的任何字符都有效.更改代码时,例如:

application = tornado.web.Application([
        (r"/?([^/]+)", WService)])
Run Code Online (Sandbox Code Playgroud)

并将网址发送到"?" mark(IP:Port/?hello_world)结果是:

404: Not Found

研究龙卷风来解决这个问题我发现了这个get_argument方法并尝试应用它,例如:

class WService2(tornado.web.RequestHandler):
    def get(self):
        user = self.get_argument('user', None)   
        respose = { 'user': user }   
        self.write(response)
Run Code Online (Sandbox Code Playgroud)

application = tornado.web.Application([
    (r"/", WService2),   
])
Run Code Online (Sandbox Code Playgroud)

但发送的URL IP:Port/user=5返回:

404:未找到

我也尝试过:

application = tornado.web.Application([
    (r"/(\w+)", WService2),   
])
Run Code Online (Sandbox Code Playgroud)

并且:

application = tornado.web.Application([
    (r"/([^/]+)", WService2),   
])
Run Code Online (Sandbox Code Playgroud)

没有任何效果.

我是否做错了,用"?"读取URL 标记并且有没有理由说Tornado没有读取带有参数的URL user?有什么遗失的吗?

我已经将Tornado更新为最新版本,但它没有用.

如果您需要更多信息或有些不清楚的地方请告诉我.

提前致谢,

Rod*_*yde 5

您没有看到整个字符串的原因是Tornado已经解析了它并将其放入请求对象中.如果要查看包含查询字符串的整个路径,请查看request.uri.

在你的例子中,如果你要去,http://localhost:9000/hello_world?x=10&y=33那么request.uri将被设置为/hello_world?x=10&y=33.

但是,正如您所说,最好使用get_argument(或get_arguments)来检查参数.这是你的例子,增加了以展示你如何阅读它们.

试着去http://localhost:9000/distance?x1=0&y1=0&x2=100&y2=100看看你得到了什么.

同样,试试吧http://localhost:9000/?user=1822&catid=48&skus=AB1,FS35S,98KSU1.这应该按照您的预期工作.

#!/usr/bin/env python

import math
import tornado.httpserver
import tornado.escape
import tornado.ioloop
import tornado.web


class DistanceService(tornado.web.RequestHandler):
    def get(self):
        x1 = float(self.get_argument('x1'))
        y1 = float(self.get_argument('y1'))
        x2 = float(self.get_argument('x2'))
        y2 = float(self.get_argument('y2'))
        dx = x1 - x2
        dy = y1 - y2
        magnitude = math.sqrt(dx * dx + dy * dy)
        self.write("the distance between the points is %3.2f" % magnitude)


class WService(tornado.web.RequestHandler):
    def get(self):
        self.write("value of request.uri: %s" % self.request.uri)
        self.write("<br>")
        self.write("value of request.path: %s" % self.request.path)
        self.write("<br>")
        self.write("value of request.query: %s" % self.request.query)


application = tornado.web.Application([
    (r"/distance", DistanceService),
    (r"/", WService),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(9000)
    tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)