首页 > 解决方案 > 如何将邮递员的 uuid 接收到龙卷风应用程序中?

问题描述

我已经使用 uuid 将我的模型保存在弹性数据库中,我使用该 uuid 从 Postman 调用我的 tornado 应用程序,uuid 有我的模型,我应该如何在我的 requestHandler 的 POST 方法中接收它,下面是代码

class myHandler(RequestHandler):  
    _thread_pool = ThreadPoolExecutor(max_workers=10)

    #My data base url
    def initialize(self): 
        self.db = self.settings['db']

    #POST method to receive data and model
    @gen.coroutine
    def post(self, model_id):
        try:
            data = tornado.escape.json_decode(self.request.body)
            yield self.predict(model_id, self.db, data)
        except Exception:
            self.respond('server_error', 500)

    ###here I have predict methods that receive the model_id and pass###

post方法参数中的model_id是我从邮递员那里收到的uuid

我的应用程序调用看起来像

elastic_url = os.environ.get('ELASTICSEARCH_URL', 'localhost:9200')
define('port', default=8888, help='Tornado port to listen on')

def make_app():  
    url = [(r"/uuid/predict", myHandler)]
    return Application(url, db=elastic_url, debug=True, autoreload=False)

if __name__ == "__main__":
    application = make_app()
    http_server = HTTPServer(application)
    http_server.listen(options.port)
    IOLoop.current().start() 

从 POSTMAN 我将 API 称为

http://127.0.0.1:8888/9cd68748-a3b5-4bc3-994d-16e921103cb2/predict

如果我没有 uuid,我只需使用正则表达式接收如下

url = [(r"/(?P<id>[a-zA-Z0-9_]+)/predict", myHandler)
#From POSTMAN I call as
http://127.0.0.1:8888/model_name/predict

标签: websockettornado

解决方案


匹配 url 和 post 函数中的名称“model_id”

    @gen.coroutine
    def post(self, model_id):
        try:
            data = tornado.escape.json_decode(self.request.body)
            yield self.predict(model_id, self.db, data)
        except Exception:
            self.respond('server_error', 500)

并使用如下网址

url = [(r"/(?P<model_id>[a-zA-Z0-9_.-]+)/predict", myHandler)
#From POSTMAN I call as
http://127.0.0.1:8888/d460e889-6860-4fb4-b040-fee70c96a029.mods/predict

推荐阅读