Create and validate torrent seed files using the Python Open Source Library __python

Source: Internet
Author: User
Tags python script in python

The following article from the reference [1], here will be my practice process record as follows, convenient up-and reference, my operating system is Ubuntu 14.04 64bit

I. Installation of open source BT seed production software package

cd/home/taoyx/Download/

wget http://jaist.dl.sourceforge.net/project/mktorrent/mktorrent/1.0/mktorrent-1.0.tar.gz

Tar zxvf mktorrent-1.0.tar.gz

CD mktorrent-1.0

Make

sudo make install

Once the installation is complete, use the following command to see if Mktorrent is in the following directory

Which mktorrent

/usr/local/bin/mktorrent

Two. Install the Python bencode module, which is used to obtain information about the BT seed file,

wget https://pypi.python.org/packages/source/b/bencode/bencode-1.0.tar.gz

Tar zxvf bencode-1.0.tar.gz

CD bencode-1.0.tar.gz

sudo python setup.py install

Three. Use the following Python script make_torrent.py to make and validate BT seed file information, and note that if you are only making and not validating, call the following command line directly in Python.

#!/usr/bin/env python #encoding: utf-8 import OS, RE, time, sys import Hashlib, bencode if Len (sys.argv)!= 3:print ' Usage:%s <src_file> <dst_file> '% sys.argv[0] sys.exit (1) bt_file = sys.argv[1] Bt_torrent = sys.argv[  2] If os.path.exists (bt_torrent): Os.remove (bt_torrent) if Os.path.exists (bt_file): #make torrent ... cmd = '/usr/local/bin/mktorrent-v-p-l 18-a http://bt1.text.cn/announce-a http://bt2.text.cn/announce-o%s '% ( Bt_torrent, bt_file) #validate torrent Info result = Os.popen (cmd). ReadLines () [ -1].strip () If ' do ' in result : Bt_path = {} bt_filename = open (bt_torrent, ' rb ') Bt_info = Bencode.bdecode (Bt_filename.read ()). Get (' info ') Bt_info_hash_hex = HASHLIB.SHA1 (Bencode.bencode (Bt_info)). Hexdigest () if Os.path.isdir (bt_file ): bt_file_size = 0 for length in bt_info.get (' Files '): Bt_file_size + = Int (length
            [' Length ']]    bt_path['/'. Join (length[' path ')] = length[' length '] else:bt_file_size = bt_info.get (' length ')
        Bt_file_name = bt_info.get (' name ') bt_path[bt_file_name] = Bt_file_size bt_filename.close () Print bt_path print "Create torrent success" else:print "Create torrent fail" Sys.exi T (1) else:print "The specified source can not find" sys.exit (0)

Here is the call effect graph


Reference documents

[1].http://wangwei007.blog.51cto.com/68019/1216616

Related Article

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.