Category Archives: Python Programming

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:

Managing Gunicorn Processes With Supervisor

Last week, I have written a post about gunicorn applications. We started gunicorn manually, and our application worked. Yay!

However, everything is not so great. When (If) the server reboots, gunicorn must be started manually, again. We should find a way to automate this. Actually, there are few ways to accomplish this, such as: init scripts, and supervisord.

Init scripts are more than enough. But they have some drawbacks. They are not so easy to write or maintain. If you have several projects, things get even harder. Supervisord is a relatively easier way for managing multiple gunicorn processes.

Let’s start with installing supervisor:

sudo apt-get install supervisor

Supervisor uses configuration files for applications located in /etc/supervisor/conf.d/ directory. The configuration for our application is below:

[program:hello]
command = gunicorn hello:app
directory = /home/username/
user = username

This is a very basic configuration required to run gunicorn. Since we are using virtualenv, we need to change “command” parameter to use python and gunicorn from our environment instead of global ones.

command = /home/username/hello/bin/python /home/username/hello/bin/gunicorn hello:app

Now we can test, whether our configuration works or does not. Reload supervisor with following commands and start our application. Stop gunicorn if it is running, and start it again with supervisor.

supervisorctl reread
supervisorctl update
supervisorctl start hello

Now gunicorn must be running, you can make sure by visiting page. If server reboots, supervisor starts it. If gunicorn fails, supervisor restarts it. Finally, everything is great!

You can find more information about supervisor configuration parameters in the documentation. You can also read my blog post about restarting and reloading supervisor.

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.

WikiLeaks Cablegate Count

Today, I wrote a small script to get the count of released WikiLeaks cables. Actually, it’s extremely small. Just two lines of code.

import urllib, piksemel, re

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

Generally, I don’t code like this. But I wanted to write it quick (5 minutes?) and keep it short. It uses piksemel to parse the page.

Enjoy!

How to install piksemel module

piksemel is a easy to use python XML parser, based on iksemel.

Not: If you’re using Pardus, piksemel is preinstalled on your system.

Not: If you’re using Windows, before you continue make sure you’ve already installed mingw32 to your system and its directory is included in PATH.

Steps for Linux users:

  • Download the package from http://cekirdek.pardus.org.tr/~bahadir/piksemel/piksemel-1.3.1.tar.gz and unpack it.
  • Run:
    python setup.py build
  • Run:
    python setup.py install

Steps for Windows users:

  • Download the package from http://cekirdek.pardus.org.tr/~bahadir/piksemel/piksemel-1.3.1.tar.gz and unpack it.
  • In piksemel-1.3.1 directory, run:
    setup.py bdist_wininst build --compiler=mingw32
  • Run the installer file under the piksemel-1.3.1/dist directory.