[英]Implementing a bittorent client and I get 403 from the tracker
I was trying my hand at implementing a bittorrent client in python (I know there are libs out there that can do this for me easily, but I'm just trying to learn new things).我正在尝试在 python 中实现一个 bittorrent 客户端(我知道有一些库可以轻松地为我做到这一点,但我只是想学习新东西)。
I downloaded and managed to succesfully decode the torrent file, however when I try to do the GET request at the tracker I get back a 403 response and I have no idea why.我下载并成功解码了 torrent 文件,但是当我尝试在跟踪器上执行 GET 请求时,我得到了 403 响应,但我不知道为什么。 This is what I tried (this is code copied from the python shell):
这是我尝试过的(这是从 python shell 复制的代码):
>>> f = open("torrents/test.torrent")
>>> torrentData = bencoder.decode(f.read())
>>> torrentData["announce"]
'http://reactor.flro.org:8080/announce.php?passkey=d59fc5b5b9e2664895ad1c68a3621caf'
>>> params["info_hash"] = sha1(bencoder.encode(torrentData["info"])).digest()
>>> params["peer_id"] = '-AZ-1234-12345678901'
>>> params["left"] = sum(f["length"] for f in torrentData["info"]["files"])
>>> params["port"] = 6890
>>> params["uploaded"] = 0
>>> params["downloaded"] = 0
>>> params["compact"] = 1
>>> params["event"] = "started"
>>> params
{'uploaded': 0, 'compact': 1, 'info_hash': '\xab}\x19\x0e\xac"\x9d\xcf\xe5g\xd4R\xae\xee\x1e\xd7\
>>> final_url = torrentData["announce"] + "&" + urllib.urlencode(params)
>>> final_url
'http://reactor.flro.org:8080/announce.php?passkey=d59fc5b5b9e2664895ad1c68a3621caf&uploaded=0&co
>>> urllib2.urlopen(final_url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
response = meth(req, response)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
'http', request, response, code, msg, hdrs)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
return self._call_chain(*args)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
result = func(*args)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
Am I missing something from the params folder?我在 params 文件夹中遗漏了什么吗? I also tried this torrent in my uTorrent client and it worked so the tracker is working fine.
我也在我的 uTorrent 客户端中尝试了这个 torrent,它工作正常,所以跟踪器工作正常。 I even tried the naked announce url (without the params) and same thing.
我什至尝试了裸宣布网址(没有参数)和同样的事情。 From what I read from the bittorrent specification there is no mention of a 403 response from the tracker.
从我从 bittorrent 规范中读到的内容中,没有提到来自跟踪器的 403 响应。
I'd be very happy if you guys could help me out with this.如果你们能帮我解决这个问题,我会很高兴。
To reduce the amount of variables it is better to test against a tracker you're running locally, eg opentracker is a good choice since it imposes few requirements.为了减少变量的数量,最好针对您在本地运行的跟踪器进行测试,例如opentracker是一个不错的选择,因为它几乎没有要求。
Errors you only get on specific trackers but not on others are likely due to additional requirements imposed by the tracker administrators and not by the bittorrent protocol itself.您只在特定跟踪器上而不是在其他跟踪器上出现的错误可能是由于跟踪器管理员强加的额外要求而不是 bittorrent 协议本身造成的。
The major exceptions are that many public trackers may not allow non-compact announces or require UDP announces instead of HTTP ones.主要的例外是许多公共跟踪器可能不允许非压缩公告或需要 UDP 公告而不是 HTTP 公告。
Ok I managed to figure out the issue.好的,我设法弄清楚了这个问题。 It's kinda silly but it's actually because the request to the tracker didn't have any headers, and that tracker actually needed an user-agent otherwise it would reject the request.
这有点愚蠢,但实际上是因为对跟踪器的请求没有任何标头,而该跟踪器实际上需要一个用户代理,否则它会拒绝该请求。 All I had to do was add a user-agent to the request.
我所要做的就是在请求中添加一个用户代理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.