>  기사  >  백엔드 개발  >  Python을 사용하여 BT 시드를 생성하고 Linux 플랫폼에서 BT 시드 정보를 얻는 방법

Python을 사용하여 BT 시드를 생성하고 Linux 플랫폼에서 BT 시드 정보를 얻는 방법

高洛峰
高洛峰원래의
2017-02-07 16:54:241671검색

이 기사의 예에서는 Linux 플랫폼에서 Python을 사용하여 BT 시드를 생성하고 BT 시드 정보를 얻는 방법을 설명합니다. 참고로 자세한 내용은 다음과 같습니다.

최근 Linux BT 서버 환경 구축에 대해 연구했는데 Linux에서 BT Seed 정보를 얻어야 합니다. 프로세스를 정리했습니다.

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

BT 시드 정보를 얻으려면 Python의 bencode 모듈이 필요합니다. 다운로드 주소: https://pypi.python.org/packages/source/b / 벤코드/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을 사용하여 BT 시드를 생성하고 Linux 플랫폼에서 BT 시드 정보를 얻는 방법에 대한 더 많은 기사를 보려면 PHP 중국어 웹사이트에 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.