【发布时间】:2015-08-29 06:41:01
【问题描述】:
我正在尝试制作一个 python 脚本,它只下载给定 infohash 的 torrent 的元数据。这个 infohash 是从一个 json 文件加载的,内容如下:
{"infohash":"someinfohash"}
如果我手动将 infohash 编码到链接字符串中,或者我自己使用字典进行编码,如下所示:
link = 'magnet:?xt=urn:btih:someinfohash'
或者像这样:
foo = {}
foo['infohash'] = 'someinfohash'
link = 'magnet:?xt=urn:btih:' + foo['infohash']
我总是可以下载元数据,没问题。但由于某种原因,当我从 json 文件加载它时,它总是超时。
thedata = open(sys.argv[1]).read()
thedata = json.loads(thedata)
ses = lt.session()
ses.listen_on(6881, 6891)
params = {
'save_path': '.', # doesn't matter because we're only downloading metadata
}
link = 'magnet:?xt=urn:btih:' + thedata['infohash']
handle = lt.add_magnet_uri(ses, link, params)
ses.add_dht_router('dht.transmissionbt.com', 6881)
ses.add_dht_router('router.bittorrent.com', 6881)
ses.add_dht_router('router.utorrent.com', 6881)
ses.start_dht()
sys.stdout.write('Downloading metadata...')
sys.stdout.flush()
timeout = time.time()
while (not handle.has_metadata()):
if (time.time() >= 300 + timeout):
print 'timed out'
sys.exit(1)
time.sleep(1)
print 'done'
ses.pause()
如果我检查字符串是否相等,像这样:
link = 'magnet:?xt=urn:btih:' + thedata['infohash']
link2 = 'magnet:?xt=urn:btih:someinfohash'
print link == link2
它打印的是真的。
有人知道发生了什么吗?
【问题讨论】:
标签: python json libtorrent