是否有处理分号的parse_qs的替代方案? [英] Is there an alternative to parse_qs that handles semi-colons?

查看:240
本文介绍了是否有处理分号的parse_qs的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些库/调用可用于处理包含与冒号不同的分号的查询字符串?

What libraries/calls are available to handle query strings containing semi-colons differently than parse_qs?

>>> urlparse.parse_qs("tagged=python;ruby")
>>> {'tagged': ['python']}



完整背景



我正在使用StackExchange API搜索标记的问题。

Full Background

I'm working with the StackExchange API to search for tagged questions.

搜索的布局如此,标签用分号分隔:

Search is laid out like so, with tags separated by semi-colons:

/2.1/search?order=desc&sort=activity&tagged=python ; ruby​​& site = stackoverflow

与API交互就好了。当我想测试呼叫时会出现问题,特别是在使用 httpretty 来模拟HTTP时。

Interacting with the API is just fine. The problem comes in when I want to test the calls, particularly when using httpretty to mock HTTP.

引人注目的是, httpretty 正在使用python标准库中的 urlparse.parse_qs 进行解析查询字符串。

Under the hood, httpretty is using urlparse.parse_qs from the python standard libraries to parse the querystring.

>>> urlparse.parse_qs("tagged=python;ruby")
{'tagged': ['python']}

显然,这种方法效果不佳。这是一个小例子,这里是一段httpretty(在测试环境之外)。

Clearly that doesn't work well. That's the small example, here's a snippet of httpretty (outside of testing context).

import requests
import httpretty

httpretty.enable()

httpretty.register_uri(httpretty.GET, "https://api.stackexchange.com/2.1/search", body='{"items":[]}')
resp = requests.get("https://api.stackexchange.com/2.1/search", params={"tagged":"python;ruby"})
httpretty_request = httpretty.last_request()
print(httpretty_request.querystring)

httpretty.disable()
httpretty.reset()

我想使用httpretty中的机器,但需要 parse_qs 的解决方法。我现在可以修补httpretty,但是很想知道还能做些什么。

I want to use the machinery from httpretty, but need a workaround for parse_qs. I can monkey patch httpretty for now, but would love to see what else can be done.

推荐答案

为了解决这个问题,我暂时猴子补丁 httpretty.core.unquote_utf8 (技术上 httpretty.compat.unquote_utf8 )。

To get around this, I temporarily monkey patched httpretty.core.unquote_utf8 (technically httpretty.compat.unquote_utf8).

#
# To get around how parse_qs works (urlparse, under the hood of
# httpretty), we'll leave the semi colon quoted.
# 
# See https://github.com/gabrielfalcao/HTTPretty/issues/134
orig_unquote = httpretty.core.unquote_utf8
httpretty.core.unquote_utf8 = (lambda x: x)

# It should handle tags as a list
httpretty.register_uri(httpretty.GET,
                       "https://api.stackexchange.com/2.1/search",
                       body=param_check_callback({'tagged': 'python;dog'}))
search_questions(since=since, tags=["python", "dog"], site="pets")

...

# Back to normal for the rest
httpretty.core.unquote_utf8 = orig_unquote
# Test the test by making sure this is back to normal
assert httpretty.core.unquote_utf8("%3B") == ";"

这假设你不需要任何其他的东西。另一个选择是在分数达到 parse_qs 之前只留下百分号编码。

This assumes you don't need anything else unquoted. Another option is to only leave the semi-colons percent-encoded before it reaches parse_qs.

这篇关于是否有处理分号的parse_qs的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆