当前位置: 首页>>代码示例>>Python>>正文


Python Tracker.parse_peer_address方法代码示例

本文整理汇总了Python中tracker.Tracker.parse_peer_address方法的典型用法代码示例。如果您正苦于以下问题:Python Tracker.parse_peer_address方法的具体用法?Python Tracker.parse_peer_address怎么用?Python Tracker.parse_peer_address使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tracker.Tracker的用法示例。


在下文中一共展示了Tracker.parse_peer_address方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Torrent_Downloader

# 需要导入模块: from tracker import Tracker [as 别名]
# 或者: from tracker.Tracker import parse_peer_address [as 别名]
class Torrent_Downloader():
	''' Manages download logic:
		- Creation and removal of peers. 
		- Book keeping of pieces downloaded and in progress.
		- Checking completed pieces and writing to file.
	'''
	def __init__(self, torrent, start_listener_callback):
		self.torrent = torrent
		self.message_handler = MessageHandler(self.torrent, self)
		self.start_listener_callback = start_listener_callback
		self.ip = self.get_IP_address()
		self.tracker = Tracker(self.torrent.announce, self.torrent.get_params())
		self.peers = self.create_peers()
		self.io_loop = get_event_loop()
		self.index = 0
		self.callback_dict = {
			'check_piece' : self.torrent.check_piece_callback,
			'pieces_changed' : self.pieces_changed_callback,
			'start_listener' : self.start_listener_callback,
		}
		self.pieces_needed = []


	def get_IP_address(self):
		response = get('http://api.ipify.org?format=json')
		ip_object = loads(response.text)
		return ip_object["ip"]

	def create_peers(self):
		peers = []
		for p in self.tracker.parse_peer_address():
			if p[0] == self.ip:
				continue
			peers.append(Peer(p[0], p[1], self))
		return peers

	def pieces_changed_callback(self, peer):
		'''	Check if connected peer has pieces I need. Send interested message.
			Call choose_piece.
			If peer has no pieces I need, disconnect and remove from peers list.
		'''
		self.torrent.update_pieces_needed()
		for i in self.torrent.pieces_needed:
			if peer.has_pieces[i]:
				self.io_loop.create_task(self.message_handler.send_message(peer=peer, message_id=2))
				self.choose_piece(peer=peer)	
				break
			else:
				self.peers.remove(peer)

	def choose_piece(self, peer):
		'''	Finds the next needed piece, updates self.have and self.pieces_needed.
			calls construct_request_payload.
		'''
		piece_index = self.torrent.pieces_needed[0]
		self.torrent.have[piece_index] = True
		self.torrent.update_pieces_needed()
		self.message_handler.construct_request_payload(peer=peer, piece_index=piece_index)
开发者ID:TansyArron,项目名称:bittorrent,代码行数:60,代码来源:torrent_downloader.py


注:本文中的tracker.Tracker.parse_peer_address方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。