The linux platform USES Python to make BT seeds and obtain BT seed information

  • 2020-05-24 05:46:11
  • OfStack

The example of this article describes the linux platform using Python to make BT seed and obtain BT seed information. I will share it with you for your reference as follows:

Recently, I studied the setup of linux BT server environment under 1. It is necessary to make BT seed under linux and obtain BT seed information. I sorted out the following process:

BT seed software production site download address.

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 bencode module is needed 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 is made and verified 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 seed.

More about Python related topics: interested readers to view this site "Python file and directory skills summary", "Python skills summary text file", "Python URL skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using skills summary", "Python string skills summary" and "Python introductory and advanced tutorial"

I hope this article is helpful for you to design Python program.


Related articles: