blob: 08a50a09e448cf39351328ebc7f6ca57c61891ee (
plain)
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
|
#!/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)
|