本文實例講述了linux平臺使用Python制作BT種子并獲取BT種子信息的方法。分享給大家供大家參考,具體如下:
最近研究了一下linux BT服務(wù)器環(huán)境的搭建,需要在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('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為做種的文件或目錄名字。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選