-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
98 lines (83 loc) · 3.39 KB
/
client.py
File metadata and controls
98 lines (83 loc) · 3.39 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import socket
import time
# Client Configuration
HOST = 'localhost'
PORT = 12345
CONN_TIMEOUT = 5
MAX_RETRIES = 3
SHIFT = 11 # Caesar Cipher Shift Value
# Caesar Cipher Encryption
def encrypt_message(message):
encrypted = []
for char in message:
if char.isalpha():
offset = 65 if char.isupper() else 97
encrypted.append(chr((ord(char) - offset + SHIFT) % 26 + offset))
else:
encrypted.append(char)
return ''.join(encrypted)
# Caesar Cipher Decryption
def decrypt_message(message):
decrypted = []
for char in message:
if char.isalpha():
offset = 65 if char.isupper() else 97
decrypted.append(chr((ord(char) - offset - SHIFT) % 26 + offset))
else:
decrypted.append(char)
return ''.join(decrypted)
def initiate_client():
attempt_count = 0
while attempt_count < MAX_RETRIES:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
client.settimeout(CONN_TIMEOUT)
client.connect((HOST, PORT))
print("Successfully connected to the server.")
while True:
user_choice = display_menu()
if user_choice == '1':
user_input = input("Enter a string for palindrome check: ")
request_message = f"simple|{user_input}"
transmit_message(client, request_message)
elif user_choice == '2':
user_input = input("Enter a string for complex palindrome check: ")
request_message = f"complex|{user_input}"
transmit_message(client, request_message)
elif user_choice == '3':
print("Closing the client application...")
break
else:
print("Invalid option. Please select 1, 2, or 3.")
break
except socket.timeout:
attempt_count += 1
print(f"Connection timed out. Retrying {attempt_count}/{MAX_RETRIES}...")
time.sleep(2)
except ConnectionRefusedError:
attempt_count += 1
print(f"Connection refused. Retrying {attempt_count}/{MAX_RETRIES}...")
time.sleep(2)
except Exception as error:
attempt_count += 1
print(f"Unexpected error: {error}. Retrying {attempt_count}/{MAX_RETRIES}...")
time.sleep(2)
if attempt_count == MAX_RETRIES:
print("Unable to connect to the server after several attempts. Exiting.")
def display_menu():
print("\nOptions Menu:")
print("1. Basic Palindrome Check")
print("2. Advanced Palindrome Analysis")
print("3. Quit")
return input("Choose an option (1/2/3): ").strip()
def transmit_message(client, message):
try:
encrypted_message = encrypt_message(message) # Encrypt before sending
client.send(encrypted_message.encode())
server_reply = client.recv(1024).decode()
decrypted_reply = decrypt_message(server_reply) # Decrypt the server response
print(f"Response from Server:\n{decrypted_reply}")
except socket.timeout:
print("No response received from the server (timeout).")
if __name__ == "__main__":
initiate_client()