0
In the following code for file transfer using Python sockets:
Client:
lista_arquivos = ['C:\Users\fulano\Imagens\passaro.jpg', 'C:\Users\fulano\Imagens\cachorro.jpg', 'C:\Users\fulano\Imagens\gato.jpg']
def transferir_arquivos(self):
for arquivo in self.lista_arquivos:
print(arquivo)
host = socket.gethostname()
port = 9000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
# enviado o nome do arquivo
nome_arquivo = os.path.basename(arquivo)
s.send(nome_arquivo.encode('utf-8'))
# próprio arquivo enviado
with open(f'{arquivo}', 'rb') as arq:
print(f"Transferindo {arquivo}")
s.send(arq.read())
print("Arquivo enviado")
s.close()
arq.close()
self.excluir_lista()
Server:
serversock = socket.socket() host = socket.gethostname() port = 9000 serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversock.bind((host, port)) serversock.listen(5)
while True:
clientsocket, address = serversock.accept()
print("Got a connection from %s" % str(address))
dados = clientsocket.recv(32)
print(dados) # mostra o conteudo de dados
nome = dados.decode()
print(f"Nome: {nome}")
with open(nome, 'wb') as f:
print('arquivo aberto')
print('Recebendo dados...')
data = clientsocket.recv(301149653)
f.write(data)
print("Enviando")
f.close()
print('Transferencia Completa!!!') clientsocket.close() print('conexão encerrada')
The following is returned when you arrive at the second item in the list:
Got a connection from ('meu_ip', 55159)
b'passaro.jpg'
Nome: passaro.jpg
arquivo aberto
Recebendo dados...
Enviando
Transferencia Completa!!!
Got a connection from ('meu_ip', 55160)
b'cachorro.jpg\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xff'
Traceback (most recent call last):
File ".\server_side.py", line 17, in <module>
nome = dados.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 11: invalid start byte
I don’t usually specify encoding, but if it helps, github.com/Diolante/Server-Web-Simple-in-Python
– FourZeroFive