def requires_network(test):
 def _is_unreachable_err(err):
  return (getattr(err, 'errno', None) in (errno.ENETUNREACH, errno.EHOSTUNREACH))
 def _has_route():
  try:
   sock = socket.create_connection((TARPIT_HOST, 80), 0.0001)
   sock.close()
   return True
  except socket.timeout:
   return True
  except socket.error as e:
   if _is_unreachable_err(e):
    return False
   else:
    raise
 @functools.wraps(test)
 def wrapper(*args, **kwargs):
  global _requires_network_has_route
  if (_requires_network_has_route is None):
   _requires_network_has_route = _has_route()
  if _requires_network_has_route:
   return test(*args, **kwargs)
  else:
   msg = "Can't run {name} because the network is unreachable".format(name=test.__name__)
   raise SkipTest(msg)
 return wrapper

def requires_network ( test ) : def _is_unreachable_err ( err ) : return ( getattr ( err , 'errno' , None ) in ( errno . ENETUNREACH , errno . EHOSTUNREACH ) ) def _has_route ( ) : try : sock = socket . create_connection ( ( TARPIT_HOST , 80 ) , 0.0001 ) sock . close ( ) return True except socket . timeout : return True except socket . error as e : if _is_unreachable_err ( e ) : return False else : raise @ functools . wraps ( test ) def wrapper ( * args , ** kwargs ) : global _requires_network_has_route if ( _requires_network_has_route is None ) : _requires_network_has_route = _has_route ( ) if _requires_network_has_route : return test ( * args , ** kwargs ) else : msg = "Can't run {name} because the network is unreachable" . format ( name = test . __name__ ) raise SkipTest ( msg ) return wrapper
helps you skip tests that require the network .