Esempio n. 1
0
	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
Esempio n. 2
0
	def exit(self):
		dict = self.authenticate({'q': 'Core_exit'})
		query = bencoding.encode(dict)

		try:
			self.sock.send(query)
			self.close()
		except socket.error:
			self.connected = False
			self.broken = True
Esempio n. 3
0
 def __init__(self, filepath):
     self.filepath = filepath
     f = open(filepath, "rb")
     self.raw_torrent = f.read()
     f.close()
     self.torrent_header = bencoding.decode(self.raw_torrent)
     self.announce = self.torrent_header[b"announce"].decode("utf-8")
     torrent_info = self.torrent_header[b"info"]
     m = hashlib.sha1()
     m.update(bencoding.encode(torrent_info))
     self.file_hash = m.digest()
Esempio n. 4
0
 def __init__(self, filepath: str):
     self.filepath = filepath
     with open(filepath, 'rb') as file:
         data = decode(file.read())
     self.meta_info = MetaInfo(data)
     self.info_hash = sha1(encode(data['info'])).digest()
     self.uploaded = 0
     self.downloaded = 0
     self.tracker_id = None
     self.complete = False
     self.running = False
Esempio n. 5
0
	def authenticate(self, dict):
		cookie = self.cookie()
		if cookie is None:
			return False

		cookie = cookie.get('cookie')
		password = self.password or ''
		hash = hashlib.sha256((password + cookie).encode('utf-8')).hexdigest()

		if 'q' in dict:
			dict['aq'] = dict['q']

		dict['q'] = 'auth'
		dict['hash'] = hash
		dict['cookie'] = cookie

		request = bencoding.encode(dict)
		dict['hash'] = hashlib.sha256(request).hexdigest()

		return dict
Esempio n. 6
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()
Esempio n. 7
0
#!/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)