def turn_off_internet(verbose=False):
 global INTERNET_OFF
 global _orig_opener
 if INTERNET_OFF:
  return
 INTERNET_OFF = True
 __tracebackhide__ = True
 if verbose:
  print(u'Internet access disabled')
 _orig_opener = urllib.request.build_opener()
 no_proxy_handler = urllib.request.ProxyHandler({})
 opener = urllib.request.build_opener(no_proxy_handler)
 urllib.request.install_opener(opener)
 socket.create_connection = check_internet_off(socket_create_connection)
 socket.socket.bind = check_internet_off(socket_bind)
 socket.socket.connect = check_internet_off(socket_connect)
 return socket

def turn_off_internet ( verbose = False ) : global INTERNET_OFF global _orig_opener if INTERNET_OFF : return INTERNET_OFF = True __tracebackhide__ = True if verbose : print ( u'Internet access disabled' ) _orig_opener = urllib . request . build_opener ( ) no_proxy_handler = urllib . request . ProxyHandler ( { } ) opener = urllib . request . build_opener ( no_proxy_handler ) urllib . request . install_opener ( opener ) socket . create_connection = check_internet_off ( socket_create_connection ) socket . socket . bind = check_internet_off ( socket_bind ) socket . socket . connect = check_internet_off ( socket_connect ) return socket
disable internet access via python by preventing connections from being created using the socket module .