-
Notifications
You must be signed in to change notification settings - Fork 4
/
ino_web_server.py
77 lines (63 loc) · 2.9 KB
/
ino_web_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# credit: http://sheep.art.pl/Wiki%20Engine%20in%20Python%20from%20Scratch
import BaseHTTPServer, urllib, re, os
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
template = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><html><body><h1>Arduino INO web server</h1>To upload to an Arduino board connected to this computer, POST to /.</body></html>"""
def escape_html(self, text):
"""Replace special HTML characters with HTML entities"""
return text.replace(
"&", "&").replace(">", ">").replace("<", "<")
def do_HEAD(self):
"""Send response headers"""
self.send_response(200)
self.send_header("content-type", "text/html;charset=utf-8")
self.end_headers()
def do_GET(self):
"""Send page text"""
self.do_HEAD()
self.wfile.write(self.template)
def do_POST(self):
"""Save new page text and display it"""
length = int(self.headers.getheader('content-length'))
if length:
text = self.rfile.read(length)
print "sketch to upload: " + text
# create ino project (if it doesn't exist already)
os.system("mkdir ino_project")
os.chdir("ino_project")
rc = os.system("ino init")
# 32512 probably means ino is not installed
if rc == 32512:
print "ino init returned " + `rc`
self.send_response(501)
else:
# write to file
fo = open("src/sketch.ino", "wb")
fo.write(text + "\n");
fo.close()
print "created src/sketch.ino"
# invoke ino to build/upload
# skip_lib_includes is used to avoid "line too long" errors with IDE 1.5.8+
rc = os.system("ino build --skip_lib_includes")
# 512 probably means invalid option (skip_lib_includes)
if rc == 512:
print "ino build returned " + `rc` + " - trying again without skip_lib_includes"
rc = os.system("ino build")
if not rc == 0:
print "ino build returned " + `rc`
self.send_response(400)
else:
rc = os.system("ino upload")
if not rc == 0:
print "ino upload returned " + `rc`
self.send_response(500)
else:
self.send_response(200)
if __name__ == '__main__':
print "running local web server at 127.0.0.1:8080..."
server = BaseHTTPServer.HTTPServer(("127.0.0.1", 8080), Handler)
server.pages = {}
server.serve_forever()