In some situation,you may need CGI server show some message or icons when receiving a request from the client.Take OSD notification as an example,when the client sending an access request,CGI server will display useful information right now.
#!/usr/bin/env python
#-*-coding=utf-8-*-
import cgi
import os
print 'Content-Type:text/html\n\n'
os.system("notify-send 'hello' 2>/tmp/notify-error")
|
But it do not work.And
notify-send 'hello' works well in gnome-terminal.I check the log file /tmp/notify-error
djstava@Gateway:/tmp$ cat notify-error
libnotify-Message: Unable to get session bus: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.
** (notify-send:4642): CRITICAL **: dbus_g_proxy_connect_signal: assertion `DBUS_IS_G_PROXY (proxy)' failed
** (notify-send:4642): CRITICAL **: dbus_g_proxy_connect_signal: assertion `DBUS_IS_G_PROXY (proxy)' failed
** (notify-send:4642): CRITICAL **: dbus_g_proxy_call: assertion `DBUS_IS_G_PROXY (proxy)' failed
libnotify-Message: Unable to get session bus: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.
** (notify-send:4642): CRITICAL **: dbus_g_proxy_disconnect_signal: assertion `DBUS_IS_G_PROXY (proxy)' failed
** (notify-send:4642): CRITICAL **: dbus_g_proxy_disconnect_signal: assertion `DBUS_IS_G_PROXY (proxy)' failed
(notify-send:4642): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed
|
That means notify-send cannot connect current X session.So I add the DISPLAY variable as the following.
os.system("DISPLAY=:0.0 notify-send 'hello' 2>/tmp/notify-error")
|
But the error remains.
Suddenly I remebered some days ago I use the python module pynotify to realize the OSD notification.Why not have a try?I download python-notify from
#!/usr/bin/env python
#-*-coding=utf-8-*-
import cgi
import os
print 'Content-Type:text/html\n\n'
try:
import pynotify
if pynotify.init("cgi script"):
n = pynotify.Notification("Title", "message")
n.show()
else:
print "There was a problem initializing the pynotify module"
except:
print "Have not pynotify installed"
|
I check the apache2 error.log,error as same as before in great disappointment.
An idea flashs in my mind,the apache2 default user
www-data and my system username
djstava is not the same
one.Maybe this is the problem.Then I change the apache2's default user
www-data to my system user
djstava,and
it works,ONLY need the $DISPLAY.
djstava
阅读(2174) | 评论(0) | 转发(0) |