我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用errno.ENETUNREACH。
def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = test_support.find_unused_port() with self.assertRaises(socket.error) as cm: socket.create_connection((HOST, port)) # Issue #16257: create_connection() calls getaddrinfo() against # 'localhost'. This may result in an IPV6 addr being returned # as well as an IPV4 one: # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), # (26, 2, 0, '', ('::1', 41230, 0, 0))] # # create_connection() enumerates through all the addresses returned # and if it doesn't successfully bind to any of them, it propagates # the last exception it encountered. # # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. expected_errnos = [ errno.ECONNREFUSED, ] if hasattr(errno, 'ENETUNREACH'): expected_errnos.append(errno.ENETUNREACH) self.assertIn(cm.exception.errno, expected_errnos)
def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = support.find_unused_port() with self.assertRaises(socket.error) as cm: socket.create_connection((HOST, port)) # Issue #16257: create_connection() calls getaddrinfo() against # 'localhost'. This may result in an IPV6 addr being returned # as well as an IPV4 one: # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), # (26, 2, 0, '', ('::1', 41230, 0, 0))] # # create_connection() enumerates through all the addresses returned # and if it doesn't successfully bind to any of them, it propagates # the last exception it encountered. # # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. expected_errnos = [ errno.ECONNREFUSED, ] if hasattr(errno, 'ENETUNREACH'): expected_errnos.append(errno.ENETUNREACH) self.assertIn(cm.exception.errno, expected_errnos)
def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. port = support.find_unused_port() with self.assertRaises(OSError) as cm: socket.create_connection((HOST, port)) # Issue #16257: create_connection() calls getaddrinfo() against # 'localhost'. This may result in an IPV6 addr being returned # as well as an IPV4 one: # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), # (26, 2, 0, '', ('::1', 41230, 0, 0))] # # create_connection() enumerates through all the addresses returned # and if it doesn't successfully bind to any of them, it propagates # the last exception it encountered. # # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. expected_errnos = [ errno.ECONNREFUSED, ] if hasattr(errno, 'ENETUNREACH'): expected_errnos.append(errno.ENETUNREACH) self.assertIn(cm.exception.errno, expected_errnos)
def test_errno(self): """ L{error.getConnectError} converts based on errno for C{socket.error}. """ self.assertErrnoException(errno.ENETUNREACH, error.NoRouteError) self.assertErrnoException(errno.ECONNREFUSED, error.ConnectionRefusedError) self.assertErrnoException(errno.ETIMEDOUT, error.TCPTimedOutError) if platformType == "win32": self.assertErrnoException(errno.WSAECONNREFUSED, error.ConnectionRefusedError) self.assertErrnoException(errno.WSAENETUNREACH, error.NoRouteError)
def test_connect_timeout(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(0.1) gs = greenio.GreenSocket(s) try: expect_socket_timeout(gs.connect, ('192.0.2.1', 80)) except socket.error as e: # unreachable is also a valid outcome if not get_errno(e) in (errno.EHOSTUNREACH, errno.ENETUNREACH): raise
def test_connect_ex_timeout(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(0.1) gs = greenio.GreenSocket(s) e = gs.connect_ex(('192.0.2.1', 80)) if e not in (errno.EHOSTUNREACH, errno.ENETUNREACH): self.assertEqual(e, errno.EAGAIN)
def main(): global HOST, PORT, EOF, MAXTRIES global s, tries, loops try: s.connect((HOST, PORT)) while True: # Measure timing using GPIO4 risetime = RCtime(4) # Send to the connected socket # (as we're using UDP, we must # send separate messages) s.sendall('foo %s%s' % (loops, EOF)) s.sendall('bar %s%s' % (risetime, EOF)) # Advance counter loops = loops + 1 except socket.error as err: errcode = err[0] if errcode==errno.ECONNREFUSED: print 'Connection refused by host!' elif errcode==errno.ENETDOWN: print 'No network connection!' elif errcode==errno.ENETUNREACH: print 'Network unreachable!' elif errcode==errno.ENETRESET: print 'Network dropped connection!' elif errcode==errno.ECONNRESET: print 'Connection reset by peer!' elif errcode==errno.EHOSTDOWN: print 'Host is down!' elif errcode==errno.EHOSTUNREACH: print 'No route to host!' else: print 'Caught: %s!' % err if tries >= MAXTRIES: GPIO.cleanup() s.close() print 'No connection. Exiting.' else: print 'Tried %i of %i times.\nWaiting %is...' % (tries, MAXTRIES, tries/10) time.sleep(tries/10) tries = tries + 1 main() except KeyboardInterrupt: GPIO.cleanup() s.close() print '%i loops' % loops