-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservidor.py
67 lines (57 loc) · 1.9 KB
/
servidor.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
65
66
67
import socket, threading
from os import system
connections = {}
clients_lock = threading.Lock()
class Server(object):
def __init__(self, capacity):
self.capacity = capacity
def handle_client(self, client_conn):
try:
while True:
data = client_conn.recv(1024)
if not data:
print(client_conn.getpeername(), 'disconectou-se')
break
elif b'client' in data[0:9]:
print('requisição de {}'.format(client_conn.getpeername()))
connections[client_conn.getpeername()[1]][1] = True
if b'request' in data[10:19]:
if self.printer_available():
conn = self.find_printer()
conn.send(b'print')
else:
msg = 'Não há nenhuma impressora disponível, execute "impressora.py\ne escolha o algoritmo."'\
.encode('utf-8')
connections[client_conn.getpeername()[1]][0].send(msg)
else:
print('Não reconhecido!')
else:
print('Não reconhecido!')
except Exception as erro:
print(erro)
client_conn.close()
def run(self):
with socket.socket() as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 50007))
s.listen(self.capacity)
while True:
connection, address = s.accept()
with clients_lock:
connections[address[1]] = []
connections[address[1]].append(connection)
connections[address[1]].append(False)
print('Server conectado por', address)
threading.Thread(target=self.handle_client, args=(connection,)).start()
def printer_available(self):
for key in connections:
if not connections[key][1]:
return True
return False
def find_printer(self):
for key in connections:
if not connections[key][1]:
return connections[key][0]
if __name__ == "__main__":
server = Server(4)
server.run()