def resultAvailable(target, flags, info): print("got network reachability status for %s:" % (info,)) if flags & kSCNetworkFlagsTransientConnection: print("- transient connection") if flags & kSCNetworkFlagsReachable: print("- reachable") if flags & kSCNetworkFlagsConnectionRequired: print("- connection required") if flags & kSCNetworkFlagsConnectionAutomatic: print("- connection automatic") if flags & kSCNetworkFlagsInterventionRequired: print("- user intervention required") if flags & kSCNetworkFlagsIsLocalAddress: print("- local interface") if flags & kSCNetworkFlagsIsDirect: print("- directly attached network") # And stop the program: loop = CFRunLoopGetCurrent() CFRunLoopStop(loop)
def main(): # Setup a dynamic store controller that monitors all keys: store = SCDynamicStoreCreate(None, "demo.controller", dynamicStoreChanged, None) SCDynamicStoreSetNotificationKeys(store, None, [".*"]) source = SCDynamicStoreCreateRunLoopSource(None, store, 0) # Setup a preferences controller prefs = SCPreferencesCreate(None, "demo.prefs", None) SCPreferencesSetCallback(prefs, prefsChanged, None) # Set up a run loop and add all controllers to that. loop = CFRunLoopGetCurrent() CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) SCPreferencesScheduleWithRunLoop(prefs, loop, kCFRunLoopCommonModes) signal.signal(signal.SIGINT, sigint) CFRunLoopRun()
def main(): if len(sys.argv) >= 2: addr = socket.gethostbyname(sys.argv[1]) else: addr = "82.94.237.218" # www.python.org loop = CFRunLoopGetCurrent() target = SCNetworkReachabilityCreateWithAddress(None, (addr, 80)) SCNetworkReachabilitySetCallback(target, resultAvailable, addr) ok, flags = SCNetworkReachabilityGetFlags(target, None) if ok: resultAvailable(target, flags, addr) else: ok = SCNetworkReachabilityScheduleWithRunLoop(target, loop, kCFRunLoopCommonModes) CFRunLoopRun()
def __init__(self): self._loop = CFRunLoopGetCurrent() self._observer = CFRunLoopObserverCreateWithHandler( None, kCFRunLoopBeforeTimers, True, 0, self._observer_loop) CFRunLoopAddObserver(self._loop, self._observer, kCFRunLoopCommonModes) self._thread = None self._running = False self._closed = False self._timer = None self._timer_q = [] self._transports = weakref.WeakValueDictionary() self._asyncgens = weakref.WeakSet() self._asyncgens_shutdown_called = False self._ready = collections.deque() self._current_handle = None self._timer_cancelled_count = 0 self._exception = None self._task_factory = None self._exception_handler = None # This mirrors how asyncio detects debug mode self._debug = sys.flags.dev_mode or ( not sys.flags.ignore_environment and bool(os.environ.get("PYTHONASYNCIODEBUG"))) self._internal_fds = 0 # self._make_self_pipe() # In debug mode, if the execution of a callback or a step of a task # exceed this duration in seconds, the slow callback/task is logged. self.slow_callback_duration = 0.1 ExceptionHandlerMixin.__init__(self) ExecutorMixin.__init__(self) SocketMixin.__init__(self) SignalMixin.__init__(self)
def sigint(*args): print("SIGINT: bailing out") loop = CFRunLoopGetCurrent() CFRunLoopStop(loop)
if __name__ == '__main__': # log("Initializing") run_at = None if connected(): run_prey() sc_keys = ['State:/Network/Global/IPv4', 'State:/Network/Global/IPv6'] store = SCDynamicStoreCreate(None, "global-network-change", network_state_changed, None) SCDynamicStoreSetNotificationKeys(store, None, sc_keys) CFRunLoopAddSource( # NSRunLoop.currentRunLoop().getCFRunLoop(), CFRunLoopGetCurrent(), SCDynamicStoreCreateRunLoopSource(None, store, 0), kCFRunLoopCommonModes) # signal.signal(signal.SIGHUP, partial(quit, "SIGHUP received")) # NOTE: This timer is basically a kludge around the fact that we can't reliably get # signals or Control-C inside a runloop. This wakes us up often enough to # appear tolerably responsive: CFRunLoopAddTimer( NSRunLoop.currentRunLoop().getCFRunLoop(), CFRunLoopTimerCreate(None, CFAbsoluteTimeGetCurrent(), timer_interval, 0, 0, timer_callback, None), kCFRunLoopCommonModes) try: