-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
44 lines (35 loc) · 1.1 KB
/
Copy pathclient.py
File metadata and controls
44 lines (35 loc) · 1.1 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
import threading
import socket
# checking if the user provided address and port
host = 'localhost'#input("Host IP: ")
port = 25565 #int(input('Port: '))
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
username = input('Choose an Username: ')
room_id = input("Room-ID: ")
if not room_id:
room_id='0'
# Recieve message from server
def client_receive():
while True:
try:
message = client.recv(1024).decode('utf-8')
if message == "/<@username>/":
client.send(username.encode('utf-8'))
elif message == "/<@room_id>/":
client.send(room_id.encode('utf-8'))
else:
print(message)
except:
print('Error!')
client.close()
break
# send message to server
def client_send():
while True:
message = f'{username}: {input("")}'
client.send(message.encode('utf-8'))
receive_thread = threading.Thread(target=client_receive)
receive_thread.start()
send_thread = threading.Thread(target=client_send)
send_thread.start()