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

yipeiwu_com5年前Python基础

本文实例讲述了linux平台使用Python制作BT种子并获取BT种子信息的方法。分享给大家供大家参考,具体如下:

最近研究了一下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 -*-
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为做种的文件或目录名字。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

利用Python实现颜色色值转换的小工具

利用Python实现颜色色值转换的小工具

先看看Zeplin 的颜色色值显示示例 原有处理方式 因为我会 Python (仅限于终端输入 python 然后当做计算器算,或者用 hex() 函数把十进制转换成十六进制),所...

简单介绍Python下自己编写web框架的一些要点

在正式开始Web开发前,我们需要编写一个Web框架。 为什么不选择一个现成的Web框架而是自己从头开发呢?我们来考察一下现有的流行的Web框架: Django:一站式开发框架,但不...

pytorch 修改预训练model实例

我就废话不多说了,直接上代码吧! class Net(nn.Module): def __init__(self , model): super(Net, self)._...

python检测IP地址变化并触发事件

IoT PoC项目中需要展示视频采集源进行wifi切换后(表明视频采集源端发生了移动),接收端观看到的视频的流畅度,以及当接收端进行移动时,检测视频的流畅度,故需要一个模块周期性地探测本...

解决在pycharm中显示额外的 figure 窗口问题

解决在pycharm中显示额外的 figure 窗口问题

问题描述 在电脑中重新安装Anaconda3&PyCharm后,运行原来的程序画图时出现了下图界面。 不能弹出如下图所示的“figure”窗口。 解决方法: 这是因为PyCharm在...