How to advertise a device on the network using Python

Now everyone can see me!

October 2016.
Python logo Image


You built some IoT device running a Python compliant OS. You want to be able to plug it anywhere, and be able to find it in the network view of Windows (or the equivalent thing of other systems).

Solution: SSDP/UPnP

I've created some code that implements a SSDP server and a HTTP server, in order to notify the network that a device is here.

The base of this code was the SSDP module of coherence.
I took it, converted it to Python 3, and kept only the interesting parts for this project: the parts that responds to `MSEARCH` queries.


The code is attached to this post, and is available as a mirror on Github.

Technical details

First, a SSDP client will try to search devices on the network by issuing a MSEARCH * query:


lib/ will handle it and reply with a 200:

HTTP/1.1 200 OK
SERVER: ZeWaren example SSDP Server
USN: uuid:e427ce1a-3e80-43d0-ad6f-89ec42e46363::upnp:rootdevice
CACHE-CONTROL: max-age=1800
last-seen: 1477147409.432466
ST: upnp:rootdevice
DATE: Sat, 22 Oct 2016 14:44:26 GMT

The client will then fetch the device description:

GET /jambon-3000.xml HTTP/1.1
Cache-Control: no-cache
Connection: Keep-Alive
Pragma: no-cache
Accept: text/xml, application/xml
User-Agent: FDSSDP

And lib/ will build and serve that file:

HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.4.3
Date: Sat, 22 Oct 2016 14:44:26 GMT
Content-type: application/xml

<friendlyName>Jambon 3000</friendlyName>
<manufacturer>Boucherie numrique SAS</manufacturer>
<modelDescription>Jambon Appliance 3000</modelDescription>

The client now knows (nearly) everything about the device.

Relevant links