FlaPer87 - import freedom; print everything

  • galería de imagenes
  • presentaciones
  • proyectos
  • copyright
  • contacto
  • acerca de...
Home

Google Adsense

Navigation

  • Recent posts

User login

  • Request new password

Communicating with our application using python and dbus

Submitted by flaper87 on Wed, 06/04/2008 - 10:32.

Hi!!

I've been programming the httpServer and the dbusServer for mouseTrap and I wanted to share how simple is communicate with our applications using dbus and its python bindings.

The piece of code we are interested in is this:

import dbus
import gobject
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

main_loop = DBusGMainLoop()
bus = dbus.SessionBus(mainloop=main_loop)

DBUS_NAME = "org.myApp"
DBUS_PATH = "/org/myApp"

class myAppdBus(dbus.service.Object):
    """
    Our dbus Class
    """

    def __init__( self ):
        """
        Starting the dbus service.
        """
        global bus
        bus_name = dbus.service.BusName(DBUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, DBUS_PATH)

    @dbus.service.method(DBUS_NAME)
    def do( self ):
        """
        Function to execute using dbus service
        """
        print "do function has been called using dbus service"


class myApp:

    def __init__(self):
        self.loop = gobject.MainLoop()
        d = myAppdBus()

    def start(self):
        self.loop.run()


if __name__ == '__main__':
    app = myApp()
    app.start()

It is a simple script with 2 classes. The main class called myApp will call the dbus class (myAppdBus) and will start the applications main loop. The second class (myAppdBus) is the dbus class, it starts the service and register the methods that can be called using dbus.

The script can be executed like this (in my case the name of the script is dbus_script.py):


$ python dbus_script.py

After running the script it is possible to execute the do function like this:


$ dbus-send --reply-timeout=30000 --print-reply --dest=org.myApp /org/myApp org.myApp.do

This last command will show us the information related to the call we just executed. The output is something like:

flaper87@r4-p17:~$ dbus-send --reply-timeout=30000 --print-reply --dest=org.myApp
/org/myApp org.myApp.do
method return sender=:1.6 -> dest=:1.7 reply_serial=2

Ass you can the classes are really simple and show an easy way to communicate with our applications using dbus. There are a lot of options a tweaks that can be implemented so it's all in you hands now.

Good Luck.

P.S: Quick post, isn't it? :P

  • communicating
  • dbus
  • English
  • mouseTrap
  • Python
  • Python
  • service

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 19 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Search

  • a11y-en
  • a11y-es
  • Anuncios
  • ArchLinux
  • curiosidades
  • Debian
  • Docbook
  • Drupal
  • English
  • Enlightenment
  • Español
  • Eventos
  • GNU/Linux
  • Italiano
  • Latex
  • mouseTrap
  • opencv
  • Poesias
  • Python
  • Reflexiones
  • Subversion
  • Tiflotecnología
  • Ubuntu
  • Uncategorized
  • Web
  • wordpress

Twitter Updates

Twitter Logo

    follow me on Twitter



    • galería de imagenes
    • presentaciones
    • proyectos
    • copyright
    • contacto
    • acerca de...