Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- Very simple HTTP server in python for logging requests
- Usage::
- ./server.py [<port>]
- """
- from http.server import BaseHTTPRequestHandler, HTTPServer
- import logging
- import broadlink
- IR_TOKEN = 0x26
- RF_433_TOKEN = 0xB2
- RF_315_TOKEN = 0xD7
- commands = {
- # Command #1: IrScrutinizer captured signal; Protocol: nec1, Parameters: D=210 S=109 F=4
- 'poweron': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111111111111133111111111111111111111133113311111133113311331133113311000499'),
- bytearray.fromhex('00010F4311000B65'),
- bytearray()
- ],
- 'poweroff': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113311111111113311111133113311111111113311331111113311331111113311331133111111111111113311111111111111111133113311331111113311000499'),
- bytearray.fromhex('00010F4311000B65'),
- bytearray()
- ],
- 'dvd': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113311111111113311111133113311111111113311331111113311331111111111111133113311111111111111331133113311111111113311331133111111000499'),
- bytearray.fromhex('00010F4311000B65'),
- bytearray()
- ],
- 'line2': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111113311331133113311111111111111111111111111111111113311331133113311000499'),
- bytearray.fromhex('00010F4311000B65'),
- bytearray()
- ],
- 'tape1': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111111111111111113311111111111111111133113311331111113311331133113311000499'),
- bytearray.fromhex('00010F4311000B66'),
- bytearray()
- ],
- 'tape2': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113411111111113411111134113411341111113411341111113411341111113411341134111111111111111111111111111111111134113411341134113411000499'),
- bytearray.fromhex('00010F4411000B61'),
- bytearray()
- ],
- 'cd': [
- IR_TOKEN,
- bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111113311111111113311111111111111111111113311331111113311331133113311000499'),
- bytearray.fromhex('00010F4311000B66'),
- bytearray()
- ],
- }
- def get_command_data(command_name, count):
- cmd = commands[command_name]
- buffer = bytearray()
- buffer.append(cmd[0])
- repeat_only = len(cmd[1]) == 0 and len(cmd[3]) == 0
- buffer.append(count - 1 if repeat_only else 0)
- seq = cmd[2] if repeat_only else mk_sequence(cmd, count)
- buffer.append(len(seq) % 256)
- buffer.append(len(seq) / 256)
- return buffer + seq
- def mk_sequence(cmd, count):
- no_repeats = count if len(cmd[1]) == 0 else count - 1
- data = cmd[1]
- for i in range(0, no_repeats):
- data = data + cmd[2]
- return data + cmd[3]
- def auto_int(x):
- return int(x, 0)
- class S(BaseHTTPRequestHandler):
- def _set_response(self):
- self.send_response(200)
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- def do_GET(self):
- logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
- self._set_response()
- self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
- if self.path == "/" :
- self.wfile.write("use one of the commands".encode('utf-8'))
- else :
- self.wfile.write("command:".encode('utf-8'))
- type = 0x2737
- host = "192.168.1.1"
- mac = bytearray.fromhex("abcdef")
- dev = broadlink.gendevice(type, (host, 80), mac)
- #dev = broadlink.rm(type, (host, 80), mac)
- dev.auth()
- payload = get_command_data('poweroff', 1)
- dev.send_data(payload)
- self.wfile.write("command send".encode('utf-8'))
- self.wfile.write("done".encode('utf-8'))
- def do_POST(self):
- content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
- post_data = self.rfile.read(content_length) # <--- Gets the data itself
- logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
- str(self.path), str(self.headers), post_data.decode('utf-8'))
- self._set_response()
- self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
- def run(server_class=HTTPServer, handler_class=S, port=8082):
- logging.basicConfig(level=logging.INFO)
- server_address = ('', port)
- httpd = server_class(server_address, handler_class)
- logging.info('Starting httpd...\n')
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- pass
- httpd.server_close()
- logging.info('Stopping httpd...\n')
- if __name__ == '__main__':
- from sys import argv
- if len(argv) == 2:
- run(port=int(argv[1]))
- else:
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement