-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
64 lines (50 loc) · 1.6 KB
/
client.py
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
from socket import *
import threading
import os
import sys
from connection import set_connection
from colors import print_color, format_string
CLIENT_SOCKET = socket(AF_INET, SOCK_STREAM)
SERVER_IP = ''
SERVER_PORT = 0
def receive():
while True:
try:
message = CLIENT_SOCKET.recv(1024).decode('ascii')
if message == 'USER':
CLIENT_SOCKET.send(USERNAME.encode())
else:
print(message)
except:
print_color('An error occurred!', 'red')
def wait_for_input():
while True:
message = f'{USERNAME}: {input("")}'
CLIENT_SOCKET.send(message.encode())
print(" _______ _____ _____ _____ _ _ _ \n"
" |__ __/ ____| __ \ / ____| (_) | | \n"
" | | | | | |__) | | | | |_ ___ _ __ | |_ \n"
" | | | | | ___/ | | | | |/ _ \ '_ \| __| \n"
" | | | |____| | | |____| | | __/ | | | |_ \n"
" |_| \_____|_| \_____|_|_|\___|_| |_|\__| \n"
)
input("PRESS ENTER TO START")
os.system('cls')
if 'default' in sys.argv:
SERVER_IP = 'localhost'
SERVER_PORT = 12000
else:
SERVER_IP, SERVER_PORT = set_connection()
print_color(f'\nOpening server...', 'yellow')
try:
CLIENT_SOCKET.connect((SERVER_IP, SERVER_PORT))
except OSError as e:
print(f'\n{e}', 'red')
exit(-1)
os.system('cls')
USERNAME = input('USERNAME: ')
USERNAME = format_string(USERNAME, 'random')
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=wait_for_input)
write_thread.start()