Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wait(self):
while True:
read_fds, write_fds = self.channel.getsock()
if not read_fds and not write_fds:
break
timeout = self.channel.timeout()
if timeout == 0.0:
self.channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
continue
rlist, wlist, xlist = select.select(read_fds, write_fds, [], timeout)
for fd in rlist:
self.channel.process_fd(fd, pycares.ARES_SOCKET_BAD)
for fd in wlist:
self.channel.process_fd(pycares.ARES_SOCKET_BAD, fd)
def wait(self):
while True:
read_fds, write_fds = self.channel.getsock()
if not read_fds and not write_fds:
break
timeout = self.channel.timeout()
if timeout == 0.0:
self.channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
continue
rlist, wlist, xlist = select.select(read_fds, write_fds, [], timeout)
for fd in rlist:
self.channel.process_fd(fd, pycares.ARES_SOCKET_BAD)
for fd in wlist:
self.channel.process_fd(pycares.ARES_SOCKET_BAD, fd)
def test_channel_local_ip(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(timeout=5.0, tries=1, servers=['8.8.8.8'], local_ip='127.0.0.1')
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ECONNREFUSED)
def test_channel_nameservers(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(timeout=5.0, tries=1, servers=['8.8.8.8'])
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertNoError(self.errorno)
def test_channel_timeout(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(timeout=0.5, tries=1)
self.channel.gethostbyname('google.com', socket.AF_INET, cb)
timeout = self.channel.timeout()
self.assertTrue(timeout > 0.0)
self.channel.cancel()
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ECANCELLED)
def test_idna_encoding(self):
host = 'españa.icom.museum'
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
# try encoding it as utf-8
self.channel.gethostbyname(host.encode(), socket.AF_INET, cb)
self.wait()
self.assertEqual(self.errorno, pycares.errno.ARES_ENOTFOUND)
self.assertEqual(self.result, None)
# use it as is (it's IDNA encoded internally)
self.channel.gethostbyname(host, socket.AF_INET, cb)
self.wait()
self.assertNoError(self.errorno)
self.assertEqual(type(self.result), pycares.ares_host_result)
def test_query_timeout(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.servers = ['1.2.3.4']
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ETIMEOUT)
def test_search(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(timeout=5.0, tries=1, domains=['google.com'])
self.channel.search('cloud', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertNoError(self.errorno)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_a_result)
self.assertNotEqual(r.host, None)
def test_custom_resolvconf(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(tries=1, timeout=2.0, resolvconf_path=os.path.join(FIXTURES_PATH, 'badresolv.conf'))
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ETIMEOUT)
def test_query_a_rotate(self):
self.result, self.errorno = None, None
self.errorno_count, self.count = 0, 0
def cb(result, errorno):
self.result, self.errorno = result, errorno
if errorno:
self.errorno_count += 1
self.count += 1
self.channel = pycares.Channel(timeout=1.0, tries=1, rotate=True)
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.count, 3)
self.assertEqual(self.errorno_count, 0)