-
Notifications
You must be signed in to change notification settings - Fork 2
/
new_reader.py
160 lines (126 loc) · 3.89 KB
/
new_reader.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
154
155
156
157
158
159
160
"""
new_reader.py
Home of the reader function
"""
import socket
import struct
import sys
import urllib.request
MAJOR = "0"
MINOR = "1"
MAINTAINENCE = "13"
TIMEOUT = 60
def version():
"""
version prints version as a string
Odd number versions are releases.
Even number versions are testing builds between releases.
Used to set version in setup.py
and as an easy way to check which
version you have installed.
"""
return f"{MAJOR}.{MINOR}.{MAINTAINENCE}"
def version_number():
"""
version_number returns version as an int.
if version() returns 2.3.01
version_number will return 2301
"""
return int(f"{MAJOR}{MINOR}{MAINTAINENCE}")
def reader(uri, headers={}):
"""
reader returns an open file handle.
stdin: cat video.ts | gumd
files: "/home/you/video.ts"
http(s) urls: "https://example.com/vid.ts"
(http headers can be added by setting headers)
udp urls: "udp://1.2.3.4:5555"
multicast urls: "udp://@227.1.3.10:4310"
Use like:
with reader('http://iodisco.com/') as disco:
disco.read()
with reader('http://iodisco.com/',headers={"myHeader":"DOOM"}) as doom:
doom.read()
with reader("udp://@227.1.3.10:4310") as data:
data.read(8192)
with reader("/home/you/video.ts") as data:
fu = data.read()
udp_data =reader("udp://1.2.3.4:5555")
chunks = [udp_data.read(188) for i in range(0,1024)]
udp_data.close()
"""
# read from stdin
if uri in [None, sys.stdin.buffer]:
return sys.stdin.buffer
# Multicast
if uri.startswith("udp://@"):
return _open_mcast(uri)
# Udp
if uri.startswith("udp://"):
return _open_udp(uri)
# Http(s)
if uri.startswith("http"):
req = urllib.request.Request(uri, headers=headers)
return urllib.request.urlopen(req)
# File
return open(uri, "rb")
def _read_stream(sock):
"""
return a socket that can be read like a file.
"""
return sock.makefile(mode="rb")
def double_rcvbuf(sock):
"""
double_rcvbuf doubles socket.SO_RCVBUF
until it errors
"""
rcvbuf_size = sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
print(f"\nReading rcvbuf_size of {rcvbuf_size}", file=sys.stderr)
while True:
try:
rcvbuf_size += rcvbuf_size
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, rcvbuf_size)
except:
print(f"Setting rcvbuf_size to {rcvbuf_size}\n\n", file=sys.stderr)
break
def _mk_sock():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
double_rcvbuf(sock)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, "SO_REUSEPORT"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
return sock
def _mk_udp_sock(udp_ip, udp_port):
"""
udp socket setup
"""
udp_sock = _mk_sock()
udp_sock.bind((udp_ip, udp_port))
return _read_stream(udp_sock)
def _open_udp(uri):
"""
udp://1.2.3.4:5555
"""
udp_ip, udp_port = (uri.split("udp://")[1]).rsplit(":", 1)
udp_port = int(udp_port)
return _mk_udp_sock(udp_ip, udp_port)
def _open_mcast(uri):
"""
udp://@227.1.3.10:4310
"""
class Socked(socket.socket):
def read(self, bites):
return self.recv(bites)
interface_ip = "0.0.0.0"
multicast_group, port = (uri.split("udp://@")[1]).rsplit(":", 1)
multicast_port = int(port)
socked = Socked(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
socked.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack("b", 32))
socked.bind(("", multicast_port))
socked.setsockopt(
socket.SOL_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(multicast_group) + socket.inet_aton(interface_ip),
)
socked.settimeout(TIMEOUT)
return socked