An Example Service: mouse.py

My goal is to have the init system start a script called mouse.py every time the Pi boots to multi-user mode (which hopefully will also mean that the network is available).

If you're following along at home, there's a good chance you don't want to mess around with my entire mouse logging project right now, so I'll write a quick Python script for testing purposes (based on this StackOverflow answer by Nathan Jhaveri).

You can paste or type the code into ~/mouse.py using Nano and mark it executable:

nano /home/pi/mouse.py
#!/usr/bin/python
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write('<:3)~~~')

httpd = SocketServer.TCPServer(("", 80), MyHandler)
httpd.serve_forever()
chmod +x ~/mouse.py

All this does is create a very simple web server that, when you visit the Raspberry Pi's network address, returns an ASCII mouse. To test, I'll check my Pi's IP address, start the script, and use curl to make sure it's running. (I'm using tmux to open more than one shell.)

Your IP address will likely be different, but in my case this looks like so:

ifconfig
sudo ./mouse.py
curl 192.168.0.13

If  you're not on a network, you can just use localhost instead:

And if you want to test in a web browser, just enter the Pi's address in the URL bar:

Now that we have an example service, let's talk about how to start it.

Last updated on Nov 23, 2015 Published on Sep 01, 2015