-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpiserver.py
179 lines (122 loc) · 4.8 KB
/
rpiserver.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import time
import datetime
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import subprocess
## Create HTTPServer to change date and time on rpi when rpi is not coonected to the internet
## Also allow to reboot or shutdown rpi when there is no keyboard
## Change ip addess to match
## Raspberry Pi W address and
## select port
##
## Use a web browser and type:
## http://192.188.9.191:8008
##
host_name = '192.168.8.191'
host_port = 8008
class MyHandler(BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
BaseHTTPRequestHandler.__init__(self, request, client_address, server)
self.protocol_version = 'HTTP/1.1'
self.newdate = ""
self.newtime = ""
self.myTime = datetime.datetime.now()
self.path = "/"
def rpiShutdown (self):
cmd = "sudo shutdown -h now"
#cmd = "ls -la"
pp=subprocess.Popen ( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
#print pp
def rpiReboot (self):
cmd = "sudo shutdown -r now"
#cmd = "ls -la"
pp=subprocess.Popen ( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
#print pp
def setRpiDatetime (self, req):
cmd = 'sudo date -s "%s"' % (req)
#cmd = "ls -la"
pp=subprocess.Popen (cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
#print pp
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def _redirect(self, path):
self.send_response(303)
self.send_header('Content-type', 'text/html')
self.send_header('Location', path)
self.end_headers()
def do_GET(self):
myDateTime = str(datetime.datetime.now())
mydate, mytime = myDateTime.split()
datestr = '''
date (yyyy-mm-dd): <input type="text" name="dtdate" value="%s"><br><br>
time (hh:mm:ss.usec): <input type="text" name="dttime" value="%s"><br><br>
<input type="submit" name="subdate" value="change"> Apply date time<br>
''' % (mydate, mytime)
rpistr = '''
rpi: <input type="submit" name="rpishutdown" value="off"> sudo shutdown -h now<br><br>
rpi: <input type="submit" name="rpireboot" value="reboot"> sudo shutdown -r now<br><br>
'''
temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read()
html = '''
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<h1>RPI</h1>
cpu %s
<br>
<form action="/" method="POST">
<br>
<br>
<br>
%s
<br>
<br>
<br>
%s
</form>
</body>
</html>
''' % ( temp, datestr,rpistr)
self.do_HEAD()
self.wfile.write(html.encode("utf-8"))
def do_POST(self):
content_length = int(self.headers['Content-Length']) # Get the size of data
post_data = self.rfile.read(content_length).decode("utf-8") # Get the data
post_req = post_data.split("&")
#print post_req
count = len (post_req)
for ii in range (0,count):
#print "request: ", post_req[ii]
if post_req[ii] == 'subdate=change':
datetime = self.newdate + " " + self.newtime
#print "changing RPI datetime to:", datetime
self.setRpiDatetime(datetime)
elif post_req[ii] == 'rpishutdown=off':
self.rpiShutdown()
elif post_req[ii] == 'rpireboot=reboot':
self.rpiReboot()
else:
if "dtdate=" in post_req[ii]:
self.newdate = post_req[ii].replace ("dtdate=","").replace("+"," ").replace("%3A", ":")
print "saving new date", self.newdate
if "dttime=" in post_req[ii]:
self.newtime = post_req[ii].replace ("dttime=","").replace("+"," ").replace("%3A", ":")
print "saving new time", self.newtime
pass
self._redirect('/') # Redirect back to the root url
def run ():
server_address = (host_name, host_port)
print("Server Starts - %s:%s" % (host_name, host_port))
httpd = HTTPServer(server_address, MyHandler)
httpd.serve_forever()
try:
http_server.serve_forever()
except KeyboardInterrupt:
print "KeyboardInterrupt"
http_server.server_close()
while(True):
try:
run()
except:
time.sleep(30)