一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

linux平台使用Python制作BT种子并获取BT种子信息的方法

时间:2017-02-10 编辑:简简单单 来源:一聚教程网

最近研究了一下linux BT服务器环境的搭建,需要在linux下制作BT种子并获取BT种子信息,整理了一下这个过程:

制作BT种子软件本站下载地址。

安装:

 代码如下复制代码

[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模块来获取BT种子信息,下载地址: https://pypi.python.org/packages/source/b/bencode/bencode-1.0.tar.gz。

安装:

 代码如下复制代码

#tar -zxf bencode-1.0.tar.gz

#cd bencode-1.0.tar.gz

#python setup.py install

制作和验证的python脚本如下:

 代码如下复制代码

#!/usr/bin/env python

# -*- coding: utf-8 -*-

importos, re, time, sys

importhashlib, bencode

file_name='bt_test.exe'

bt_source='/data/updir/%s'%file_name

bt_name='/data/source/%s.torrent'%file_name

ifos.path.exists(bt_name):

  os.remove(bt_name)

ifos.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'inres:

    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()

    ifos.path.isdir(bt_source):

      bt_file_size=0

      forlengthinbt_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()

    printbt_path

    print"Create torrent success"

  else:

    print"Create torrent Error"

    sys.exit()

else:

  print"This source not find"

  sys.exit()

file_name为做种的文件或目录名字。

热门栏目