Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django未正确翻译Bittorrent查询字符串_Python_Django_Encoding_Bittorrent - Fatal编程技术网

Python Django未正确翻译Bittorrent查询字符串

Python Django未正确翻译Bittorrent查询字符串,python,django,encoding,bittorrent,Python,Django,Encoding,Bittorrent,作为一个更大项目的一部分,我正在Django框架之上编写一个小的Bittorrent跟踪器。但是,我在解码公告请求的“info_hash”参数时遇到问题 基本上,uTorrent接受相关torrent的SHA1散列,URL对其十六进制表示进行编码,然后在GET请求中将其作为info_散列参数发送给跟踪器 信息散列 A44B44B0EE8D85A9F7135489D522A19DA2C87C91 获取编码为: %a4KD%b0%ee%8d%85%a9%f7%13T%89%d5%22%a1%9d%

作为一个更大项目的一部分,我正在Django框架之上编写一个小的Bittorrent跟踪器。但是,我在解码公告请求的“info_hash”参数时遇到问题

基本上,uTorrent接受相关torrent的SHA1散列,URL对其十六进制表示进行编码,然后在GET请求中将其作为info_散列参数发送给跟踪器

信息散列

A44B44B0EE8D85A9F7135489D522A19DA2C87C91
获取编码为:

%a4KD%b0%ee%8d%85%a9%f7%13T%89%d5%22%a1%9d%a2%c8%7c%91
但是,Django将其解码为Unicode字符串:

u'\ufffdKD\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\x13T\ufffd\ufffd"\ufffd\ufffd\ufffd\ufffd|\ufffd'
而不是像这样的字符串文字:

\xa4KD\xb0\xee\x8d\x85\xa9\xf7\x13T\x89\xd5"\xa1\x9d\xa2\xc8|\x91
我怎样才能阻止Django尝试将信息散列转换为Unicode,这样我就可以取消引用它了?我的目标是获得一个字符串文本,然后将其编码为十六进制字符串


有什么想法吗?如果我遗漏了一些关于编码的概念,请道歉。谢谢

Django使用默认编码对所有GET数据进行解码。您需要自己获取查询字符串,可能来自
os.environ['query\u string']
request.environ['query\u string']
您的设置是什么。默认编码?还有,哈希在HTTP头中是什么样子的?在编码过程中完全不应修改,如下所示:

>>> import urllib
>>> urllib.urlencode({'hash':"A44B44B0EE8D85A9F7135489D522A19DA2C87C91"})
'hash=A44B44B0EE8D85A9F7135489D522A19DA2C87C91'
自:

>>> urllib.quote('A44B44B0EE8D85A9F7135489D522A19DA2C87C91') == 'A44B44B0EE8D85A9F7135489D522A19DA2C87C91'
True
因此:

>>> urllib.unquote('%a4KD%b0%ee%8d%85%a9%f7%13T%89%d5%22%a1%9d%a2%c8%7c%91') == 'A44B44B0EE8D85A9F7135489D522A19DA2C87C91'
False

uTorrent将特定的散列编码为:“%a4KD%b0%ee%8d%85%a9%f7%13T%89%d5%22%a1%9d%a2%c8%7c%91”我不明白为什么它是这样编码的,但唯一的解码方法(我发现)是通过查询字符串手动进行的。谢谢,我不确定是否有更优雅的方法。