Linux platform uses Python to make BT seeds and get BT seed information

Source: Internet
Author: User
The examples in this article describe how the Linux platform uses Python to make BT seeds and get BT seed information. Share to everyone for your reference, as follows:

Recently studied the Linux BT Server environment to build, need to make BT seeds under Linux and obtain the BT seed information, finishing this process:

Make BT seed software download address of this website.

Installation:

[Root@localhost src]# tar zxf mktorrent-1.0.tar.gz[root@localhost src]# cd mktorrent-1.0[root@localhost mktorrent-1.0] # make[root@localhost mktorrent-1.0]# make install[root@localhost ~]# which mktorrent/usr/local/bin/mktorrent

Python's Bencode module is required to obtain the BT seed information, download address: https://pypi.python.org/packages/source/b/bencode/bencode-1.0.tar.gz.

Installation:

#tar-zxf bencode-1.0.tar.gz#cd bencode-1.0.tar.gz#python setup.py Install

The Python scripts produced and validated are as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-import os, Re, time, Sysimport hashlib, bencodefile_name = ' Bt_test.exe ' Bt_sou Rce = '/data/updir/%s '% file_namebt_name = '/data/source/%s.torrent '% File_nameif os.path.exists (bt_name): Os.remove (b T_name) if Os.path.exists (bt_source): conm = "/usr/local/bin/mktorrent-v-p-l 18-a http://bt1.text.cn/announce-a http:    Bt2.text.cn/announce-o%s%s "% (bt_name,bt_source) res = Os.popen (conm). ReadLines () [ -1].strip () If ' done ' in res: Bt_path = {} bt_file = open (Bt_name, ' rb ') Bt_info = Bencode.bdecode (Bt_file.read ()). Get (' info ') bt_info_hash_he x = HASHLIB.SHA1 (Bencode.bencode (Bt_info)). Hexdigest () if Os.path.isdir (bt_source): bt_file_size = 0 for Leng Th in Bt_info.get (' Files '): Bt_file_size = bt_file_size + int (length[' length ')) bt_path['/'. Join (length[' Pat H '])] = length[' length '] else:bt_file_size = bt_info.get (' length ') Bt_file_name = bt_info.get (' name ') b T_path[bt_file_name]=bt_fIle_size bt_file.close () Print bt_path print "Create torrent success" else:print "Create torrent Error" s Ys.exit () Else:print "This source not find" sys.exit ()

File_name is the name of a file or directory for making a seed.

I hope this article is helpful for Python program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.