def call(self, query, args=None, auth=False):
		dict = {'q': query}

		if args is not None:
			dict['args'] = args

		if auth:
			dict = self.authenticate(dict)

		query = bencoding.encode(dict)

		try:
			self.sock.send(query)

			response = self.sock.recv(1024 * 1024)
			if len(response) < 1:
				raise ConnectionError()
			response = bencoding.decode(response)

			if self.check_respose_auth_failed(response):
				raise AuthFailed()
			else:
				return response

		except (socket.error, ConnectionError):
			self.connected = False
			self.broken = True
			raise ConnectionError()
		except bencoding.DecodeError:
			return None
Exemple #2
0
 def __parseAnnounceResponse( self, response ):
     response = bencoding.decode( response )
     if 'failure reason' in response:
         log.error( 'Tracker announce request failed: %s' % ( response[ 'failure reason' ] ) )
         raise TrackerException
     if 'warning message' in response:
         log.warning( 'Tracker announce warning: %s' % ( response[ 'warning message' ] ) )
     self.seedersCount = response[ 'complete' ]
     self.leechersCount = response[ 'incomplete' ]
     if 'tracker id' in response:
         self.__trackerId = response[ 'tracker id' ]
     self.__parsePeers( response[ 'peers' ] )
Exemple #3
0
    def __init__(self, file):
        f = open(file, 'rb')
        data = f.read()
        f.close()

        self.__torrent = decode(data)

        self.__trackers = []
        if b'announce-list' in self.__torrent.keys():
            for l in self.__torrent[b'announce-list']:
                self.__trackers += [str(announce, 'utf-8') for announce in l]
        else:
            self.__trackers.append(str(self.__torrent[b'announce'], 'utf-8'))

        self.__info = self.__torrent[b'info']
        self.__length = 0
        if b'files' in self.__info.keys():
            for f in self.__info[b'files']:
                self.__length += f[b'length']
        else:
            self.__length = self.__info[b'length']

        self.__hash = hashlib.sha1(encode(self.__info)).digest()
#!/usr/bin/python3

import bencoding;

initial = {'Life, the universe and everything': [42, 'forty-two']}

encoded = bencoding.encode(initial)
print(encoded)

decoded = bencoding.decode(encoded)
print(decoded)
Exemple #5
0
	def get_info_from_tracker(self):
		'''	Construct tracker request and return decoded response.
			Spec here: https://wiki.theory.org/BitTorrentSpecification#Tracker_Request_Parameters
		'''
		tracker_info = get(self.announce, params=self.tracker_params, stream=True).raw.read()
		return decode(tracker_info)