我当时正在研究bittorrent协议,并想尝试一些跟踪器请求,以获取有关对等点和其他方面的信息,但我无法从我尝试过的任何跟踪器中得到任何适当的响应。
这就是我的对白
{'info_hash': '7bf74c4fd609bf288523f7cd51af3bdbc19df610', 'peer_id': '139a3f2ff0143c9f24c19c4f95ed1378aaf449d2', 'port': '6881', 'uploaded': '0', 'downloaded': '0', 'left': '931135488', 'compact': '1', 'no_peer_id': '0', 'event': 'started'}import bencoding
import hashlib
import secrets
import requests
import urllib
file = open('../altlinux.torrent', 'rb')
data = bencoding.bdecode(file.read())
info_hash = hashlib.sha1(bencoding.bencode(data[b'info'])).hexdigest()
params = {
'info_hash': info_hash,
'peer_id': secrets.token_hex(20),
'port': '6881',
'uploaded': '0',
'downloaded': '0',
'left': str(data[b'info'][b'length']),
'compact': '1',
'no_peer_id': '0',
'event': 'started'
}
print(params)
page = requests.get(data[b'announce'], params=params)
print(page.text所以这就是我写的,我也犯了同样的错误,
d14:failure reason50:Torrent is not authorized for use on this tracker.e我甚至尝试过将info_hash编码到
urllib.parse.quote_plus(info_hash)只是为了使它成为六分格式的url编码格式。
我不知道我哪里出了问题有人能帮忙吗?
发布于 2019-10-08 15:04:07
您需要传递原始info_hash,而不是它的编码版本。peer_id也是如此:
params = {
'info_hash': bytes.fromhex(info_hash),
'peer_id': bytes.fromhex(secrets.token_hex(20)),
'port': '6881',
'uploaded': '0',
'downloaded': '0',
'left': str(data[b'info'][b'length']),
'compact': '1',
'no_peer_id': '0',
'event': 'started'
}此外,应该指出,这将只适用于单个文件洪流。当您看到一个包含多个文件的文件时,您获取长度的方式需要处理files元素,因为这些文件没有length条目。
发布于 2019-10-08 23:51:29
info_hash不应该是十六进制表示。应该是二进制表示法。也就是说,您需要对其进行URL编码。也就是说,不要打电话给.hexdigest()。
看起来,当您调用urllib.parse.quote_plus(info_hash)时,您将对信息哈希的十六进制表示进行编码。也就是说,它仍然是十六进制编码。
您可能想使用类似于urllib.parse.quote(info_hash)的东西
https://stackoverflow.com/questions/58282768
复制相似问题