-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·153 lines (109 loc) · 4.13 KB
/
server.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
#!/usr/bin/python
import liblo, sys, os, getopt
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import urlparse, parse_qs
# use globals for server settings
HTTP_IP = ''
HTTP_PORT = 8080
OSC_IP = 'localhost'
OSC_PORT = 10000
global OSC_target
def usage():
"""Print out usage options."""
print """
Simple HTTP-to-OpenSoundControl server.
Parses querystring from '/send-osc/' request and sends them via OSC messages, otherwise acts as file server (be careful, this way you are exposing your filesystem --> ment only for closed LAN usage).
Example: for request '/send-osc/?user1=bob&user2=alice' server will send OSC messages '/user1':'bob' and '/user2':'alice'.
Options:
[-h | --help] : prints this help
[-l | --listen] IP_ADDR[:PORT] : start http server on IP_ADDR:PORT; default: listens on all interfaces on port 8080
[-o | --osc_dest] IP_ADDR[:PORT] : send OSC messages to IP_ADDR:PORT; default: sends to localhost on port 10000
"""
return
def init(argv=None):
"""Parses arguments and sets global options."""
# set globals
global HTTP_IP
global HTTP_PORT
global OSC_IP
global OSC_PORT
# get args
try:
args, opts = getopt.getopt(argv, 'hl:o:', ['help', 'listen=', 'osc_dest='])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
for o, a in args:
# help
if o in ('-h', '--help'):
usage()
sys.exit()
# http server listen
elif o in ('-l', '--listen'):
try:
HTTP_IP, HTTP_PORT = a.split(':')
HTTP_PORT = int(HTTP_PORT)
except:
HTTP_IP = a
# osc destionation
elif o in ('-o', '--osc_dest'):
try:
OSC_IP, OSC_PORT = a.split(':')
OSC_PORT = int(OSC_PORT)
except:
OSC_IP = a
else:
assert False, "unhandled option"
return
class MyHandler(BaseHTTPRequestHandler):
"""HTTP handler for simple file serving and parsing '/send-osc/' request to OSC."""
def do_GET(self):
# parse GET path
url_object = urlparse(self.path)
# if 'submit' parse querystring and send OSC messages
if (url_object[2] == '/send-osc/'):
# parse querystring ...
params = parse_qs(url_object[4])
print
# ... and send values via OSC
for key, values in params.iteritems():
key = '/' + key
for value in values:
liblo.send(OSC_target, key, value)
print "Sent OSC message: '%s':'%s'" % (key, value,)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
return
# else serve files
try:
f = open(os.curdir + os.sep + self.path)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f.read())
f.close()
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def main(argv=None):
"""Main function, parses options and starts http server."""
global OSC_target
# set global parametars
init(argv)
# prepare OSC
try:
OSC_target = liblo.Address(OSC_IP, OSC_PORT)
print 'Sending OSC messages to %s:%d' % (OSC_IP, OSC_PORT)
except liblo.AddressError, err:
print str(err)
# start HTTP server
try:
server = HTTPServer((HTTP_IP, HTTP_PORT), MyHandler)
print 'Server listening on %s:%d' % (HTTP_IP, HTTP_PORT,)
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main(sys.argv[1:])