本文整理汇总了Python中tracker.Tracker.send_request_and_parse_response方法的典型用法代码示例。如果您正苦于以下问题:Python Tracker.send_request_and_parse_response方法的具体用法?Python Tracker.send_request_and_parse_response怎么用?Python Tracker.send_request_and_parse_response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tracker.Tracker
的用法示例。
在下文中一共展示了Tracker.send_request_and_parse_response方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Client
# 需要导入模块: from tracker import Tracker [as 别名]
# 或者: from tracker.Tracker import send_request_and_parse_response [as 别名]
class Client(object):
def __init__(self, torrent):
self.torrent = torrent
self.torrent_state = 'random'
self.reactor = Reactor()
self.reactor_activated = False
self.peer_id = '-TZ-0000-00000000000'
self.peers = []
self.decode_torrent_and_setup_pieces()
self.handshake = self.build_handshake()
self.setup_tracker()
self.stitcher = Stitcher(self)
self.setup_peers()
def decode_torrent_and_setup_pieces(self):
f = open(self.torrent, 'r')
metainfo = B.bdecode(f.read())
data = metainfo['info'] # Un-bencoded dictionary
self.info_hash = H.sha1(B.bencode(data)).digest()
self.announce_url = self.find_http_announce_url(metainfo)
#self.announce_url = 'http://tracker.ccc.de:6969/announce'
self.file_name = data['name'] # Dir name if multi, otherwise file name
self.piece_length = data['piece length']
if 'files' in data: # Multifile torrent
self.setup_multi_file_info(data)
else:
self.setup_single_file_info(data)
self.setup_download_directory()
self.check_if_dload_file_exists()
self.setup_pieces(self.piece_length, data['pieces'])
def find_http_announce_url(self, metainfo):
print metainfo.keys()
# print metainfo['announce-list']
if self.is_http_url(metainfo['announce']):
return metainfo['announce']
elif 'announce-list' in metainfo.keys():
for url in metainfo['announce-list']:
url = url[0]
if self.is_http_url(url):
print url
return url
raise SystemExit('UDP announce urls are not supported. Currently only HTTP is supported.')
def is_http_url(self, url):
return 'http://' in url
def setup_multi_file_info(self, metainfo):
self.is_multi_file = True
self.files = metainfo['files'] # dictionary of file lengths + paths
self.file_length = sum([file_dict['length'] for file_dict in self.files]) # file_length = total # bytes to dload
def setup_single_file_info(self, metainfo):
self.is_multi_file = False
self.file_length = metainfo['length']
def build_handshake(self):
logging.info('Building handshake')
pstr = 'BitTorrent protocol'
handshake = struct.pack('B' + str(len(pstr)) + 's8x20s20s',
# 8x => reserved null bytes
len(pstr),
pstr,
self.info_hash,
self.peer_id
)
assert handshake != None
assert len(handshake) == 49 + len(pstr)
logging.info('Handshake constructed.')
return handshake
def setup_tracker(self):
self.tracker = Tracker(self, self.announce_url)
def setup_peers(self):
peer_ips = self.tracker.send_request_and_parse_response()
self.connect_to_peers(peer_ips)
def connect_to_peers(self, peer_tuples):
peers = [Peer(ip, port, self) for ip, port in peer_tuples]
logging.debug('Attempting to connect to peers %s', peer_tuples)
for peer in peers:
try:
if peer.ip == self.get_self_ip():
logging.info('Skipping peer; cannot connect to self')
continue
peer.connect()
peer_handshake = peer.send_and_receive_handshake(self.handshake)
logging.debug('Handshake returned.')
if peer.verify_handshake(peer_handshake, self.info_hash):
logging.debug('Handshake verified. Adding peer to peer list')
self.add_peer(peer)
if not self.reactor_activated:
self.activate_reactor()
self.reactor_activated = True
except IOError as e:
logging.warning('Error in construct_peers! %s', e)
self.manage_requests(5)
#.........这里部分代码省略.........