Advertisement
easternnl

Broadlink Python Webserver

Apr 19th, 2018
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. Very simple HTTP server in python for logging requests
  4. Usage::
  5.    ./server.py [<port>]
  6. """
  7. from http.server import BaseHTTPRequestHandler, HTTPServer
  8. import logging
  9. import broadlink
  10.  
  11. IR_TOKEN = 0x26
  12. RF_433_TOKEN = 0xB2
  13. RF_315_TOKEN = 0xD7
  14.  
  15. commands = {
  16.     # Command #1: IrScrutinizer captured signal; Protocol: nec1, Parameters: D=210 S=109 F=4
  17.     'poweron': [
  18.         IR_TOKEN,
  19.         bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111111111111133111111111111111111111133113311111133113311331133113311000499'),
  20.         bytearray.fromhex('00010F4311000B65'),
  21.         bytearray()
  22.     ],
  23.     'poweroff': [
  24.         IR_TOKEN,
  25.         bytearray.fromhex('00010F881111113311111111113311111133113311111111113311331111113311331111113311331133111111111111113311111111111111111133113311331111113311000499'),
  26.         bytearray.fromhex('00010F4311000B65'),
  27.         bytearray()
  28.     ],
  29.      'dvd': [
  30.         IR_TOKEN,
  31.         bytearray.fromhex('00010F881111113311111111113311111133113311111111113311331111113311331111111111111133113311111111111111331133113311111111113311331133111111000499'),
  32.         bytearray.fromhex('00010F4311000B65'),
  33.         bytearray()
  34.     ],
  35.     'line2': [
  36.         IR_TOKEN,
  37.         bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111113311331133113311111111111111111111111111111111113311331133113311000499'),
  38.         bytearray.fromhex('00010F4311000B65'),
  39.         bytearray()
  40.     ],
  41.     'tape1': [
  42.         IR_TOKEN,
  43.         bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111111111111111113311111111111111111133113311331111113311331133113311000499'),
  44.         bytearray.fromhex('00010F4311000B66'),
  45.         bytearray()
  46.     ],
  47.     'tape2': [
  48.         IR_TOKEN,
  49.         bytearray.fromhex('00010F881111113411111111113411111134113411341111113411341111113411341111113411341134111111111111111111111111111111111134113411341134113411000499'),
  50.         bytearray.fromhex('00010F4411000B61'),
  51.         bytearray()
  52.     ],
  53.     'cd': [
  54.         IR_TOKEN,
  55.         bytearray.fromhex('00010F881111113311111111113311111133113311331111113311331111113311331111113311111111113311111111111111111111113311331111113311331133113311000499'),
  56.         bytearray.fromhex('00010F4311000B66'),
  57.         bytearray()
  58.     ],
  59.    
  60. }
  61.  
  62. def get_command_data(command_name, count):
  63.     cmd = commands[command_name]
  64.     buffer = bytearray()
  65.     buffer.append(cmd[0])
  66.     repeat_only = len(cmd[1]) == 0 and len(cmd[3]) == 0
  67.     buffer.append(count - 1 if repeat_only else 0)
  68.     seq = cmd[2] if repeat_only else mk_sequence(cmd, count)
  69.     buffer.append(len(seq) % 256)
  70.     buffer.append(len(seq) / 256)
  71.     return buffer + seq
  72.  
  73.  
  74. def mk_sequence(cmd, count):
  75.     no_repeats = count if len(cmd[1]) == 0 else count - 1
  76.     data = cmd[1]
  77.     for i in range(0, no_repeats):
  78.         data = data + cmd[2]
  79.     return data + cmd[3]
  80.  
  81.  
  82. def auto_int(x):
  83.     return int(x, 0)
  84.  
  85. class S(BaseHTTPRequestHandler):
  86.     def _set_response(self):
  87.         self.send_response(200)
  88.         self.send_header('Content-type', 'text/html')
  89.         self.end_headers()
  90.  
  91.     def do_GET(self):
  92.         logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
  93.         self._set_response()
  94.         self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
  95.         if self.path == "/" :
  96.             self.wfile.write("use one of the commands".encode('utf-8'))
  97.         else :
  98.             self.wfile.write("command:".encode('utf-8'))    
  99.             type = 0x2737
  100.             host = "192.168.1.1"
  101.             mac = bytearray.fromhex("abcdef")
  102.             dev = broadlink.gendevice(type, (host, 80), mac)
  103.             #dev = broadlink.rm(type, (host, 80), mac)
  104.             dev.auth()
  105.             payload = get_command_data('poweroff', 1)
  106.             dev.send_data(payload)
  107.             self.wfile.write("command send".encode('utf-8'))
  108.            
  109.         self.wfile.write("done".encode('utf-8'))    
  110.  
  111.     def do_POST(self):
  112.         content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
  113.         post_data = self.rfile.read(content_length) # <--- Gets the data itself
  114.         logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
  115.                 str(self.path), str(self.headers), post_data.decode('utf-8'))
  116.  
  117.         self._set_response()
  118.         self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
  119.  
  120. def run(server_class=HTTPServer, handler_class=S, port=8082):
  121.     logging.basicConfig(level=logging.INFO)
  122.     server_address = ('', port)
  123.     httpd = server_class(server_address, handler_class)
  124.     logging.info('Starting httpd...\n')
  125.     try:
  126.         httpd.serve_forever()
  127.     except KeyboardInterrupt:
  128.         pass
  129.     httpd.server_close()
  130.     logging.info('Stopping httpd...\n')
  131.  
  132. if __name__ == '__main__':
  133.     from sys import argv
  134.  
  135.     if len(argv) == 2:
  136.         run(port=int(argv[1]))
  137.     else:
  138.         run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement