-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP.py
More file actions
115 lines (107 loc) · 4.07 KB
/
HTTP.py
File metadata and controls
115 lines (107 loc) · 4.07 KB
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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import SonosControl
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
## def do_GET(self):
## s = SonosControl.getStatus()
## if s == "PLAYING":
## SonosControl.pause()
## self.send_response(808)
## self.send_header('Content-type', 'text/html')
## self.end_headers()
## print "paused"
## else:
## SonosControl.play()
## self.send_response(505)
## self.send_header('Content-type', 'text/html')
## self.end_headers()
## print "playing"
## self.wfile.write("<html><body><h1>hi!</h1></body></html>")
def do_HEAD(self):
self._set_headers()
def do_POST(self):
request_path = self.path
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
text = request_headers.getheaders('Data')
locate = request_headers.getheaders('Location')
print locate
if text[0]=='Sync':
SonosControl.setZoneVolumeEqual()
#ADD Play same song
if locate[0] =="ALL":
print (text)
if text[0]=='Play':
SonosControl.playAll()
print "playing"
elif text[0]=='Pause':
SonosControl.pauseAll()
print "paused"
elif text[0]=='Volume+':
SonosControl.volumeUpAll()
elif text[0]=='Volume-':
SonosControl.volumeDownAll()
elif text[0]=='Next':
SonosControl.play_nextAll()
elif text[0]=='Prev':
SonosControl.play_previousAll()
elif text[0]=='IsPlaying':
self.send_response(200 + SonosControl.isPlayingAll())
self.send_header('Content-type', 'text/html')
self.end_headers()
return
else:
print "Not programmed"
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
return
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
else:
print (text)
if text[0]=='Play':
SonosControl.play(locate[0])
print "playing " + locate[0]
elif text[0]=='Pause':
SonosControl.pause(locate[0])
print "paused"
elif text[0]=='Volume+':
SonosControl.volumeUp(locate[0])
elif text[0]=='Volume-':
SonosControl.volumeDown(locate[0])
elif text[0]=='Next':
SonosControl.play_next(locate[0])
elif text[0]=='Prev':
SonosControl.play_previous(locate[0])
elif text[0]=='IsPlaying':
self.send_response(200 + SonosControl.isPlaying(locate[0]))
self.send_header('Content-type', 'text/html')
self.end_headers()
return
else:
print "Not programmed"
self.send_response(404)
self.send_header('Content-type', 'text/html')
self.end_headers()
return
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def run(server_class=HTTPServer, handler_class=S, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting httpd...'
httpd.serve_forever()
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()