-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreq.py
More file actions
69 lines (60 loc) · 2.14 KB
/
req.py
File metadata and controls
69 lines (60 loc) · 2.14 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
import requests
import sys
PORT = 1028
URL = f"http://127.0.0.1:{PORT}"
def execute_request(method, url, **kwargs):
print(f"==+ {method.upper()} +==")
try:
r = requests.request(method, url, timeout=5, **kwargs)
print(f"==+ status: {r.status_code} +==")
print(f"==+ headers: {dict(r.headers)} +==")
print(f"==+ body: {r.text} +==")
except requests.ConnectionError:
print(f"==+ error: Connection refused by {url} +==")
except requests.Timeout:
print(f"==+ error: Request to {url} timed out +==")
except Exception as e:
print(f"==+ error: {str(e)} +==")
def main():
while True:
try:
status = int(
input(
"enter 1 for get()\n"
" 2 for post()\n"
" 3 for put()\n"
" 4 for delete()\n"
" 5 for patch()\n"
" 6 for head()\n"
" 7 for options()\n"
" 8 to quit\n"
"> "
)
)
match status:
case 1:
execute_request("get", URL)
case 2:
execute_request("post", URL, json={"msg": "hello from POST"})
case 3:
execute_request("put", URL, json={"msg": "hello from PUT"})
case 4:
execute_request("delete", URL)
case 5:
execute_request("patch", URL, json={"msg": "hello from PATCH"})
case 6:
execute_request("head", URL)
case 7:
execute_request("options", URL)
case 8:
print("Exiting.")
sys.exit(0)
case _:
print("Invalid input.")
except ValueError:
print("Please enter a valid integer.")
except KeyboardInterrupt:
print("\nExiting.")
sys.exit(0)
if __name__ == "__main__":
main()