diff options
Diffstat (limited to 'streamogg.py')
| -rwxr-xr-x | streamogg.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/streamogg.py b/streamogg.py new file mode 100755 index 0000000..08a50a0 --- /dev/null +++ b/streamogg.py @@ -0,0 +1,82 @@ +#!/usr/bin/python + +import threading +import os +import socket +import re + +filename = '/tmp/test.ogg' +port = 42001 + +lock = threading.Lock() + +connections = [] +header = '' + +fd = os.open(filename, os.O_RDONLY) +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(('', port)) + + +def new_connection(): + global connections + + s.listen(0) + conn, addr = s.accept() + conn.sendall('HTTP/1.1 200 OK\r\nContent-Type: video/ogg\r\n\r\n') + conn.sendall(header) + with lock: + connections.append(conn) + print "new connection" + + t = threading.Thread(target=new_connection) + t.daemon = True + t.start() + + +def main(): + global header, connections, fd + + buf = '' + + # search header + while True: + buf += os.read(fd, 4096) + m = re.match(r'^(OggS.+?OggS.+?OggS.+?OggS.+?)(OggS.*)', buf, flags=re.DOTALL) + if m: + header = m.group(1) + #remainder = m.group(2) + print "header found" + break + + + t = threading.Thread(target=new_connection) + t.daemon = True + t.start() + + while True: + buf = os.read(fd, 4096) + if buf == '': + break + + with lock: + for conn in connections: + try: + conn.sendall(buf) + except socket.error: + conn.close() + with lock: + connections.remove(conn) + print "connection closed" + + os.close(fd) + + +try: + main() +except KeyboardInterrupt: + for conn in connections: + conn.close() + os.close(fd) + + |
