Python cherrypy,解析query_string

时间:2013-09-08 12:14:32

标签: python parsing cherrypy

我正在尝试从请求中获取查询和数据(GET参数和POST参数)

curl --data "foo=bar&hello=world" "http://localhost:8080/mypath?orange=5&apple=8"

query_string = cherrypy.request.query_string  # 'orange=5&apple=8'
post_data = cherrypy.request.body.params  # {'foo': 'bar', 'hello': 'world'}

post_data正确形成dict。 我怎么能像post_data一样解析query_string?

我正在阅读cherrypy doc,我看到了这个:

  

process_query_string()

     

将查询字符串解析为Python结构。 (核心)

但这不起作用,cherrypy.request.process_query_string() ais返回None

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

CherryPy使用cherrypy.lib.httputil.parse_query_string填充带有GET参数的request.params,您可以像这样使用它:

from cherrypy.lib.httputil import parse_query_string
parse_query_string(cherrypy.request.query_string)

返回带有解析查询字符串参数的dict。

答案 1 :(得分:0)

query = urllib.parse.parse_qs(cherrypy.request.query_string,True)