Пример #1
0
    def do_GET(self):
        """
        Handling the GET requests for scrape and announce according to
        predefined data initialised in the server
        """
        json_responses = self.server.json_responses
        curr_indices = self.server.curr_indices
        self.parsed_path = urlparse(self.path)
        #logging.info(f'Parsed path:{self.parsed_path}')
        #parsed_query=parse_qs(self.parsed_path.query.replace('%',''),errors='backslashreplace'
        #                          )
        input_hash = get_info_hash_field(
            self.parsed_path.query)  #self._get_hash(parsed_query)
        input_hash = convert_url_hash_to_hex(input_hash)
        #logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
        logging.info(f'Accepted hash : {input_hash}')
        input_hash = input_hash.lower()
        if input_hash not in json_responses.keys():
            logging.info('Hash not found')
            self._set_response()
            self.wfile.write(
                Bencode.encode({
                    'failure reason':
                    f'SERVER: Hash doesnt exist : {input_hash}'
                }))
            return
        #self.wfile.write(f"GET request for {self.path}\nExtra Data={self.server.data}".encode('utf-8'))
        #logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
        cmd = self.parsed_path.path[1:]

        responses = json_responses[input_hash][cmd]
        response_idx = curr_indices[input_hash][cmd]
        logging.info(f'Cmd : {cmd}. Curr idx : {response_idx}')
        curr_indices[input_hash][cmd] = (curr_indices[input_hash][cmd] +
                                         1) % len(responses)
        res = responses[response_idx]

        self._set_response()
        self.wfile.write(res.encode('ISO-8859-1'))
Пример #2
0
def test_encode_errors():
    with pytest.raises(BencodeEncodingError):
        Bencode.encode(object())
Пример #3
0
def test_encode_errors():
    with pytest.raises(BencodeEncodingError):
        Bencode.encode(object())
Пример #4
0
def test_encode_error_long():
    # os.path.getsize() can be longs on py2
    a_long = long(741634835)
    encoded_long = Bencode.encode(a_long)