Output
$ ./curly.py --help
$ ./curly.py https://kennesaw.edu/ out.html
$ cat out.html | head -n 5
$ ./curly.py https://placekitten.com/300/300 kitten.jpg
$ imgcat kitten.jpg
$ ./curly.py https://placekitten.com/300/300 kitten.jpg
$ imgcat kitten.jpg
Source code
import sys
import requests
import argparse
import logging
import pprint
parser = argparse.ArgumentParser(
description='Retrieves a single webpage, and writes it to a file.')
parser.add_argument('-v', '--verbose', action='store_true',
help="Prints connection headers")
parser.add_argument('url', metavar="URL", type=str,
help='A URL to send a request to')
parser.add_argument('file', nargs="?", help='Output file to write to.',
type=argparse.FileType('wb'), default=sys.stdout)
args = parser.parse_args()
url = args.url
outfile = args.file
logging.basicConfig(format="",
level=(logging.DEBUG if args.verbose else logging.INFO))
try:
res = requests.get(url)
elapsed_ms = int(res.elapsed.total_seconds() * 1000)
logging.debug(
f"Got response status {res.status_code} in {elapsed_ms} ms")
logging.debug(res.headers)
if (outfile is sys.stdout):
print(res.text)
else:
logging.info(
f"Writing response data to {outfile.name} ({len(res.content)} bytes)")
outfile.write(res.content)
sys.exit(0)
except Exception as e:
print("ERR:", e, file=sys.stderr)
sys.exit(1)