-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
54 lines (44 loc) · 1.54 KB
/
client.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import json
import requests
# Set the mode to either 'DEV' or 'PROD'
MODE = 'DEV' # Change this to 'PROD' for production mode
file_loc = 'fake_data.json'
output_file = 'output_' + file_loc
if MODE == 'DEV':
import warnings
from urllib3.exceptions import InsecureRequestWarning
# Mute the InsecureRequestWarning in development mode
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
verify_ssl = False
else:
verify_ssl = True
# Define login data
login_data = {
'username': 'username',
'password': 'password_hash'
}
url = 'https://127.0.0.1:5000/'
# Attempt to log in
login_response = requests.post(url + 'login', json=login_data, verify=verify_ssl)
if login_response.status_code == 200:
try:
with open(file_loc, 'r') as jf:
payload = json.load(jf)
access_token = login_response.json().get('access_token')
headers = {
'Authorization': f'Bearer {access_token}',
'Payload': json.dumps(payload),
'Compute': '1'
}
except FileNotFoundError:
print(f"File not found: {file_loc}")
except Exception as e:
print(f"An error occurred while opening the file: {e}")
# Access a protected resource
protected_response = requests.get(url + 'protected', headers=headers, verify=verify_ssl)
if protected_response.status_code == 200:
print(protected_response.json())
else:
print('Error Code:', protected_response.status_code)
else:
print('Login failed. Invalid credentials.')