-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Requests Library.py
More file actions
80 lines (65 loc) · 2.4 KB
/
Python Requests Library.py
File metadata and controls
80 lines (65 loc) · 2.4 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# Arthur: Scotty Jokon
# Date: 12/7/2023
# Purpose: Python Conditional Statements
# Resources: https://chat.openai.com/share/7d6d7504-ade5-4c60-8485-96d68c6248cb
import requests
def main():
# Step 1: Prompt the user for the destination URL
destination_url = input("Enter the destination URL: ")
# Step 2: Prompt the user to select an HTTP Method
http_method = input("Select an HTTP Method (GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS): ").upper()
# Step 3: Print the entire request and ask for confirmation
print("\nRequest to be sent:")
print(f"URL: {destination_url}")
print(f"HTTP Method: {http_method}")
confirmation = input("Do you want to proceed? (yes/no): ").lower()
if confirmation != 'yes':
print("Operation aborted.")
return
# Step 4: Perform the request using the requests library
response = perform_request(destination_url, http_method)
# Step 5: Print response header information
print("\nResponse Header Information:")
for key, value in response.headers.items():
print(f"{key}: {value}")
# Step 6: Translate status code to plain terms
print("\nTranslated Status Code:")
translate_status_code(response.status_code)
def perform_request(url, method):
try:
if method == 'GET':
response = requests.get(url)
elif method == 'POST':
response = requests.post(url)
elif method == 'PUT':
response = requests.put(url)
elif method == 'DELETE':
response = requests.delete(url)
elif method == 'HEAD':
response = requests.head(url)
elif method == 'PATCH':
response = requests.patch(url)
elif method == 'OPTIONS':
response = requests.options(url)
else:
raise ValueError("Invalid HTTP Method")
return response
except requests.RequestException as e:
print(f"Error: {e}")
return None
def translate_status_code(status_code):
translations = {
200: "OK",
201: "Created",
204: "No Content",
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
500: "Internal Server Error",
}
translation = translations.get(status_code, "Unknown Status Code")
print(f"{status_code}: {translation}")
if __name__ == "__main__":
main()