[Python] tornado 获取url中的变量

wuchangjian2021-11-12 17:34:45编程学习

例如
url = “http://172.31.162.43:9189/test/nnnn”
url = “http://172.31.162.43:9189/test/bbb”
url = “http://172.31.162.43:9189/test/aaa”
现在要获取 test/后面的变量

路由的写法

正则匹配

('/test/([^/]+)', IndexHandler)

匹配上的字符串会自动传到get/post中的参数中

server

from abc import ABC

from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
from tornado.web import Application, RequestHandler

class IndexHandler(RequestHandler, ABC):
    def post(self, name):
        print("name", name)
        self.write("ok")


define('port', type=int, default=9189, multiple=False)

url_list = [('/test/([^/]+)', IndexHandler)]
app = Application(url_list)
server = HTTPServer(app)
server.listen(options.port)
IOLoop.current().start()

client

import requests


url = "http://172.31.162.43:9189/test/nnnn"
post = requests.post(url=url, data={"test_name": "tessss"})
print(post.text)

相关文章

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。