def lookup(name, samAddr=_defaultSAMAddr): """ lookup an i2p name :returns i2p.datatypes.Destination: """ if isinstance(name, datatypes.Destination): # if it's already a destination return it :p return name # create new socket for lookup sock = None try: sock = pysocket.create_connection(samAddr) repl = _sam_cmd(sock, _greeting()) if repl.opts['RESULT'] != 'OK': # fail to handshake sock.close() raise pysocket.error(errno.EAGAIN, "cannot connect to i2p router") except pysocket.timeout as ex: raise ex except pysocket.error as ex: raise ex else: assert sock is not None # do lookup repl = _sam_cmd(sock, "NAMING LOOKUP NAME={}".format(name)) # close socket as it is not needed anymore sock.close() if 'VALUE' in repl.opts: dest = datatypes.Destination(raw=repl.opts['VALUE'], b64=True) return dest else: # failed to lookup raise pysocket.herror(errno.EAGAIN, "name not resolved: {}".format(name))
def lookup(name, samAddr=_defaultSAMAddr): """ lookup an i2p name :returns i2p.datatypes.Destination: """ sock = pysocket.socket() if isinstance(name, datatypes.Destination): # if it's already a destination return it :p return name # create new socket for lookup sock = pysocket.socket() try: sock.connect(_samAddr) repl = _sam_cmd(sock, 'HELLO VERSION MIN=3.0 MAX=3.2') if repl.opts['RESULT'] != 'OK': # fail to handshake sock.close() raise pysocket.error(errno.EAGAIN, "cannot connect to i2p router") except pysocket.timeout as ex: raise ex except pysocket.error as ex: raise ex else: # do lookup repl = _sam_cmd(sock, "NAMING LOOKUP NAME={}".format(name)) # close socket as it is not needed anymore sock.close() if 'VALUE' in repl.opts: dest = datatypes.Destination(raw=repl.opts['VALUE'], b64=True) return dest else: # failed to lookup raise pysocket.herror(errno.EAGAIN, "name not resolved: {}".format(name))
def lookup(name, samAddr=_defaultSAMAddr, sock=None): """ lookup an i2p name :returns i2p.datatypes.Destination: """ if isinstance(name, datatypes.Destination): # if it's already a destination return it :p return name doclose = sock is None # create new socket for lookup if sock is None: try: sock = pysocket.create_connection(samAddr) repl = _sam_cmd(sock, _greeting()) if repl.opts['RESULT'] != 'OK': # fail to handshake sock.close() raise pysocket.error(errno.EAGAIN, "cannot connect to i2p router") except pysocket.timeout as ex: raise ex except pysocket.error as ex: raise ex assert sock is not None # do lookup repl = _sam_cmd(sock, "NAMING LOOKUP NAME={}".format(name)) # close socket as it is not needed anymore if doclose: sock.close() if 'VALUE' in repl.opts: dest = datatypes.Destination(raw=repl.opts['VALUE'], b64=True) return dest else: # failed to lookup raise pysocket.herror(errno.EAGAIN, "name not resolved: {}".format(name))
def test_raises_herror(self, msocket): msocket.socket.return_value = self.sock msocket.herror = socket.herror self.sock.connect.side_effect = socket.herror(1, 'foo') r = simplestatus.check_host('foo.bar', 80) self.assertFalse(r[0]) self.assertEqual('[Errno 1] foo', r[1])
def mock_gethostbyaddr(addr): if addr == resolvable_ip: return ("unittestServer", None, None) elif addr == unresolvable_ip1: raise socket.gaierror() elif addr == unresolvable_ip2: raise socket.herror() else: raise socket.timeout()
def testGetOnGce_NotInMemory_NotOnDisk_CheckServerErrors(self): errors = (six.moves.urllib.error.URLError(''), six.moves.urllib.error.HTTPError(None, None, None, None, None), socket.timeout(''), socket.error(''), socket.herror(''), socket.gaierror(''), six.moves.http_client.BadStatusLine('')) for error in errors: self._SetUpServerResponse(errors=error) on_gce_cache = gce_cache._OnGCECache() self.assertIs(on_gce_cache.GetOnGCE(), False) self.assertIs(on_gce_cache.connected, False) self.AssertFileExistsWithContents('False', self.tempfilepath)
def gethostbyaddr(self, addr: str) -> Tuple[str, List[str], List[str]]: """Simulate socket.gethostbyaddr""" try: hostname = self.by_addr[ip_address(addr)] except KeyError: raise socket.herror(1, "Unknown host") # The FQDN might not be the primary host name # Return short host name as the primary, with other variants as aliases alias_iter = accumulate(hostname.split("."), lambda *sides: ".".join(sides)) return next(alias_iter), list(alias_iter), [addr]
def test_checkReverseLookup(self, gethostname_mock, gethostbyname_mock, getfqdn_mock): gethostname_mock.return_value = "test" gethostbyname_mock.side_effect = ["123.123.123.123", "123.123.123.123"] getfqdn_mock.return_value = "test.example.com" hostInfo = HostInfo() self.assertTrue(hostInfo.checkReverseLookup()) gethostbyname_mock.assert_any_call("test.example.com") gethostbyname_mock.assert_any_call("test") self.assertEqual(2, gethostbyname_mock.call_count) gethostbyname_mock.side_effect = ["123.123.123.123", "231.231.231.231"] self.assertFalse(hostInfo.checkReverseLookup()) gethostbyname_mock.side_effect = ["123.123.123.123", "123.123.123.123"] getfqdn_mock.side_effect = socket.herror() self.assertFalse(hostInfo.checkReverseLookup())
def lookup(self, name): """ look up a name :param name: a name or b32 address :returns an i2p.datatypes.Destination instance: """ if isinstance(name, datatypes.Destination): # if it's already a destination return it :p return name # check cache if name in self._dest_cache: # cache hit return self._dest_cache[name] # cache miss, do lookup # create new socket for lookup sock = None try: sock = pysocket.create_connection(self._samAddr) self._samHandshake(sock) except pysocket.timeout as ex: raise ex except pysocket.error as ex: raise ex else: # do lookup repl = _sam_cmd(sock, "NAMING LOOKUP NAME={}".format(name)) # close socket as it is not needed anymore sock.close() if 'VALUE' in repl.opts: dest = datatypes.Destination(raw=repl.opts['VALUE'], b64=True) if not name.endswith(".b32.i2p"): # cache name as it's not a b32 address self._dest_cache[name] = dest # cache base32 address self._dest_cache[dest.base32()] = dest return dest else: # failed to lookup raise pysocket.herror(errno.EAGAIN, "name not resolved: {}".format(name))
def get_web_server(): """Find the web server for this host on the LIGO Data Grid. @returns the fully-qualified domain name of the web server associated with this host. Example: >>> socket.getfqdn() ldas-pcdev1.ligo-la.caltech.edu >>> htmlutils.get_web_server() "https://ldas-jobs.ligo-la.caltech.edu" """ # get fully qualified domain name (FQDN) fqdn = socket.getfqdn() # work out web root if re.search("ligo.caltech", fqdn): url = "https://ldas-jobs.ligo.caltech.edu" elif re.search("ligo-wa", fqdn): url = "https://ldas-jobs.ligo-wa.caltech.edu" elif re.search("ligo-la", fqdn): url = "https://ldas-jobs.ligo-la.caltech.edu" elif re.search("atlas", fqdn): match = re.search("atlas[13]", fqdn) if match: host = match.group() url = "https://%s.atlas.aei.uni-hannover.de" % host else: url = "https://atlas1.atlas.aei.uni-hannover.de" elif re.search("phy.syr", fqdn): url = "https://sugar-jobs.phy.syr.edu" elif re.search("astro.cf", fqdn): url = "https://gravity.astro.cf.ac.uk" elif re.search("phys.uwm", fqdn): url = "https://ldas-jobs.phys.uwm.edu" else: raise socket.herror("No web domain known for host %s." % fqdn) return url
def lookup(self, name): """ look up a name :param name: a name or b32 address :returns an i2p.datatypes.Destination instance: """ if isinstance(name, datatypes.Destination): # if it's already a destination return it :p return name # check cache if name in self._dest_cache: # cache hit return self._dest_cache[name] # cache miss, do lookup # create new socket for lookup sock = pysocket.socket() try: sock.connect(self._samAddr) self._samHandshake(sock) except pysocket.timeout as ex: raise ex except pysocket.error as ex: raise ex else: # do lookup repl = _sam_cmd(sock, "NAMING LOOKUP NAME={}".format(name)) # close socket as it is not needed anymore sock.close() if 'VALUE' in repl.opts: dest = datatypes.Destination(raw=repl.opts['VALUE'], b64=True) if not name.endswith(".b32.i2p"): # cache name as it's not a b32 address self._dest_cache[name] = dest # cache base32 address self._dest_cache[dest.base32()] = dest return dest else: # failed to lookup raise pysocket.herror(errno.EAGAIN, "name not resolved: {}".format(name))
def get_web_server(): """Find the web server for this host on the LIGO Data Grid. @returns the fully-qualified domain name of the web server associated with this host. Example: >>> socket.getfqdn() ldas-pcdev1.ligo-la.caltech.edu >>> htmlutils.get_web_server() "https://ldas-jobs.ligo-la.caltech.edu" """ # get fully qualified domain name (FQDN) fqdn = socket.getfqdn() # work out web root if re.search("ligo.caltech", fqdn): url = "https://ldas-jobs.ligo.caltech.edu" elif re.search("ligo-wa", fqdn): url = "https://ldas-jobs.ligo-wa.caltech.edu" elif re.search("ligo-la", fqdn): url = "https://ldas-jobs.ligo-la.caltech.edu" elif re.search("atlas", fqdn): match = re.search("atlas[13]", fqdn) if match: host = match.group() url = "https://%s.atlas.aei.uni-hannover.de" % host else: url = "https://atlas1.atlas.aei.uni-hannover.de" elif re.search("phy.syr", fqdn): url = "https://sugar-jobs.phy.syr.edu" elif re.search("astro.cf", fqdn): url = "https://gravity.astro.cf.ac.uk" elif re.search("phys.uwm", fqdn): url = "https://ldas-jobs.phys.uwm.edu" else: raise socket.herror("No web domain known for host %s." % fqdn) return url
def test__resolve_ip_err(mocker): m = mocker.patch("wmfmariadbpy.dbutil.socket.gethostbyaddr") m.side_effect = socket.herror() with pytest.raises(ValueError): dbutil._resolve_ip(ipaddress.ip_address("1.1.1.1"))
def impl(addr): if addr not in ipaddrlist: raise socket.herror() return (hostname, [], ipaddrlist)
class RequestTests(TestCase): def test_from_http_request(self): http_request = HttpRequest() http_request.method = "PATCH" http_request.path = "/kylef" http_request.META["REMOTE_ADDR"] = "32.64.128.16" http_request.META["HTTP_USER_AGENT"] = "test user agent" http_request.META["HTTP_REFERER"] = "https://fuller.li/" http_response = HttpResponse(status=204) request = Request() request.from_http_request(http_request, http_response, commit=False) self.assertEqual(request.path, "/kylef") self.assertEqual(request.method, "PATCH") self.assertEqual(request.ip, "32.64.128.16") self.assertEqual(request.response, 204) self.assertEqual(request.user_agent, "test user agent") self.assertEqual(request.referer, "https://fuller.li/") def test_from_http_request_with_user(self): http_request = HttpRequest() http_request.method = "GET" http_request.user = User.objects.create(username="******") request = Request() request.from_http_request(http_request, commit=False) self.assertEqual(request.user.id, http_request.user.id) def test_from_http_request_redirection(self): http_request = HttpRequest() http_request.method = "GET" http_response = HttpResponse(status=301) http_response["Location"] = "/foo" request = Request() request.from_http_request(http_request, http_response, commit=False) self.assertEqual(request.redirect, "/foo") def test_from_http_request_not_commit(self): http_request = HttpRequest() http_request.method = "GET" request = Request() request.from_http_request(http_request, commit=False) self.assertIsNone(request.id) def test_str_conversion(self): request = Request(method="PATCH", path="/", response=204) request.time = datetime.now() self.assertEqual(str(request), "[{}] PATCH / 204".format(request.time)) def test_browser_detection_with_no_ua(self): request = Request(method="GET", path="/", response=200) self.assertEqual(request.browser, None) def test_browser_detection_with_no_path(self): request = Request( user_agent= "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0" ) self.assertEqual(request.browser, "Firefox") request = Request( user_agent= "Mozilla/5.0 (compatible; MSIE 9.0; America Online Browser 1.1; Windows NT 5.0)" ) self.assertEqual(request.browser, "AOL") def test_determining_search_keywords_with_no_referer(self): request = Request() self.assertEqual(request.keywords, None) def test_determining_search_keywords(self): request = Request( referer= "https://www.google.com/search?client=safari&rls=en&q=querykit+core+data&ie=UTF-8&oe=UTF-8" ) self.assertEqual(request.keywords, "querykit core data") @mock.patch("metrics.models.gethostbyaddr", return_value=("foo.net", [], ["1.2.3.4"])) def test_hostname(self, *mocks): request = Request(ip="1.2.3.4") self.assertEqual(request.hostname, "foo.net") @mock.patch("metrics.models.gethostbyaddr", side_effect=socket.herror(2, "Host name lookup failure")) def test_hostname_invalid(self, *mocks): request = Request(ip="1.2.3.4") self.assertEqual(request.hostname, request.ip) def test_save(self): request = Request(ip="1.2.3.4") request.save() @mock.patch("metrics.models.request_settings.LOG_IP", False) def test_save_not_log_ip(self): request = Request(ip="1.2.3.4") request.save() self.assertEqual(settings.IP_DUMMY, request.ip) @mock.patch("metrics.models.request_settings.ANONYMOUS_IP", True) def test_save_anonymous_ip(self): request = Request(ip="1.2.3.4") request.save() self.assertTrue(request.ip.endswith(".1")) @mock.patch("metrics.models.request_settings.LOG_USER", False) def test_save_not_log_user(self): user = User.objects.create(username="******") request = Request(ip="1.2.3.4", user=user) request.save() self.assertIsNone(request.user) def test_get_user(self): user = User.objects.create(username="******") request = Request.objects.create(ip="1.2.3.4", user=user) self.assertEqual(request.get_user(), user)
def reverse_resolve(self, target_ip): for name, candidate_ip in self.dns.items(): if not isIPAddress(name) and candidate_ip == target_ip: return defer.succeed(name) return defer.fail(socket.herror())
class RequestTests(TestCase): def test_from_http_request(self): http_request = HttpRequest() http_request.method = 'PATCH' http_request.path = '/kylef' http_request.META['REMOTE_ADDR'] = '32.64.128.16' http_request.META['HTTP_USER_AGENT'] = 'test user agent' http_request.META['HTTP_REFERER'] = 'https://fuller.li/' http_response = HttpResponse(status=204) request = Request() request.from_http_request(http_request, http_response, commit=False) self.assertEqual(request.path, '/kylef') self.assertEqual(request.method, 'PATCH') self.assertEqual(request.ip, '32.64.128.16') self.assertEqual(request.response, 204) self.assertEqual(request.user_agent, 'test user agent') self.assertEqual(request.referer, 'https://fuller.li/') def test_from_http_request_with_user(self): http_request = HttpRequest() http_request.method = 'GET' http_request.user = User.objects.create(username='******') request = Request() request.from_http_request(http_request) self.assertEqual(request.user.id, http_request.user.id) def test_from_http_request_redirection(self): http_request = HttpRequest() http_request.method = 'GET' http_response = HttpResponse(status=301) http_response['Location'] = '/foo' request = Request() request.from_http_request(http_request, http_response) self.assertEqual(request.redirect, '/foo') def test_from_http_request_not_commit(self): http_request = HttpRequest() http_request.method = 'GET' request = Request() request.from_http_request(http_request, commit=False) self.assertIsNone(request.id) def test_str_conversion(self): request = Request(method='PATCH', path='/', response=204) request.time = datetime.now() self.assertEqual(str(request), '[{}] PATCH / 204'.format(request.time)) def test_browser_detection_with_no_ua(self): request = Request(method='GET', path='/', response=200) self.assertEqual(request.browser, None) def test_browser_detection_with_no_path(self): request = Request(user_agent='Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0') self.assertEqual(request.browser, 'Firefox') def test_determining_search_keywords_with_no_referer(self): request = Request() self.assertEqual(request.keywords, None) def test_determining_search_keywords(self): request = Request( referer='https://www.google.com/search?client=safari&rls=en&q=querykit+core+data&ie=UTF-8&oe=UTF-8' ) self.assertEqual(request.keywords, 'querykit core data') @mock.patch('request.models.gethostbyaddr', return_value=('foo.net', [], ['1.2.3.4'])) def test_hostname(self, *mocks): request = Request(ip='1.2.3.4') self.assertEqual(request.hostname, 'foo.net') @mock.patch('request.models.gethostbyaddr', side_effect=socket.herror(2, 'Host name lookup failure')) def test_hostname_invalid(self, *mocks): request = Request(ip='1.2.3.4') self.assertEqual(request.hostname, request.ip) def test_save(self): request = Request(ip='1.2.3.4') request.save() @mock.patch('request.models.request_settings.REQUEST_LOG_IP', False) def test_save_not_log_ip(self): request = Request(ip='1.2.3.4') request.save() self.assertEqual(settings.REQUEST_IP_DUMMY, request.ip) @mock.patch('request.models.request_settings.REQUEST_ANONYMOUS_IP', True) def test_save_anonymous_ip(self): request = Request(ip='1.2.3.4') request.save() self.assertTrue(request.ip.endswith('.1')) @mock.patch('request.models.request_settings.REQUEST_LOG_USER', False) def test_save_not_log_user(self): user = User.objects.create(username='******') request = Request(ip='1.2.3.4', user=user) request.save() self.assertIsNone(request.user) def test_get_user(self): user = User.objects.create(username='******') request = Request.objects.create(ip='1.2.3.4', user=user) self.assertEqual(request.get_user(), user)
def get_hostame(self): """Obtener el hostname para la dirección, si éste se encuentra disponible """ if not self.is_valid: raise socket.herror('unknown host') return self.hostname
def fake_hostbyaddr(*args, **kwargs): raise socket.herror("This is a test :-)")
def sockshostbyaddr(addr): raise socket.herror(1, "Unknown host")
def fake_getattrinfo(*args, **kwargs): raise socket.herror("This is a test :-)")