-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
50 lines (40 loc) · 1.3 KB
/
client.py
File metadata and controls
50 lines (40 loc) · 1.3 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
import socket
import threading
# Caesar cipher
def encrypt(message, shift=3):
result = ""
for char in message:
if char.isalpha():
shift_base = 65 if char.isupper() else 97
result += chr((ord(char) - shift_base + shift) % 26 + shift_base)
else:
result += char
return result
def decrypt(message, shift=3):
return encrypt(message, -shift)
def receive_messages(client_socket):
while True:
try:
encrypted_msg = client_socket.recv(1024).decode()
if encrypted_msg:
print(">>", decrypt(encrypted_msg))
except:
print("Disconnected from server.")
client_socket.close()
break
def start_client():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("127.0.0.1", 5555)) # Change IP if server is remote
username = input("Enter your username: ")
client.send(encrypt(username).encode())
print("Connected to chat server. Type 'exit' to leave.")
thread = threading.Thread(target=receive_messages, args=(client,))
thread.start()
while True:
msg = input("")
if msg.lower() == "exit":
break
client.send(encrypt(msg).encode())
client.close()
if __name__ == "__main__":
start_client()