15

urlparse.parse_qs对于解析 url 参数很有用,它适用于简单的 ASCII url,由str. 所以我可以解析一个查询,然后使用urllib.urlencode解析的数据构造相同的路径:

>>> import urlparse
>>> import urllib
>>>
>>> path = '/?key=value' #path is str
>>> query = urlparse.urlparse(path).query
>>> query
'key=value'
>>> query_dict = urlparse.parse_qs(query)
>>> query_dict
{'key': ['value']}
>>> '/?' + urllib.urlencode(query_dict, doseq=True)
'/?key=value' # <-- path is the same here

当 url 包含百分比编码的非 ASCII 参数时,它也可以正常工作:

>>> value = urllib.quote(u'значение'.encode('utf8'))
>>> value
'%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
>>> path = '/?key=%s' % value
>>> path
'/?key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
>>> query = urlparse.urlparse(path).query
>>> query
'key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
>>> query_dict = urlparse.parse_qs(query)
>>> query_dict
{'key': ['\xd0\xb7\xd0\xbd\xd0\xb0\xd1\x87\xd0\xb5\xd0\xbd\xd0\xb8\xd0\xb5']}

>>> '/?' + urllib.urlencode(query_dict, doseq=True)
'/?key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'  # <-- path is the same here

但是,当使用 django 时,我使用 获取 url request.get_full_path(),并将路径作为unicode字符串返回:

>>> path = request.get_full_path()
>>> path
u'/?key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5' # path is unicode

看看现在会发生什么:

>>> query = urlparse.urlparse(path).query
>>> query
u'key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
>>> query_dict = urlparse.parse_qs(query)
>>> query_dict
{u'key': [u'\xd0\xb7\xd0\xbd\xd0\xb0\xd1\x87\xd0\xb5\xd0\xbd\xd0\xb8\xd0\xb5']}
>>>

query_dict包含 unicode 字符串,其中包含字节!不是unicode点!当然,在尝试对该字符串进行 urlencode 时,我遇到了 UnicodeEncodeError:

>>> urllib.urlencode(query_dict, doseq=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\Lib\urllib.py", line 1337, in urlencode
    l.append(k + '=' + quote_plus(str(elt)))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-15: ordinal not in range(128)

目前我有一个解决方案:

# just convert path, returned by request.get_full_path(), to `str` explicitly:
path = str(request.get_full_path())

所以问题是:

  • 为什么 parse_qs 返回如此奇怪的字符串(unicode,包含字节)?
  • 将url转换为str安全吗?
4

1 回答 1

17

在将其传递给之前.parse_qs(),使用 ASCII编码回字节:

query_dict = urlparse.parse_qs(query.encode('ASCII'))

str()与显式编码的作用相同。是的,这是安全的,URL 编码使用 ASCII 码点。

parse_qs收到了一个 Unicode 值,所以它也返回了一个 Unicode 值;解码字节不是它的工作。

于 2013-05-17T17:24:00.853 回答