在 python 中对 torrent 跟踪器的 HTTP 请求

Posted

技术标签:

【中文标题】在 python 中对 torrent 跟踪器的 HTTP 请求【英文标题】:HTTP request to the torrent tracker in python 【发布时间】:2017-06-28 13:19:20 【问题描述】:

我想写一个python torrent客户端(因为我需要它并且我想提高我的技能)。 我必须向跟踪器发送 HTTP 获取请求。我正在使用请求库。

from read_torrent import *
import requests as req


txt = read_torrent('arch.torrent')
(res, other) = parse(txt)
pl = 
pl['uploaded'] = 0
pl['downloaded'] = 0
pl['event'] = 'started'
pl['peer_id'] = '12345678987654321234'
pl['info_hash'] = dont_know
pl['left'] = 0
pl['port'] = 6881

r = requests.get(res['announce'], params=pl)
r.raise_for_status()
print('-------RESULT-------')
print(r.text)

两个问题:

    我是否以正确的方式使用get 函数? info_hash 应该使用什么?

附言。我已经阅读了THIS 和维基理论页面。

【问题讨论】:

该链接今天可能有效,但可能会在几周后变得无用。请发布代码示例来说明您的问题,而不是在 github 存储库上重定向 这可能更适合codereview.stackexchange.com 【参考方案1】:

info_hash 是您从 .torrent 文件计算的 SHA1 哈希。 (编码信息字典)。可以在here 找到 Python 的示例。

get请求首先应该包含HTTP-Tracker的链接+announce请求的结构。

请注意:现在大多数跟踪器都是 UDP(开销较小)。您必须确保您的 .torrent 文件包含 HTTP 跟踪器(在编辑器中打开文件)。以 udp:// 开头的跟踪器不会响应您的 HTTP 请求。

【讨论】:

以上是关于在 python 中对 torrent 跟踪器的 HTTP 请求的主要内容,如果未能解决你的问题,请参考以下文章