/
server.py
executable file
·40 lines (30 loc) · 1.02 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
from os import remove as remove_file
from os.path import exists as file_exists
from sys import argv as args
from subprocess import PIPE, STDOUT, run as run_command
from xmlrpc.server import SimpleXMLRPCServer as RPCServer
from contextlib import suppress
def create_file(filename, data):
data = data.data
with open(filename, "wb") as f:
f.write(data)
def delete_file(filename):
if file_exists(filename):
return remove_file(filename)
def call(command):
command.insert(0, "avrdude")
res = run_command(command, stdout=PIPE, stderr=STDOUT)
return (res.returncode, res.stdout.decode("ascii"))
def main():
address = args[1].split(":")
address = (address[0], int(address[1]))
rpc = RPCServer(address, allow_none=True)
for f in (create_file, delete_file, call):
rpc.register_function(f)
print("Listening on %s:%s" % address)
with suppress(KeyboardInterrupt):
rpc.serve_forever()
print("\nBye!")
if __name__ == "__main__":
main()