python3使用bencode.py庫實現BT種子生成磁力鏈接


需要先安裝bencode.py庫。

pip install bencode.py
import bencode
import hashlib
from urllib.parse import quote


def torrent_file_to_magnet(torrent_file):
    data =  open(torrent_file, 'rb').read()
    metadata = bencode.bdecode(data)
    name = metadata['info']['name']
    dn = quote(name)
    info_bts = bencode.bencode(metadata['info'])
    info_hash = hashlib.sha1(info_bts).hexdigest()
    return f'magnet:?xt=urn:btih:{info_hash}&dn={dn}'


if __name__ == '__main__':
    print(torrent_file_to_magnet('ubuntu-20.04-desktop-amd64.iso.torrent'))

 

輸出:

magnet:?xt=urn:btih:9fc20b9e98ea98b4a35e6223041a5ef94ea27809&dn=ubuntu-20.04-desktop-amd64.iso

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM