Home  >  Article  >  Backend Development  >  How to use Python to create BT seeds and obtain BT seed information on Linux platform

How to use Python to create BT seeds and obtain BT seed information on Linux platform

高洛峰
高洛峰Original
2017-02-07 16:54:241672browse

The example in this article describes how to use Python on the Linux platform to create BT seeds and obtain BT seed information. I would like to share it with you for your reference. The details are as follows:

I recently studied the establishment of a Linux BT server environment. I need to create BT seeds and obtain BT seed information under Linux. I have sorted out the process:

Create the download address of this website for BT seed software.

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

Requires python’s bencode module to obtain 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 script produced and verified is as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, re, time, sys
import hashlib, bencode
file_name = 'bt_test.exe'
bt_source = '/data/updir/%s' % file_name
bt_name = '/data/source/%s.torrent' % file_name
if os.path.exists(bt_name):
  os.remove(bt_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_hex = hashlib.sha1(bencode.bencode(bt_info)).hexdigest()
    if os.path.isdir(bt_source):
      bt_file_size = 0
      for length in bt_info.get('files'):
        bt_file_size = 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_file.close()
    print bt_path
    print "Create torrent success"
  else:
    print "Create torrent Error"
    sys.exit()
else:
  print "This source not find"
  sys.exit()

file_name is The name of the file or directory to be seeded.

I hope this article will be helpful to everyone in Python programming.

For more related articles on how to use Python to create BT seeds and obtain BT seed information on the Linux platform, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn