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
|
#!/usr/bin/python
import time
import os
import BaseHTTPServer
import urlparse
import pickle
import urllib
HOST_NAME = '0.0.0.0'
PORT_NUMBER = 8000
VLM_PORT=9000 # change this according to your vlm setting.
try:
storage = pickle.load(open('emul.data'))
except IOError:
storage = {}
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
"""Respond to a GET request."""
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
try:
self._parse()
except Exception,ex:
self.wfile.write("Error: %s\n" % type(ex))
self.wfile.write(" : %s\n" % ex.message)
def _parse(self):
url = urlparse.urlparse(self.path)
params = dict([part.split('=') for part in url[4].split('&')])
if params['class']=='Storage':
if params['action'] == 'put':
storage.update({params['name']:params['content']})
self.wfile.write('ok')
if params['action'] == 'list':
for k in storage.keys():
self.wfile.write('%s=%s\n'% (k,storage[k]))
if params['action'] == 'get':
self.wfile.write(storage[params['name']])
self.wfile.write('\n')
if params['action'] == 'delete':
try:
storage.pop(params['name'])
self.wfile.write('ok')
except KeyError:
self.wfile.write('unknow key %s' % params['name'])
if params['class'] =='Media':
if params['action'] == 'play':
source = params['source']
cmd='/requests/status.xml?command=in_play&input=%s' % source
self._sendVLC(cmd)
if params['action'] == 'pause':
cmd='/requests/status.xml?command=pl_pause'
self._sendVLC(cmd)
if params['action'] == 'stop':
cmd='/requests/status.xml?command=pl_stop'
self._sendVLC(cmd)
def _sendVLC(self,cmd):
dest_url='http://localhost:%s%s' % (VLM_PORT,cmd)
try:
z = urllib.urlretrieve(dest_url)
except IOError:
print "Please check that VLM is running on port %s" % VLM_PORT
else:
self.wfile.write('ok')
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
output = open('emul.data', 'wb')
pickle.dump(storage, output)
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) |
Partager