-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebserver.py
More file actions
81 lines (65 loc) · 2.29 KB
/
webserver.py
File metadata and controls
81 lines (65 loc) · 2.29 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
import threading
from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit
from functools import wraps
from flask import request, Response
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == 'admin' and password == '1234'
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
class Server(threading.Thread):
def __init__(self, jukebox):
threading.Thread.__init__(self)
self.jukebox = jukebox
def run(self):
self.app = Flask(__name__)
self.app.config['SECRET_KEY'] = 'secret!'
self.socketio = SocketIO(self.app)
@self.socketio.on('connect')
def handle_connect():
emit('newSong', self.jukebox.current_song_name)
@self.socketio.on('playback')
def handle_message(playback):
if playback == "pause":
self.jukebox.pause()
elif playback == "play":
self.jukebox.play()
elif playback == "next":
self.jukebox.play_next_song()
return self.jukebox.current_song_name
print(playback)
@self.socketio.on('addToQueue')
def handle_add_to_queue(song):
if self.jukebox.add_to_queue(song):
return True
else:
return False
@self.app.route("/")
def index():
return render_template("queue.html")
@self.app.route("/playstyring")
@requires_auth
def control():
return render_template("index.html")
self.socketio.run(self.app, host='0.0.0.0', port=80)
def new_song(self, song):
with self.app.test_request_context('/'):
self.socketio.send('newSong', song)
if __name__ == '__main__':
server = Server()
server.start()