python实现bt种子 torrent转magnet

Python实现bt转磁链 
参考前人资料主要两种方式
1,利用python的bencode模块
2,安装libtorrent模块
尝试过两种方法特记录
环境:Windows系统  python 3
 
bencode模块有很多,最早的一个不支持 python3,网上的实例代码就是使用2.7 所以 结果是不行。
 选了一个bencoder.pyx 1.2.0 不知道为啥选这个 
可以参考一下他的示例
pip 安装,上代码
from bencoder import bencode, bdecode
import hashlib
import base64

with open("加勒比海盗5:死无对证.2017.BD720P.X264.AAC.English&Mandarin.CHS-ENG.laziku.mp4.torrent", mode='rb',) as f :
    torrent = bdecode(f.read())
    # print(bencode(torrent[b'info']))
    # print(torrent)
    # torrent = f.read()


digest = hashlib.sha1(bencode(torrent[b'info'])).digest()
# torrent[b'info'] 里面包含了种子文件的关键信息 对这部分进行哈希处理
b32hash = base64.b32encode(digest)
print('magnet:?xt=urn:btih:%s' % b32hash.decode())

#结果 magnet:?xt=urn:btih:7GI3YCSXAKTGIYBJYKVUVIKELGDBXLDF
我使用 bt转磁链网站 得到结果 
 
用迅雷试一下 都能抓到种子
 
参考一下 关于bt种子的文件格式 文章
python2.7 的bt转磁链 代码
 
 
libtorrent 的处理代码更加简单,里面封装好了方法 直接调用就可以
不 过我在win环境下 这个模块安装出错,
 摘记一下代码
 
参考文章:http://www.au92.com/archives/P-y-t-h-o-n-jiang-B-T-zhong-zi-wen-jian-zhuan-huan-wei-ci-li-lian-de-liang-zhong-fang-fa.html
 
 
 
原文地址:https://www.cnblogs.com/hoomob/p/7566583.html