[Tutor] Desktop Notifications on Linux

Dharmit Shah dharmit.dev at gmail.com
Thu Apr 10 13:34:52 CEST 2014

Hi Peter,

On Thu, Apr 10, 2014 at 2:30 PM, Peter Otten <__peter__ at web.de> wrote:
> Dharmit Shah wrote:
>> I am trying to create a script that will go through the
>> /var/log/secure file on a Linux system and provide desktop
>> notifications for failed login attempts.
>> Here is the code - http://pastebin.com/MXP8Yd91
>> And here's notification.py - http://pastebin.com/BhsSTP6H
>> I am facing issue in the function "new_attempts_from_last". I am not
>> able to raise a desktop notification from this function. It always
>> fails with this traceback - http://pastebin.com/cgHMScv3
>> I see this error only when I try to raise a notification from the
>> aforementioned function. If I run test examples under
>> /usr/share/doc/notify-python/examples, it works just fine. Also, if I
>> try to raise a notification from under if __name__ == "__main__":, it
>> works without any issues. So I don't think there's any issue with OS's
>> notification daemon. Running from python shell like below also works
>> fine:
>> In [1]: import notification
>> In [2]: notification.notification("Hey")
>> What am I missing or doing wrong here?
>> If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop
>> environment.
>> For readability purposes, I have provided pastebin links. Let me know
>> if this is not the correct way.

Thanks for your prompt response.

> Maybe you are running the code as a user that has no "desktop"? Here's a
> strapped-down demo:
> $ cat notify_demo.py
> import pynotify
> import sys
> pynotify.init("Notification")
> n = pynotify.Notification("Notification", " ".join(sys.argv[1:]))
> n.show()
> $ python notify_demo.py hello world
> The notification appeared on my desktop. However, when trying again as root
> I got the error you are seeing:
> $ sudo su
> # python notify_demo.py hello again
> Traceback (most recent call last):
>   File "notify_demo.py", line 6, in <module>
>     n.show()
> glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name
> org.freedesktop.Notifications was not provided by any .service files

That does ring some bells. I am logged into my F20 system as non-root
user but since reading /var/log/secure file requires superuser
privileges, I am running it as sudo:

  sudo python secure.py

That probably explains the issue I am facing. I will add the user to
the root group and see if it helps.

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list