Tag Archives: python

Quick Guide to D-Bus with Python

Recent weeks, I dealt with D-Bus for my Google Summer of Code project. I was trying to control system services, and systemd has a D-Bus interface. In the beginning, D-Bus seemed a little confusing for me, but I believe I understand it now. In this post I will try to explain it in simple terms and an example.

D-Bus is a interprocess communication tool. It delivers messages to applications. The most confusing thing for me was that taxonomy of D-Bus having several terms such as bus names, objects, paths, methods, signals, properties, interfaces, proxies… But when you learn their meanings, the bigger picture gets more clear.

  • Bus names are applications. They often have well known names in a format similar to namespaces in Java (e.g “com.onurguzel.MyIntergalacticApplication”).
  • Objects are synonymous to the objects in programming languages, and paths are the object instances.
  • Methods are functions that is run on the object, and signals are the broadcasts from the object. Properties are the constants or variables in the object.
  • Interfaces are the types of the object. Thus, some object may support several interfaces. Their format is the same as bus names, but they should not be mixed with each other.
  • Proxies are higher level objects. When a method is invoked on the proxy, it is converted to D-Bus method call message, sent, and reply message is returned. They offer easier operation with D-Bus then manually creating messages, sending them and waiting for a reply.

Since all major Linux distributions use NetworkManager for network connections, I preferred to create this tutorial with it.

import dbus

bus = dbus.SystemBus()

# Warning: Bus names and interfaces are different terms.
# Just because they contain same format or even same data
# does not mean they are the same thing.
# I used these variables to denote the diffrance where bus names and
# interfaces are used.
NM_BUSNAME = 'org.freedesktop.NetworkManager'
NM_IFACE = 'org.freedesktop.NetworkManager'

# Create Python object from /org/freedesktop/NetworkManager instance
# in org.freedesktop.NetworkManager application.
nm = bus.get_object(NM_BUSNAME, '/org/freedesktop/NetworkManager')

# Get list of active connections from the properties
# "Get" method is in "org.freedesktop.DBus.Properties" interface
# It takes the interface name which has the property and name of
# the property as arguments.

connections = nm.Get(NM_BUSNAME, 'ActiveConnections',
                     dbus_interface=dbus.PROPERTIES_IFACE)

# While invoking a method on the object, dbus_interface keyword
# argument is provided to specify which interface has the method.

# Now, lets disconnect. This time we are using the NetworkManager
# interface: "org.freedesktop.NetworkManager"
for path in connections:
    nm.DeactivateConnection(path, dbus_interface=NM_IFACE)

If you have questions about this tutorial, you can ask in the comments section below. For more information about D-Bus, my references:

How to Run Flask Applications with Nginx Using Gunicorn

We have recently bought a VPS for İTÜ24, the online newsletter of Istanbul Technical University. The server is running on Ubuntu Server 12.04 operating system. Due to limited memory resources and performance concerns, we preferred to setup nginx as web server.

Our server will serve several web pages and applications developed in various programming languages, such as PHP, Python, Ruby (on Rails). Currently, we have one Python application, which is using Flask framework.

How we run Flask application with nginx, step by step…
Continue reading

WikiLeaks Cablegate Count v2

Since the official WikiLeaks page has changed, the previous script needs to be updated. Here it is:

import urllib, piksemel, re

wleaks = int(re.search(r"(d+) / [0-9,]+", piksemel.parseString(urllib.urlopen("http://www.wikileaks.ch/cablegate.html").read()).getTag("body").getTag("div").getTag("div").getTag("a").nextTag().getTag("p").toString()).groups()[0])

Please remember to install piksemel module.

Show forecast using Yahoo! Weather

Note: This snippet uses piksemel module to parse XML files. If you don’t have it, read this article to install it.

First of all, you need the WOEID (Where on Earth Identification) of the city. In order to learn it, go to http://weather.yahoo.com and search for the city. You’ll find the WOEID at the end of the url of the page.

# for Istanbul, TR
woeid = 2344116

Use this id in Yahoo! Weather API url, for w parameter.

weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d" % woeid

If you want the results to be in metric units, add u=c parameter into your query.

weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d&u=c" % woeid

Using urllib, fetch the XML file:

import urllib

xml = urllib.urlopen(weatherUri).read()

We can parse this XML easily using piksemel:

import piksemel

temp = piksemel.parseString(xml).getTag('channel').getTag('item').getTag('yweather:condition').getAttribute('temp')

You can also view XML file and look for other resources such as: humidity, visibility, wind, etc.

yatp: yet another transfer protocol

In the beginning of March, I started to a new project with my room-mate Diego.

The main problem is sharing. We shouldn’t be sending e-mails to our friends to share a link, a document file, a presentation or some pictures. We shouldn’t try to memorize a TinyURL to tell a friend. Instant messengers? Eww! We hate those, who doesn’t?

Instant messengers are often useful. However there are many alternatives. Most of them requires you to be online to receive messages, and your online status will be public to your contacts. Most of them uses more then 30 MB of your computer memory. You cannot use them in a network which has no internet connection. Lots of disadvantages.

There’s also no easy way to share files. Websites providing file sharing services make you to much, enter a CAPTCHA image, then wait again. You cannot share a file even if you’re in the same network. OS differences, lack of configuration, bugs, security holes…

We are planning to create a new service based on HTTP to simplify sharing things between friends, especially whose computers are in the same network. We called it yatp, yet another transfer protocol, even it’s not a protocol. We did some brainstorming and created a website. More details will be announced later. And do not forget to visit: yatp.net

Sharing will be fun.