-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpeeper.py
36 lines (30 loc) · 1.09 KB
/
httpeeper.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
import argparse
import requests
import json
def main():
# Set up argument parser
parser = argparse.ArgumentParser(description="HTTP Requester with output in JSON or YAML")
parser.add_argument('url', help="URL to fetch data from")
parser.add_argument('-yaml', help="Output in YAML format", action='store_true')
args = parser.parse_args()
# Perform the HTTP request
try:
response = requests.get(args.url)
response.raise_for_status() # Raises stored HTTPError, if one occurred.
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
return
# Process and print the response
try:
response_data = response.json() # Convert response to JSON
except ValueError as e:
print(f"Error processing JSON response: {e}")
return
if args.yaml:
# Output in YAML format
print(yaml.dump(response_data, allow_unicode=True, default_flow_style=False))
else:
# Default output in JSON format
print(json.dumps(response_data, indent=4))
if __name__ == "__main__":
main()