-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_Module_File
46 lines (42 loc) · 1.55 KB
/
Main_Module_File
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
import requests
from requests.exceptions import ConnectionError, Timeout
login_url = 'https://172.16.1.1:8090'
payload = {
'username': '-----',
'password': '-----'
}
timeout_duration = 5
def check_connection(url, timeout):
try:
response = requests.get(url, timeout=timeout, verify=False)
if response.status_code == 200:
print("Connection successful.")
return True
else:
print(f"Server responded with status code: {response.status_code}")
return False
except ConnectionError:
print(f"Failed to connect to {url}. Please check your network connection.")
return False
except Timeout:
print(f"Connection to {url} timed out after {timeout} seconds.")
return False
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return False
def login():
try:
with requests.Session() as session:
response = session.post(login_url, data=payload, verify=False)
if response.status_code == 200:
print("Logged in successfully")
else:
print(f"Login failed with status code: {response.status_code}")
print(response.text) # Print response to debug
except requests.exceptions.RequestException as e:
print(f"An error occurred during login: {e}")
if __name__ == "__main__":
if check_connection(login_url, timeout_duration):
login()
else:
print("Could not establish a connection. Please try again later.")