Web server on demmand

import sys, BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
 
HandlerClass=SimpleHTTPRequestHandler
ServerClass=BaseHTTPServer.HTTPServer
 
protocol = "HTTP/1.0"
host = "192.168.0.185"
port = 8888
server_address = (host, port)
HandlerClass.protocol_version = protocol
httpd = ServerClass(server_address, HandlerClass)
 
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()

The above example serves all pages/files in the current directory, as long as the host:port combination is accessible.

Great if you want to quickly test javascript or send a document to your iphone!

Switching your monitor off remotely (linux, windows, and mac)

If you want to switch off your monitor from your command line in linux, there is an easy way:

sleep 1 && xset -display :0.0 dpms force off

What if you want to run this command remotely?

Here is my take on this question:
– use Python and Bottle to build a simple web server.
– use “/on” and “/off” pages to run command line scripts that switch the monitor on and off.

Let’s do this in Linux, Windows and Mac.

LINUX

from bottle import route, run
import os
 
@route('/off')
def off():
        os.system("xset -display :0.0 dpms force off")
        return "Screen is now off"
 
@route('/on')
def on():
        os.system("xset -display :0.0 dpms force on")
        return "Screen is now on"
 
run(host='192.168.0.xxx', port=53535)

you just run this server in the background. If you need to switch the monitor off or on, just get any machine within the network and point its browser to these links:

http://192.168.0.xxx:53535/off
http://192.168.0.xxx:53535/on

WINDOWS
To do the same in windows, you can use nircmd, a free utility, to switch off the screen.

To switch on the screen back, I’m using a simple autohotkey script, that just moves the mouse a bit:

MouseMove, 200, 100
MouseMove, 100, 100

Save the above two lines in a text file with the name mouse.ahk, in the same folder as the script below:

from bottle import route, run
import os
 
@route('/off')
def off():
        os.system("nircmd.exe monitor off")
        return "Screen is now off"
 
@route('/on')
def on():
        os.system("mouse.ahk")
        return "Screen is now on"
 
run(host='192.168.0.xxx', port=53535)

The command needs to be in your path (alternatively give it the full path of the nircmd binary).

MAC
with a bit of help from here, the script is as follows:

from bottle import route, run
import os
 
@route('/off')
def off():
        os.system("pmset displaysleepnow")
        return "Screen is now off"
 
@route('/on')
def on():
        os.system("caffeinate -u &")
        return "Screen is now on"
 
run(host='192.168.0.xxx', port=53535)

No security considerations taken into account here; we are assuming you are running this in a secure environment. Depending on your setup, you may have to use administrator/root privileges and make adjustments to your firewall.

The above is part of my IKEA mirror project. Some button combinations will be switching monitors on and off.

Hacking an IKEA mirror [part 5]

I needed a case for the RaspberryPi. The good thing about the RPi being a business card-sized computer is that it fits nicely in a plastic box from old business cards. If you look on Ebay, you can buy 20 of them for £5 including delivery.

The boxes make fantastic custom RPi cases! I didn’t spend too much time and effort in cutting these accurately, but the result is fine for the purpose. All the necessary ports are exposed and the GPio cables have a custom hole.

The board fits snuggly in the box, so I did not bother fixing it. A bit of bluetack was used to stick the plastic box on the metal frame.

Hacking an IKEA mirror [part 1]

In a recent trip to IKEA, I found an interesting and very “hackable ” item: the TYNSES mirror:

hackable mirror from IKEA

hackable mirror from IKEA

[link to IKEA]

I wasn’t really interested in the actual mirror. I was after the nice metal frame. I removed the mirror, which was held in place by 6 stickers and I’m planning on making it a panel on which I will add 8 buttons connected to a raspberry pi. Then the panel will be clamped on my desk, allowing me to run things by pressing a combination of the buttons.

The buttons will be at the bottom part, in two rows. I’m thinking of having an old Nexus at the top, as a mini screen connected to the Raspberry Pi.

I will post more on this later on, as I build it.

Python and PDF

Here is an example of how to merge the first pages of a number of pdf pages into a single file.

I wrote this script, because I needed to print the first page of 200 individual files and I didn’t really want to open each one manually…

 

import os.path, pyPdf
from os import walk
 
output = pyPdf.PdfFileWriter()
original = "c:\\original\\folder\\"
f = []
 
for (dirpath, dirnames, filenames) in walk(original): f.extend(filenames)
 
for eachfile in filenames: 
	ffile = original + "\\" + eachfile
 
	if "pdf" in eachfile:
		pdf = pyPdf.PdfFileReader(open(ffile, "rb"))
		output.addPage(pdf.getPage(0))
	print ffile
 
outputStream = open("c:\\out.pdf", "wb")
output.write(outputStream)
outputStream.close()