Introduction

The process of sending and receiving IP multicast traffic in Python is a relatively easy task. Unfortunately there are few Python examples available, and even less ready-to-use libraries.

py-multicast provides an easy to use interface to receive multicast streams:

    >>> import multicast
    >>> receiver = multicast.MulticastUDPReceiver ("eth0", "238.0.0.1", 1234 )
    >>> receiver.read()
    'stuff sent by multicast'
    >>> receiver.close()    

This will open a socket, bind to interface eth0, join multicast group 238.0.0.1 and listen for traffic incoming on port 1234. Specifying None instead of interface name will bind the socket to the system-default interface for sending multicasts.

It is also possible to define a unicast listener:

    >>> receiver1 = multicast.UDPReceiver ("eth0", 1234 )
    >>> receiver2 = multicast.UDPReceiver ("10.1.0.1", 1234 )

The first argument may be an IP address or interface name (in which case the socket will be bound to the first address on the given interface).
Specifying 0.0.0.0 binds to all interfaces.

There is also a more general way to create the receiver:

    >>> receiver1 = multicast.DatagramReceiver ("239.0.0.1", 1234, "eth0" )
    >>> receiver2 = multicast.DatagramReceiver ("10.1.0.1", 1234 )

The first example creates a multicast receiver, second one is unicast.

There is utility module packed in py-multicast which allows you to query all network interfaces defined in the system:

    >>> from multicast import network
    >>> config = network.ifconfig()
    >>> config['eth0'].addresses
    ['10.0.0.1']
    >>> config['eth0'].multicast
    True
    >>> config['eth0'].up
    True
It may be used to verify if a given interface is up and if it supports multicast.

Redirecting UDP traffic

The package contains a utility script to intercept UDP traffic arriving at the host and redirect it to any other destination. The source/destination can be any combination of unicast/multicast.
The utility is used in a production environment to tunnel a multicast MPEG TS, via internet to another location, where it is again retransmitted as multicast.
    $ udp-redirect.py 10.0.0.1:1234 10.0.0.2:1234
This simply retransmits traffic incoming to 10.0.0.1:1234 to 10.0.0.2:1234.
    $ udp-redirect.py 239.0.0.1:1111@eth0 10.0.0.2:1111
Retransmit multicast traffic on eth0 to 10.0.0.2:1111
    $ udp-redirect.py 10.0.0.2:1111 239.0.0.2:1111@eth1
Retransmit unicast traffic on 10.0.0.2:111 as multicast.
    $ udp-redirect.py 239.0.0.1:1111@eth0 239.0.0.2:1112@eth1
Multicast to multicast example.

Documentation

That's it :) Ask questions below. Read the code :)

Installation

Install from the Python Package Index

  easy_install -U py-multicast

Download a source distribution

Requirements


comments powered by Disqus