get-with-headers.py
30 lines
| 817 B
| text/x-python
|
PythonLexer
/ tests / get-with-headers.py
Eric Hopper
|
r2532 | #!/usr/bin/env python | ||
__doc__ = """This does HTTP get requests given a host:port and path and returns | ||||
a subset of the headers plus the body of the result.""" | ||||
Gilles Moris
|
r7544 | import httplib, sys, re | ||
Patrick Mezard
|
r7054 | |||
try: | ||||
import msvcrt, os | ||||
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | ||||
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | ||||
except ImportError: | ||||
pass | ||||
Eric Hopper
|
r2532 | headers = [h.lower() for h in sys.argv[3:]] | ||
conn = httplib.HTTPConnection(sys.argv[1]) | ||||
conn.request("GET", sys.argv[2]) | ||||
response = conn.getresponse() | ||||
print response.status, response.reason | ||||
for h in headers: | ||||
if response.getheader(h, None) is not None: | ||||
print "%s: %s" % (h, response.getheader(h)) | ||||
Gilles Moris
|
r7544 | data = response.read() | ||
data = re.sub('\d+ years', 'many years', data) | ||||
sys.stdout.write(data) | ||||
Bryan O'Sullivan
|
r5561 | |||
if 200 <= response.status <= 299: | ||||
sys.exit(0) | ||||
sys.exit(1) | ||||