V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
zeyexe
V2EX  ›  Tornado

Tornado 有没有简单的办法一次性取到所有参数

  •  
  •   zeyexe · 2013-07-19 12:18:11 +08:00 · 15213 次点击
    这是一个创建于 3905 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://127.0.0.1/?a=1&b=2&c=3&d=4&e=5&f=6&g=7

    像上面那样,我现在想把 a b c d e f g 这些参数一次性取到,但是通过 get_argument 方法每次只能取一个参数,大家有没有什么简单的办法把所有参数全部取到?
    10 条回复    2016-09-09 12:47:20 +08:00
    talentsnail
        1
    talentsnail  
       2013-07-19 12:19:22 +08:00   ❤️ 1
    get_arguments()
    zeyexe
        2
    zeyexe  
    OP
       2013-07-19 12:31:35 +08:00
    @talentsnail 这个方法实际上是获取某个参数的全部值。是用在 http://127.0.0.1/?a=1&a=2&a=3 这种情况下的。
    jkeylu
        3
    jkeylu  
       2013-07-19 13:01:27 +08:00   ❤️ 2
    self.request.arguments
    kernel1983
        4
    kernel1983  
       2013-07-19 13:12:54 +08:00   ❤️ 1
    http://www.tornadoweb.org/en/stable/httpserver.html

    method
    HTTP request method, e.g. “GET” or “POST”

    uri
    The requested uri.

    path
    The path portion of uri

    query
    The query portion of uri

    version
    HTTP version specified in request, e.g. “HTTP/1.1”


    tornado的文档是比较吊, 基本上无视初学者的感受
    zeyexe
        5
    zeyexe  
    OP
       2013-07-19 13:36:56 +08:00
    @jkeylu
    @kernel1983

    倒是忘了最基本的东西。
    hiwljun
        6
    hiwljun  
       2013-07-19 13:58:48 +08:00   ❤️ 1
    web.py的web.input()非常好用,tornado里我的做法是这样的:
    在BaseHandler加个input函数获取所有的值并转换成storage:
    from Storage import storage

    def input(elf):
    i = storage()
    args = self.request.arguments
    for a in args:
    i[a] = self.get_argument(a)
    return i

    i["files"], i["path"]这些再另作处理下。就可以用self.input()获取所有GET和POST的值了。
    zeyexe
        7
    zeyexe  
    OP
       2013-07-19 14:28:06 +08:00
    from Storage import storage

    这是 web.py 的模块吗?
    hiwljun
        8
    hiwljun  
       2013-07-19 15:14:35 +08:00
    @zeyexe 是的。
    __author__ = 'Aaron Swartz'
    class Storage(dict):
    """
    A Storage object is like a dictionary except `obj.foo` can be used
    in addition to `obj['foo']`.

    >>> o = storage(a=1)
    >>> o.a
    1
    >>> o['a']
    1
    >>> o.a = 2
    >>> o['a']
    2
    >>> del o.a
    >>> o.a
    Traceback (most recent call last):
    ...
    AttributeError: 'a'

    """
    def __getattr__(self, key):
    try:
    return self[key]
    except KeyError, k:
    raise AttributeError, k

    def __setattr__(self, key, value):
    self[key] = value

    def __delattr__(self, key):
    try:
    del self[key]
    except KeyError, k:
    raise AttributeError, k

    def __repr__(self):
    return '<Storage ' + dict.__repr__(self) + '>'

    storage = Storage
    lq611
        9
    lq611  
       2013-08-20 10:39:42 +08:00
    self.request.arguments
    bwangel
        10
    bwangel  
       2016-09-09 12:47:20 +08:00   ❤️ 1
    刚刚看 tornado 源码,突然想起这个帖子来了。

    我现在看的 tornado 版本是 4.4.1

    在 HTTPServerRequest 对象(也就是 get 方法中的 self.request 对象)中,有三个属性

    `arguments`: 所有的 post/get 参数字典
    `query_arguments`: 所有的 get 参数字典
    `body_arguments`: 所有的 post 参数字典

    现在文档中也有写了:

    http://www.tornadoweb.org/en/stable/httputil.html#tornado.httputil.HTTPServerRequest.query_arguments
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1742 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:37 · PVG 00:37 · LAX 09:37 · JFK 12:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.