-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
52 lines (43 loc) · 1.44 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
import socket
import subprocess
SERVER_HOST = 'localhost'
SERVER_PORT = 9999
BUFFERSIZE = 4096
def play_stream(sock):
process = subprocess.Popen(
['vlc', '--file-caching=3000', '-'],
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
try:
while True:
data = sock.recv(BUFFERSIZE)
if not data:
break
process.stdin.write(data)
except Exception as e:
print(f"[ERROR] Errore durante lo streaming: {e}")
finally:
process.stdin.close()
process.wait()
def connection():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((SERVER_HOST, SERVER_PORT))
while True:
response = sock.recv(BUFFERSIZE).decode(errors='ignore').strip()
if "Verrai disconnesso." in response or "Arrivederci!" in response or "Connessione chiusa" in response:
print(f"[SERVER] {response}")
print("Disconnessione dal server.")
break
elif 'Riproduco il file' in response:
print(f"[SERVER] {response}")
play_stream(sock)
break
elif 'requiredInput' in response:
message = input("Tu: ")
sock.sendall(message.encode())
else:
print(f"[SERVER] {response}")
if __name__ == "__main__":
connection()