简体   繁体   English

在python Tornado服务器上处理许多路径的最佳方法是什么?

[英]What's the best way to handle many paths on a python Tornado server?

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
        (r"/", MainHandler),
        (r"/auth/login", AuthLoginHandler),
        (r"/auth/logout", AuthLogoutHandler),
        (r"/auth/register",RegisterHandler),
        (r'/user/([a-z\d.]{5,})/?',UserHandler),
        (r'/user/([a-z\d.]{5,})/friends',UserFriendHandler),
        (r'/user/([a-z\d.]{5,})/status',StatusHandler),
        (r'/user/([a-z\d.]{5,})/wall',WallHandler),
        (r'/actions/respond_friend',FriendActionHandler),
        ]

This is my handlers array. 这是我的处理程序数组。 This is likely to grow even more. 这可能会增长更多。 My question involves the line: 我的问题涉及这一行:

    (r'/actions/respond_friend',FriendActionHandler),

I plan on using this line to respond to friend requests via post, with the friends username as a post parameter. 我计划使用此行通过post来响应朋友的请求,并将friends用户名作为post参数。 How would I retrieve this parameter when I send the post (where is it "stored")? 发送帖子时(在“存储”位置)如何检索此参数? Does it make more sense to do this: 这样做是否更有意义:

(r'/user/([a-z\d.]{5,})/actions/respond_friend',FriendActionHandler),

You can use get_argument to get a request param inside of your post method 您可以使用get_argument在post方法内部获取请求参数

username = self.get_argument('username', None)

I don't know if it makes sense to pass the username through the url. 我不知道通过网址传递用户名是否有意义。 If you are it would be duplicating it to post the username as well. 如果您愿意的话,它也会复制它来发布用户名。

If a url param doesn't match the regex I believe tornado just raises a 404. So if you need more fine grained control over what errors are raised, it could make more sense to just post the username in the post body 如果url参数与正则表达式不匹配,我相信龙卷风只会引发404。因此,如果您需要对引发的错误进行更细粒度的控制,将用户名发布在发布正文中可能更有意义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM