Skip to content

Instantly share code, notes, and snippets.

@thaisviana
Created September 3, 2021 11:54
Show Gist options
  • Save thaisviana/7c2d3caaa8cf4b1377200d42a1a61d42 to your computer and use it in GitHub Desktop.
Save thaisviana/7c2d3caaa8cf4b1377200d42a1a61d42 to your computer and use it in GitHub Desktop.
arq_client.py
import socket, sys, os
# Cria o socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
nome_arq = input('Entre com o nome do arquivo: ')
"/Users/thaisviana/Downloads/infnet.png"
try:
# Tenta se conectar ao servidor
s.connect((socket.gethostbyname(""), 9091))
# Envia o nome do arquivo:
s.send(nome_arq.encode('ascii'))
# Recebe o tamanho do arquivo:
msg = s.recv(1024)
tamanho = int(msg.decode('ascii'))
# Checa se o arquivo existe no servidor:
if tamanho >= 0:
print('tamanho do arquivo', tamanho)
# Gera o arquivo local na pasta download
nome_arq = nome_arq.split('/')[-1]
with open(nome_arq, "wb+") as arq:
soma = 0
txt_bytes = s.recv(4096)
# Escreve o arquivo
while txt_bytes:
arq.write(txt_bytes)
soma = soma + len(txt_bytes)
# os.system('clear')
print("Baixando..." ,soma, "KB", tamanho, "KB")
txt_bytes = s.recv(4096)
else:
print('Arquivo não encontrado no servidor!')
except Exception as erro:
raise
s.close()
input("Pressione qualquer tecla para sair...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment