-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
61 lines (51 loc) · 1.79 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
# Script for the Client
# Imports
import datetime
import select
import socket
import sys
from playsound import playsound
# Default IP and Port
ip_address = '127.0.0.1'
port = 6969
# Check arguments
if len(sys.argv) != 3:
print('Using default IP and Port "127.0.0.1:4242"\nYou can specify an IP and Port while starting the client: '
'"python3 script ip port"\n')
if len(sys.argv) == 3:
ip_address = str(sys.argv[1])
port = int(sys.argv[2])
# Connect to Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((ip_address, port))
name = input('Please enter your name: ')
while True:
# List with possible input streams
sockets_list = [sys.stdin, server]
read_sockets, write_socket, error_socket = select.select(sockets_list, [], [])
for socks in read_sockets:
if socks == server:
message = socks.recv(2048).decode()
if message:
playsound('./res/beep.mp3')
print(message)
else:
print('Server connection dropped!')
server.close()
exit()
else:
message = sys.stdin.readline().replace('\n', '')
message = message.strip()
if len(message) == 0:
continue
if message.lower() == '/quit':
print('Cutting connection...')
exit()
if message.lower().startswith('/nickname') and len(message) > 10:
name = message.split(' ')[1]
continue
date_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
message = f'[{date_now}] {name}: {message}'
sys.stdout.write(f'{message}\n')
sys.stdout.flush()
server.sendto(message.encode(), (ip_address, port))