forked from arvidn/libtorrent-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_range.py
47 lines (38 loc) · 959 Bytes
/
test_range.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
import urllib2
import sys
url = sys.argv[1]
r = urllib2.Request(url)
resp = urllib2.urlopen(r)
data = resp.read(8192)
f = open('single_request', 'wb+')
while len(data) > 0:
f.write(data)
data = resp.read(8192)
full_size = int(resp.info()['Content-Length'])
f.close()
print 'file size: %d' % full_size
f = open('multi_request', 'wb+')
rng = 1
start = 0
while full_size > 0:
r = urllib2.Request(url)
rng = min(rng, full_size)
r.add_header('Range', 'bytes=%d-%d' % (start, start + rng - 1))
print '%d-%d' % (start, start + rng - 1)
full_size -= rng
start += rng
resp = urllib2.urlopen(r)
data = resp.read(8192)
request_size = 0
while len(data) > 0:
request_size += len(data)
f.write(data)
data = resp.read(8192)
print 'received %d bytes' % request_size
if request_size != rng:
print 'received %d bytes, expected %d' % (request_size, rng)
print 'http status: %d' % (resp.getcode())
print resp.info()
break
rng *= 2
f.close()