Skip to content

Instantly share code, notes, and snippets.

@darkxanter
Last active April 21, 2018 09:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darkxanter/0a93a85780b3696d8699 to your computer and use it in GitHub Desktop.
Save darkxanter/0a93a85780b3696d8699 to your computer and use it in GitHub Desktop.
Python DBus handle hibernate, sleep and resume
#!/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