This commit is contained in:
redfast00 2019-08-22 03:26:15 +02:00
commit 04e29d7ca4
No known key found for this signature in database
GPG key ID: 5946E0E34FD0553C

65
server.py Normal file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env python3
import string
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
import base64
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
def release_keys():
write_report(NULL_CHAR*8)
def printchar(c):
if c.islower():
write_report(NULL_CHAR*2+chr(4 + ord(c) - ord('a'))+NULL_CHAR*5)
elif c.isupper():
write_report(chr(32)+NULL_CHAR+chr(4 + ord(c) - ord('A'))+NULL_CHAR*5)
elif c.isdigit():
write_report(NULL_CHAR*2+chr(0x1e + ((ord(c) - ord('0') - 1) % 10))+NULL_CHAR*5)
elif c == ' ':
write_report(NULL_CHAR*2+chr(44)+NULL_CHAR*5)
elif c == '\n':
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
else:
write_report(chr(32)+NULL_CHAR+chr(0x38)+NULL_CHAR*5)
release_keys()
def change_color(background, foreground):
# DELETE keypress
write_report(NULL_CHAR*2+chr(0x2a)+NULL_CHAR*5)
def printstring(s):
for c in s:
printchar(c)
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, world!')
def do_POST(self):
print(self.headers)
message = base64.b64decode(self.headers['X-Messages']).decode('ascii')
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
self.wfile.write(response.getvalue())
if message == 'reset':
printstring('\n' * 32)
else:
printstring(message + '\n')
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()