linux平台使用Python制作BT种子并获取BT种子信息的方法
本文实例讲述了linux平台使用Python制作BT种子并获取BT种子信息的方法。分享给大家供大家参考,具体如下:
最近研究了一下linuxBT服务器环境的搭建,需要在linux下制作BT种子并获取BT种子信息,整理了一下这个过程:
制作BT种子软件本站下载地址。
安装:
[root@localhostsrc]#tarzxfmktorrent-1.0.tar.gz [root@localhostsrc]#cdmktorrent-1.0 [root@localhostmktorrent-1.0]#make [root@localhostmktorrent-1.0]#makeinstall [root@localhost~]#whichmktorrent /usr/local/bin/mktorrent
需要python的bencode模块来获取BT种子信息,下载地址:https://pypi.python.org/packages/source/b/bencode/bencode-1.0.tar.gz。
安装:
#tar-zxfbencode-1.0.tar.gz #cdbencode-1.0.tar.gz #pythonsetup.pyinstall
制作和验证的python脚本如下:
#!/usr/bin/envpython
#-*-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-l18-ahttp://bt1.text.cn/announce-ahttp://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"Createtorrentsuccess"
else:
print"CreatetorrentError"
sys.exit()
else:
print"Thissourcenotfind"
sys.exit()
file_name为做种的文件或目录名字。
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《PythonURL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《PythonSocket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。