Python DBus handle hibernate, sleep and resume
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = 'xanter' | |
#from datetime import datetime | |
import signal | |
import time | |
import dbus | |
import gobject | |
import urllib2 | |
from dbus.mainloop.glib import DBusGMainLoop | |
def sigterm_handler(_signo, _stack_frame): | |
print(_signo) | |
gobject.idle_add(quit) | |
def tryopen(): | |
try: | |
urllib2.urlopen("http://ya.ru/") | |
print("success open") | |
except Exception as e: | |
print(e) | |
def handle_sleep(mode): | |
# mode = args[0] | |
if mode: | |
print("Sleep") | |
tryopen() | |
else: | |
print("Resume") | |
time.sleep(10) | |
tryopen() | |
#print "%s PrepareForSleep%s" % (datetime.now().ctime(), args) | |
DBusGMainLoop(set_as_default=True) # integrate into gobject main loop | |
bus = dbus.SystemBus() # connect to system wide dbus | |
bus.add_signal_receiver( # define the signal to listen to | |
handle_sleep, # callback function | |
'PrepareForSleep', # signal name | |
'org.freedesktop.login1.Manager', # interface | |
'org.freedesktop.login1' # bus name | |
) | |
signal.signal(signal.SIGINT, sigterm_handler) | |
signal.signal(signal.SIGTERM, sigterm_handler) | |
# signal.signal(signal.SIGKILL, sigterm_handler) | |
loop = gobject.MainLoop() | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment