-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleclient.py
More file actions
44 lines (33 loc) · 1.12 KB
/
simpleclient.py
File metadata and controls
44 lines (33 loc) · 1.12 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
#!/usr/bin/python3
import socket
import threading
SERVER_IP = '::1' # Change if server is on another machine
SERVER_PORT = 1999
USERNAME = input("Enter your username: ")
client_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
client_socket.settimeout(2)
response_received = threading.Event()
# Send JOIN request
join_msg = f"JOIN {USERNAME}"
client_socket.sendto(join_msg.encode('utf-8'), (SERVER_IP, SERVER_PORT))
def listen():
while True:
try:
data, addr = client_socket.recvfrom(1024)
print("\n" + data.decode('utf-8'))
response_received.set()
except socket.timeout:
continue
threading.Thread(target=listen, daemon=True).start()
# Message loop
while True:
response_received.wait()
sendToFriend = str(input("Do you want to send a msg to a friend Y/N:"))
response_received.clear()
if (sendToFriend == "Y"):
to_user = input("\nSend to: ")
message = input("Message: ")
msg = f"MSG {to_user} {message}"
else:
msg = input()
client_socket.sendto(msg.encode('utf-8'), (SERVER_IP, SERVER_PORT))