Last active
March 4, 2017 13:27
-
-
Save elpatron68/0026a1aa6ed47643499c21391cc5b757 to your computer and use it in GitHub Desktop.
Simple http server for remotely setting Windows into hybernate mode or shutting it down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Simple http server for remotely set Windows into hybernate mode or shutting it down | |
# (c) 2017 Markus Busche, [email protected] | |
# | |
# Requirements | |
# I tested this script with Python 3.6. It should also run with Python 2.x with some slight modifications. | |
# No 3rd party packages are required. | |
# | |
# Usage: | |
# Start this script on your Windows machine and let in run in background. | |
# | |
# - To set your machine into hybernate mode: Open http://<IP-Adress of your PC>/hybernate | |
# - To shutdown your PC: Open http://<IP-Adress of your PC>/shutdown | |
# | |
# To run this script as a Windows service, follow the instructions on Stack Overflow. Answer #2 worked fine for me. | |
# https://stackoverflow.com/questions/8666373/start-python-py-as-a-service-in-windows | |
# | |
# My personal useage: | |
# I use this script to voice-control my Windows machine with Amazons Alexa and HA-Bridge (https://github.com/bwssytems/ha-bridge) | |
import http.server | |
import socketserver | |
import subprocess | |
PORT = 8081 | |
# Shutdown Timeout in seconds | |
TIMEOUT = '60' | |
# Set DEBUGMODE = True for simulation | |
DEBUGMODE = False | |
class MyRequestHandler(http.server.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
command = '' | |
if self.path == '/hybernate': | |
if DEBUGMODE == True: | |
print('Received hybernate command') | |
command = '/h' | |
if self.path == '/shutdown': | |
if DEBUGMODE == True: | |
print('Received shutdown command') | |
command = '/s /f /t ' + TIMEOUT + ' /c "Received remotely triggered shutdown command"' | |
if command != '': | |
try: | |
if DEBUGMODE == False: | |
subprocess.Popen('shutdown.exe ' + command) | |
else: | |
print('shutdown.exe ' + command) | |
self.send_response(200) | |
except: | |
self.send_response(400) | |
else: | |
self.send_response(404) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
Handler = MyRequestHandler | |
server = socketserver.TCPServer(('0.0.0.0', PORT), Handler) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment