-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuncSocket.py
More file actions
42 lines (37 loc) · 1.32 KB
/
funcSocket.py
File metadata and controls
42 lines (37 loc) · 1.32 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
BUFFER_SIZE = 1024
import socket
import os, time
def enviar_arquivo(namefile, socketC, addr, op):
dirr = ['servidor\\', 'cliente\\']
print(f'func: Enviando arquivo: {namefile}')
namefile = dirr[op] + namefile
with open(namefile, 'rb') as file:
while True:
data = file.read(BUFFER_SIZE)
if not data:
break
socketC.sendto(data, addr)
time.sleep(0.001)
print(f'Arquivo enviado!')
socketC.sendto(b'FIM_DO_ARQUIVO', addr)
def receber_arquivo(namefile, socketC, op):
with open(namefile, 'wb') as file:
print(f'func: Recebendo arquivo: {namefile[8:]}')
while True:
try:
data = socketC.recv(BUFFER_SIZE)
if not data:
print('no data')
break
if data == b'FIM_DO_ARQUIVO':
print("Arquivo recebido com sucesso!")
break
file.write(data)
except socket.error as e:
print(f"Erro ao receber o arquivo: {e}")
break
def rename_arq(namefile, op):
#namefile = dirr[op] + namefile
nome_atual = namefile.split('.')
novo_nome = nome_atual[0]+str(op)+'.'+nome_atual[1]#dirr[op]+nome_atual[0]+'_n'+'.'+nome_atual[1]
return novo_nome