def test_parse_tracker_url(self): result = parse_tracker_url("http://tracker.openbittorrent.com:80/announce") self.assertEqual(result[0], "HTTP") self.assertEqual(result[2], "announce") result = parse_tracker_url("udp://tracker.openbittorrent.com:80/announce") self.assertEqual(result[0], "UDP") self.assertEqual(result[2], "announce") result = parse_tracker_url("udp://tracker.openbittorrent.com:80") self.assertEqual(result[0], "UDP") self.assertFalse(result[2]) result = parse_tracker_url("http://tracker.openbittorrent.com/announce") self.assertEqual(result[0], "HTTP") self.assertEqual(result[2], "announce")
def create_tracker_session(tracker_url, timeout, socket_manager, connection_pool=None): """ Creates a tracker session with the given tracker URL. :param tracker_url: The given tracker URL. :param timeout: The timeout for the session. :return: The tracker session. """ tracker_type, tracker_address, announce_page = parse_tracker_url(tracker_url) if tracker_type == u'udp': return UdpTrackerSession(tracker_url, tracker_address, announce_page, timeout, socket_manager) return HttpTrackerSession(tracker_url, tracker_address, announce_page, timeout, connection_pool=connection_pool)
def create_tracker_session(tracker_url, timeout): """ Creates a tracker session with the given tracker URL. :param tracker_url: The given tracker URL. :param timeout: The timeout for the session. :return: The tracker session. """ tracker_type, tracker_address, announce_page = parse_tracker_url(tracker_url) if tracker_type == u'UDP': return UdpTrackerSession(tracker_url, tracker_address, announce_page, timeout) else: return HttpTrackerSession(tracker_url, tracker_address, announce_page, timeout)
def create_tracker_session(tracker_url, on_result_callback): """ Creates a tracker session with the given tracker URL. :param tracker_url: The given tracker URL. :param on_result_callback: The on_result callback. :return: The tracker session. """ tracker_type, tracker_address, announce_page = parse_tracker_url(tracker_url) if tracker_type == u'UDP': session = UdpTrackerSession(tracker_url, tracker_address, announce_page, on_result_callback) else: session = HttpTrackerSession(tracker_url, tracker_address, announce_page, on_result_callback) return session
def create_tracker_session(tracker_url, on_result_callback): """ Creates a tracker session with the given tracker URL. :param tracker_url: The given tracker URL. :param on_result_callback: The on_result callback. :return: The tracker session. """ tracker_type, tracker_address, announce_page = parse_tracker_url( tracker_url) if tracker_type == u'UDP': session = UdpTrackerSession(tracker_url, tracker_address, announce_page, on_result_callback) else: session = HttpTrackerSession(tracker_url, tracker_address, announce_page, on_result_callback) return session
def test_parse_udp_no_port(self): parse_tracker_url("udp://tracker.openbittorrent.com")
def _process_header(self): # get and check HTTP response code protocol, code, msg = self._header_buffer.split(' ', 2) if code == '301' or code == '302': idx = self._header_buffer.find('Location: ') if idx == -1: self._is_failed = True else: new_location = ( self._header_buffer[idx:].split('\r\n')[0]).split(' ')[1] try: idx = new_location.find('info_hash=') if idx != -1: new_location = new_location[:idx] if new_location[-1] != '/': new_location += "/" new_location += "announce" tracker_type, tracker_address, announce_page = parse_tracker_url( new_location) if tracker_type != self._tracker_type: raise RuntimeError( u"cannot redirect to a different tracker type: %s", new_location) else: self._logger.debug(u"%s being redirected to %s", self, new_location) self._tracker_address = tracker_address self._announce_page = announce_page self._socket.close() self._socket = None self.recreate_connection() except RuntimeError as run_err: self._logger.info( u"%s: Runtime Error: %s, address: %s, announce: %s", self, run_err, self._tracker_address, self._announce_page) self._is_failed = True except Exception as err: self._logger.exception( u"Failed to process HTTP tracker header: [%s], Tracker: %s," u" Tracker Address: %s, Tracker Announce: %s", err, self._tracker_url, self._tracker_address, self._announce_page) self._logger.debug(u"Header: %s", self._header_buffer) self._is_failed = True return if code != '200': # error response code self._logger.debug(u"%s HTTP SCRAPE error response code [%s, %s]", self, code, msg) self._is_failed = True return # check the content type idx = self._header_buffer.find('Content-Encoding: ') if idx == -1: # assuming it is plain text or something similar self._content_encoding = 'plain' else: encoding = ( self._header_buffer[idx:].split('\r\n')[0]).split(' ')[1] self._content_encoding = encoding # get the content length idx = self._header_buffer.find('Content-Length: ') if idx == -1: # assume that the content is small # process the retrieved information success = self._process_scrape_response() if success: self._is_finished = True else: self._is_failed = True else: idx += len('Content-Length: ') self._content_length = int(self._header_buffer[idx:].split( '\r\n', 1)[0].strip())
def test_parse_tracker_url_wrong_type_1(self): parse_tracker_url("abc://tracker.openbittorrent.com:80/announce")
def test_parse_scheme_unknown(self): parse_tracker_url("unknown://ipv6.torrent.ubuntu.com:6969/announce")
def test_parse_tracker_url_wrong_type_2(self): parse_tracker_url("udp://tracker.openbittorrent.com/announce")
def test_parse_http_non_standard_port(self): result = parse_tracker_url( "http://ipv6.torrent.ubuntu.com:6969/announce") self.assertEqual(result, (u'http', (u'ipv6.torrent.ubuntu.com', 6969), u'/announce'))
def test_parse_scheme(self): parse_tracker_url("http://torrent.ubuntu.com:80")
def test_parse_scheme_correct_http(self): result = parse_tracker_url("http://torrent.ubuntu.com:6969/announce") self.assertEqual(result, (u'http', (u'torrent.ubuntu.com', 6969), u'/announce'))
def test_parse_http_no_announce_path(self): parse_tracker_url("unknown://ipv6.torrent.ubuntu.com:6969")
def _process_header(self): # get and check HTTP response code protocol, code, msg = self._header_buffer.split(' ', 2) if code == '301' or code == '302': idx = self._header_buffer.find('Location: ') if idx == -1: self._is_failed = True else: new_location = (self._header_buffer[idx:].split('\r\n')[0]).split(' ')[1] try: idx = new_location.find('info_hash=') if idx != -1: new_location = new_location[:idx] if new_location[-1] != '/': new_location += "/" new_location += "announce" tracker_type, tracker_address, announce_page = parse_tracker_url(new_location) if tracker_type != self._tracker_type: raise RuntimeError(u"cannot redirect to a different tracker type: %s", new_location) else: self._logger.debug(u"%s being redirected to %s", self, new_location) self._tracker_address = tracker_address self._announce_page = announce_page self._socket.close() self._socket = None self.recreate_connection() except RuntimeError as run_err: self._logger.info(u"%s: Runtime Error: %s, address: %s, announce: %s", self, run_err, self._tracker_address, self._announce_page) self._is_failed = True except Exception as err: self._logger.exception(u"Failed to process HTTP tracker header: [%s], Tracker: %s," u" Tracker Address: %s, Tracker Announce: %s", err, self._tracker_url, self._tracker_address, self._announce_page) self._logger.debug(u"Header: %s", self._header_buffer) self._is_failed = True return if code != '200': # error response code self._logger.debug(u"%s HTTP SCRAPE error response code [%s, %s]", self, code, msg) self._is_failed = True return # check the content type idx = self._header_buffer.find('Content-Encoding: ') if idx == -1: # assuming it is plain text or something similar self._content_encoding = 'plain' else: encoding = (self._header_buffer[idx:].split('\r\n')[0]).split(' ')[1] self._content_encoding = encoding # get the content length idx = self._header_buffer.find('Content-Length: ') if idx == -1: # assume that the content is small # process the retrieved information success = self._process_scrape_response() if success: self._is_finished = True else: self._is_failed = True else: idx += len('Content-Length: ') self._content_length = int(self._header_buffer[idx:].split('\r\n', 1)[0].strip())
def test_parse_tracker_url_wrong_type_4(self): parse_tracker_url("http://tracker.openbittorrent.com:abc/announce")
def test_parse_tracker_url_wrong_type_3(self): parse_tracker_url("http://tracker.openbittorrent.com:80")
def test_parse_http_no_port(self): result = parse_tracker_url("http://tracker.openbittorrent.com/announce") self.assertEqual(result, (u'http', (u'tracker.openbittorrent.com', 80), u'/announce'))
def test_parse_http_non_standard_port(self): result = parse_tracker_url("http://ipv6.torrent.ubuntu.com:6969/announce") self.assertEqual(result, (u'http', (u'ipv6.torrent.ubuntu.com', 6969), u'/announce'))
def test_parse_http_no_port(self): result = parse_tracker_url( "http://tracker.openbittorrent.com/announce") self.assertEqual(result, (u'http', (u'tracker.openbittorrent.com', 80), u'/announce'))
def test_parse_scheme_correct_udp(self): result = parse_tracker_url("udp://tracker.openbittorrent.com:80") self.assertEqual(result, (u'udp', (u'tracker.openbittorrent.com', 80), u''))