Repository navigation
Expand file tree
/
Copy pathserverChat.py
More file actions
58 lines (49 loc) · 1.56 KB
/
Copy pathserverChat.py
File metadata and controls
58 lines (49 loc) · 1.56 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import socket
import threading
import sys
import pickle
class servidor():
def __init__(self, host="localhost", port=4000):
self.clientes = []
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((str(host), int(port)))
self.sock.listen(10)
self.sock.setblocking(False)
aceptar = threading.Thread(target=self.aceptarCon)
procesar = threading.Thread(target=self.procesarCon)
aceptar.daemon = True
aceptar.start()
procesar.daemon = True
procesar.start()
while True:
msg = input("->")
if msg == "salir":
self.sock.clase()
def msg_to_all(self, msg, cliente):
for c in self.clientes:
try:
if c != cliente:
c.send(msg)
except:
self.clientes.remove(c)
def aceptarCon(self):
print("aceptarCon iniciado")
while True:
try:
conn, addr = self.sock.accept()
conn.setblocking(False)
self.clientes.append(conn)
except:
pass
def procesarCon(self):
print("procesarCon iniciado")
while True:
if len(self.clientes) > 0:
for c in self.clientes:
try:
data = c.recv(1024)
if data:
self.msg_to_all(data, c)
except:
pass
s = servidor()