Skip to content

Commit 6aedb38

Browse files
committed
Create server.py
Very simple server to help working with the .tpl files stored in ESP8266/data Serves files on port 8080 For now, the server only understands $INCLUDE[filename.inc]$ type statements (with filename.inc in the same folder as the .tpl file)
1 parent 77dd4ab commit 6aedb38

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

server.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
import SimpleHTTPServer, SocketServer
5+
6+
#Replace this with a different path if you need to...
7+
base_path = os.path.join(os.getcwd(),"ESP8266","data")
8+
9+
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
10+
def do_GET(self):
11+
if self.path.endswith(".tpl"):
12+
self.send_response(301)
13+
self.send_header("Content-type", "text/html")
14+
self.end_headers()
15+
16+
data = self.process(self.path)
17+
self.wfile.write(data)
18+
self.wfile.close()
19+
return
20+
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
21+
22+
def process(self,fn):
23+
if fn.startswith("/") or fn.startswith("\\"):
24+
fn = fn[1:]
25+
fn = os.path.join(base_path,os.path.normpath(fn))
26+
fpath,_ = os.path.split(fn)
27+
lines = open(fn).read()
28+
lines = lines.split("\n")
29+
n_lines = len(lines)
30+
i = 0
31+
while i < n_lines:
32+
line = lines[i].strip()
33+
if line.startswith("$INCLUDE["):
34+
p0 = line.find("[")+1
35+
p1 = line.find("]")
36+
if p0 < 0 or p0 == len(line) or p1 < 0:
37+
continue
38+
fn_inc = os.path.join(fpath,line[p0:p1])
39+
if not os.path.exists(fn_inc):
40+
i = i+1
41+
continue
42+
43+
lines_inc = open(fn_inc).read()
44+
lines_inc = lines_inc.split("\n")
45+
if i < n_lines-1:
46+
lines1 = lines[i+1:]
47+
else:
48+
lines1 = []
49+
lines = lines[:i]+lines_inc+lines1
50+
n_lines = len(lines)
51+
else:
52+
i = i+1
53+
return "\n".join(lines)
54+
55+
if __name__ == '__main__':
56+
print "="*60
57+
print "Serving files from:"
58+
print base_path
59+
os.chdir(base_path)
60+
print "="*60
61+
handler = MyHandler
62+
server = SocketServer.TCPServer(("",8080), handler)
63+
server.serve_forever()

0 commit comments

Comments
 (0)