Пример #1
0
 def process_find_node_request(self, req, address):
     msg = {
         't': req['t'],
         'y': 'r',
         'r': {
             'id': get_neighbor_id(self.nid),
             'nodes': KNode.encode_nodes(self.node_list[:8])
         }
     }
     self.ufd.sendto(bencode(msg), address)
Пример #2
0
 def send_find_node(self, address, nid, target_id=random_id()):
     msg = {
         't': ''.join(chr(random.randint(0, 255)) for _ in xrange(2)),
         'y': 'q',
         'q': 'find_node',
         'a': {
             'id': nid,
             'target': target_id
         }
     }
     self.ufd.sendto(bencode(msg), address)
Пример #3
0
    def _getTorrentHash(self, download):
        headers = download.extra_data["headers"] if "headers" in download.extra_data else {}
        response = requests.get(download.url, headers=headers)

        if response.status_code == 200:
            metainfo = bencode.bdecode(response.content)
            info = metainfo["info"]
            return (True, Binary(response.content), hashlib.sha1(bencode.bencode(info)).hexdigest().upper())
        else:
            log.info("Download torrent file for rutorrent failed with url %s" % download.url)
            return (False, None, "")
Пример #4
0
 def process_get_peers_request(self, req, address):
     infohash = req['a']['info_hash']
     msg = {
         't': req['t'],
         'y': 'r',
         'r': {
             'id': get_neighbor_id(infohash, 3),
             'nodes': KNode.encode_nodes(self.node_list[:8]),
             'token': infohash[:4]  # 自定义token,例如取infohash最后四位
         }
     }
     self.ufd.sendto(bencode(msg), address)
Пример #5
0
    def _getTorrentHash(self, download):
        headers = download.extra_data[
            'headers'] if 'headers' in download.extra_data else {}
        response = requests.get(download.url, headers=headers)

        if response.status_code == 200:
            metainfo = bencode.bdecode(response.content)
            info = metainfo['info']
            return (True, Binary(response.content),
                    hashlib.sha1(bencode.bencode(info)).hexdigest().upper())
        else:
            log.info("Download torrent file for rutorrent failed with url %s" %
                     download.url)
            return (False, None, '')
Пример #6
0
def request_metadata(the_socket, ut_metadata, piece):
    msg = chr(BT_MSG_ID) + chr(ut_metadata) + bencode({
        'msg_type': 0,
        'piece': piece
    })
    send_message(the_socket, msg)
Пример #7
0
def send_ext_handshake(the_socket):
    msg = chr(BT_MSG_ID) + chr(EXT_HANDSHAKE_ID) + bencode(
        {'m': {
            'ut_metadata': 1
        }})
    send_message(the_socket, msg)
Пример #8
0
def getTorrentHash(torrentPath):
    with open(torrentPath, 'rb') as f:
        metainfo = bencode.bdecode(f.read())
    info = metainfo['info']
    return hashlib.sha1(bencode.bencode(info)).hexdigest().upper()
Пример #9
0
def getTorrentHash(torrentPath):
    with open(torrentPath, "rb") as f:
        metainfo = bencode.bdecode(f.read())
    info = metainfo["info"]
    return hashlib.sha1(bencode.bencode(info)).hexdigest().upper()
Пример #10
0
 def send_pong(self, msg, address):
     msg = {'t': msg['t'], 'y': 'r', 'r': {'id': self.nid}}
     self.ufd.sendto(bencode(msg), address)