• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 21次浏览 已收录 0个评论

本文实例讲述了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, sysimport hashlib, bencodefile_name = 'bt_test.exe'bt_source = '/data/updir/%s' % file_namebt_name = '/data/source/%s.torrent' % file_nameif 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('<a style="color:transparent">本文来源gao($daima.com搞@代@#码$网</a>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程序设计有所帮助。

更多linux平台使用Python制作BT种子并获取BT种子信息的方法相关文章请关注搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:linux平台使用Python制作BT种子并获取BT种子信息的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址