【问题标题】:How to send requests to bittorrent tracker using python requests如何使用 python 请求向 bittorrent 跟踪器发送请求
【发布时间】:2020-02-05 12:42:05
【问题描述】:

我正在研究 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)

只是为了使它成为 hexdigest 的 url 编码格式

我不确定我哪里出错了 有人可以帮忙吗?

【问题讨论】:

    标签: python-requests bittorrent hashlib tracker libtorrent


    【解决方案1】:

    info_hash 不应该是 hex 表示。它应该是 binary 表示。即您需要对其进行 URL 编码。即不要打电话给.hexdigest()

    看起来当您调用urllib.parse.quote_plus(info_hash) 时,您正在对信息哈希的十六进制表示进行 URL 编码。即它仍然是十六进制编码的。

    你可能想使用类似urllib.parse.quote(info_hash)的东西

    【讨论】:

      【解决方案2】:

      您需要传递原始的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'
      }
      

      另外,应该指出的是,这仅适用于单个文件的种子。当您看到一个包含多个文件的文件时,您获取长度的方式将需要处理 torrent 中的 files 元素,因为这些文件没有 length 条目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多