解码网址参数 [英] De-encode URL parameters

查看:122
本文介绍了解码网址参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一个以前向我发送HTTP字符串的服务器进行交谈:

  / path / to / my / handler /?action-query& id = 112& type = vca& info = ch = 0& type = event& ev16 [sts = begin(...)

所以infoGET参数包括=和&字符。这是非常正统的,但是我们为此写了一个解析器。然而,最近他们决定编码它的一部分,所以现在的字符串看起来像这样..

  / path / to /我的/ handler /?action = query& id = 112& type = vca& info = ch%3D0%26type%3Devent%26ev46 [sts%3Dbegin(...)
pre>

这打破了我们的解析器,期望像第一个这样的字符串。



我可以以某种方式解码字符串,以便我可以使用旧的代码(以便在重新编写解析器时不会损坏) / p>

根据下面的回答,我们可以使用urllib.unquote()来清理字符串。但是,我们依赖于request.GET,它基于第一个字符串设置。是否可以根据新的转换字符串重构GET对象,或以某种方式强制重新计算?

解决方案

怀疑你想要的是 unquote 函数从 urllib 模块。

 >>> s ='/ path / to / my / handler /?action = query& id = 112& type = vca& info = ch%3D0%26type%3Devent%26ev46 [sts%3Dbegin'
>> ; import urllib
>>> urllib.unquote
'/ path / to / my / handler /?action = query& id = 112& type = vca& info = ch = 0& type = event& ev46 [sts = begin'

编辑:我不太熟悉Django,但是其文档的请求和响应对象部分说明如下:


QueryDict实例是不可变的,除非你创建一个copy()。这意味着您不能直接更改request.POST和request.GET的属性。


根据我对这些文档的有限读取,您可能可以将 unquote()函数应用于 HttpRequest.body 属性并构建新的 QueryDict 结果(如有必要,可能会使用它来更新当前的)。


I am talking to a server that used to send me HTTP strings like this:

/path/to/my/handler/?action-query&id=112&type=vca&info=ch=0&type=event&ev16[sts=begin (...)

So the "info" GET parameter included "=" and "&" characters. It was rather unorthodox but nevertheless we wrote a parser for it. However, recently they have decided to encode part of it, so now the string looks like this..

/path/to/my/handler/?action=query&id=112&type=vca&info=ch%3D0%26type%3Devent%26ev46[sts%3Dbegin (...)

This breaks our parser, which expects a string like the first one.

Can I somehow "de-encode" the string, so that I can use the old code (so that it's not broken as we re-write the parser)?

As per answer below, we can use urllib.unquote() to clean the string up. However, we are relying on request.GET, which gets set up based on the first string. Is it possible to reconstruct the GET object based on the new converted string, or somehow force it to re-evaluate?

解决方案

I suspect what you want is the unquote function from the urllib module.

>>> s = '/path/to/my/handler/?action=query&id=112&type=vca&info=ch%3D0%26type%3Devent%26ev46[sts%3Dbegin'
>>> import urllib
>>> urllib.unquote(s)
'/path/to/my/handler/?action=query&id=112&type=vca&info=ch=0&type=event&ev46[sts=begin'

Edit: I'm not very familiar with Django, but the Request and response object section of their docs states the following:

QueryDict instances are immutable, unless you create a copy() of them. That means you can't change attributes of request.POST and request.GET directly.

Based on my limited reading of those docs, you might be able to apply the unquote() function to the HttpRequest.body attribute and build a new QueryDict out of the results (and possibly use it to update your current one if necessary).

这篇关于解码网址参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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