I had a little itch that needed scratching. This particular itch was that my media server was far away from my bedroom and after a bit of TV or movie watching I did not want to get out of my nice warm bed to turn it off. I usually remote in and shut it down from my ipad however this takes a long time and the last thing you want to do when feeling sleepy is to remember passwords and server addresses.
Python to the rescue.
I wrote (copied and pated from the internet) a small web server that runs python CGI scripts. In this case just 1 script called shutdown.py that does just that, shuts down my media server.
Now all I have to do is open up my web browser on my iPad and click a link.The webserver runs the shutdown script and turns the PC off.
Nice and simple. I have all the files on my home directory and a shortcut in the startup folder to server.pyw to kick it off When you run a .PYW file it runs it silently without a command prompt.
If anyone wants the code it is all below and in the .ZIP file here (Shutdown python script)
This could probably do with some work to get it to output the command if it is successfully or not but it works as it on Windows and can be easily modified for Mac or Unix.
Note: I have updated the below code in shutdown.py to actually work now. It is different to the .ZIP file. Also this will only work with Python 2.7.
If you have Python 3, you will need to replace the "BaseHTTPServer" and "CGIHTTPServer" with "http.server" Nice and simple
import BaseHTTPServer import CGIHTTPServer import cgitb; cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer handler = CGIHTTPServer.CGIHTTPRequestHandler server_address = ("", 9000) handler.cgi_directories = ["/"]
httpd = server(server_address, handler) httpd.serve_forever()
print("Content-Type: text/html\n\n") # html markup follows print(""" <html> <Title>Good bye Dave</Title> <body> <p>Shutting Down PC</p> """)
p = subprocess.Popen('shutdown /s /t 0', shell=False, stdout=None, stderr=None)
print ("""</body> </html> """)