-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservidor.py
More file actions
129 lines (88 loc) · 3.79 KB
/
servidor.py
File metadata and controls
129 lines (88 loc) · 3.79 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import socket
import threading
import random
import string
# Configurações
MAX_FILE_SIZE = 500 * 1024 * 1024
BLOCK_SIZE = 4096
PORTA_SERVIDOR = 8080
transfers = {}
def handle_client(client_socket, address):
print(f"[NOVA CONEXÃO] {address} conectado.")
try:
request = client_socket.recv(1024).decode().split('|')
if len(request) < 1:
return
command = request[0]
# LÓGICA DO ENVIADOR - SEND
if command == 'SEND':
if len(request) < 4: return
filename = request[1]
filesize = int(request[2])
file_hash = request[3]
if filesize > MAX_FILE_SIZE:
client_socket.send("ERROR:Arquivo muito grande".encode())
client_socket.close()
return
code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=4))
transfers[code] = {
'socket': client_socket,
'filename': filename,
'filesize': filesize,
'hash': file_hash
}
client_socket.send(f"CODE:{code}".encode())
print(f"[SESSÃO CRIADA] Código: {code} | Arq: {filename}")
print(f"--> O socket do enviador {address} ficou salvo aguardando o receptor.")
return
# LÓGICA DO RECEPTOR
elif command == 'RECV':
if len(request) < 2: return
code = request[1]
if code in transfers:
sender_data = transfers[code]
sender_socket = sender_data['socket']
filename = sender_data['filename']
filesize = sender_data['filesize']
print(f"O cliente {address} pediu o arquivo {code}")
client_socket.send(f"FILENM|{filename}|{filesize}|{sender_data['hash']}".encode())
client_socket.recv(1024)
try:
sender_socket.send("UPLOAD_NOW".encode())
except:
client_socket.send("ERROR:Enviador desconectou".encode())
del transfers[code]
client_socket.close()
return
print(f"--> Iniciando transferência de {filesize} bytes...")
remaining = filesize
while remaining > 0:
read_size = min(BLOCK_SIZE, remaining)
data = sender_socket.recv(read_size)
if not data: break
client_socket.send(data)
remaining -= len(data)
print(f"✅ Transferência concluída! Código {code} finalizado.")
client_socket.close()
else:
client_socket.send("ERROR:Código inválido ou expirado".encode())
client_socket.close()
except Exception as e:
print(f"❌ Erro na conexão com {address}: {e}")
client_socket.close()
def iniciar_servidor():
HOST = '0.0.0.0'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORTA_SERVIDOR))
server.listen()
print(f"--- SERVIDOR RODANDO ---")
while True:
try:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[THREAD] Total ativas: {threading.active_count() - 1}")
except Exception as e:
print(f"Erro ao aceitar conexão: {e}")
if __name__ == "__main__":
iniciar_servidor()