-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathclient-with-check.py
More file actions
executable file
·35 lines (31 loc) · 1.08 KB
/
client-with-check.py
File metadata and controls
executable file
·35 lines (31 loc) · 1.08 KB
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
#!/usr/bin/env python3
#
# An HTTP client that checks that the content of the response has at least
# Content-Length bytes.
#
# For requests 2.x (should not be needed for requests 3.x).
#
import requests
import sys
response = requests.get('http://localhost:8080/')
if not response.ok:
sys.exit('error: HTTP {}'.format(response.status_code))
# Check that we have read all the data as the requests library does not
# currently enforce this.
expected_length = response.headers.get('Content-Length')
if expected_length is not None:
# We cannot use len(response.content) as this would not work when the body
# of the response was compressed (e.g. when the response has
# `Content-Encoding: gzip`).
actual_length = response.raw.tell()
expected_length = int(expected_length)
if actual_length < expected_length:
raise IOError(
'incomplete read ({} bytes read, {} more expected)'.format(
actual_length,
expected_length - actual_length
)
)
print(response.headers)
print(response.content)
print(len(response.content))