PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/buildroot-2016.02/board/raspberrypi/rootfs_overlay/root/musicplayer.py

https://gitlab.com/Rakowski/linsw_zad4
Python | 85 lines | 65 code | 20 blank | 0 comment | 6 complexity | 6c85ba92c63d15867844e71e67820d40 MD5 | raw file
  1. import os
  2. import tornado.ioloop
  3. import tornado.web
  4. import base64
  5. from subprocess import call
  6. FILES = '/mnt/files'
  7. FIFO = '/root/mplayer_fifo'
  8. MM = None
  9. class MplayerManager:
  10. def send(self, msg):
  11. os.system('echo "' + msg + '" > ' + FIFO)
  12. def setVolume(self, val):
  13. send('volume ' + val + '1')
  14. def toggleMute(self):
  15. send('mute')
  16. def pause(self):
  17. send('pause')
  18. def stop(self):
  19. send('stop')
  20. class Index(tornado.web.RequestHandler):
  21. def get(self):
  22. files = []
  23. for filename in os.listdir(FILES):
  24. files.append(filename)
  25. self.render('index.html', files=files)
  26. def post(self):
  27. auth_hdr = self.request.headers.get('Authorization')
  28. print auth_hdr
  29. if auth_hdr == None or not auth_hdr.startswith('Basic '):
  30. return self.auth()
  31. auth_decoded = base64.decodestring(auth_hdr[6:])
  32. username, password = auth_decoded.split(':', 2)
  33. if username != "ja" or password != "ja":
  34. return self.auth()
  35. f = self.request.files['datafile'][0]
  36. filename = f['filename']
  37. print "Upload request with file: " + filename
  38. x = open(FILES + filename, 'w')
  39. x.write(f['body'])
  40. x.close()
  41. self.redirect("/")
  42. def auth(self):
  43. self.set_status(401)
  44. self.set_header('WWW-Authenticate', 'Basic realm=Fileserver')
  45. self.finish()
  46. return False
  47. class VolumeChanged(tornado.web.RequestHandler):
  48. def post(self, vol):
  49. MM.setVolume(vol)
  50. class MuteToggled(tornado.web.RequestHandler):
  51. def post(self):
  52. MM.toggleMute()
  53. class Paused(tornado.web.RequestHandler):
  54. def post(self):
  55. MM.pause()
  56. if __name__ == "__main__":
  57. MM = MplayerManager()
  58. webserv = tornado.web.Application([
  59. (r"/", Index),
  60. (r"/volume_changed/(1?[0-9]?[0-9])", VolumeChanged),
  61. (r"/mute/", MuteToggled),
  62. (r"/pause/", Paused)
  63. ])
  64. webserv.listen(80)
  65. tornado.ioloop.IOLoop.instance().start()