-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·53 lines (42 loc) · 1.37 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
#!/usr/bin/env python
import socket
import threading
import os
HEADER = 8
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!exit"
SERVER = "192.168.1.8"
PORT = 5050
ADDR = (SERVER, PORT)
name = "roshan"
def send(connection, msg):
msg = msg.encode(FORMAT)
msg_length = len(msg)
send_length_msg = str(msg_length).encode(FORMAT)
send_length_msg += b" " * (HEADER - len(send_length_msg))
connection.send(send_length_msg)
connection.send(msg)
def receive(connection):
while (msg_length:= connection.recv(HEADER).decode(FORMAT)):
msg_length = int(msg_length)
msg = connection.recv(msg_length).decode(FORMAT)
print(msg)
try:
senderName, receivedMessage = msg.split('>')
os.system(f'''notify-send """Local Chat:{senderName}""" """{receivedMessage} """''')
except ValueError:
os.system(f'''notify-send """Local Chat""" """ {msg} """''')
def write(connection):
while True:
msg = input()
send(connection, msg)
if msg == DISCONNECT_MESSAGE:
connection.close()
quit()
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(ADDR)
send(connection, name)
receive_thread = threading.Thread(target=receive, args=(connection,))
receive_thread.start()
write_thread = threading.Thread(target=write, args=(connection,))
write_thread.start()