I have a simple client-server program, in which I try to send the contents of my Documents folder that contains 2 files ["img1.jpg", "img2.jpg"].
Functioning:
The server waits for a client to connect and receives the messages from it, but if the message is text: files then the createExp () function that receives the name of the new folder to be created and the amount of files it goes to start Has receive.
With that data, I start a for cycle that has to be repeated according to the number of files that the user indicated to the server.
Cycle for:
This cycle has the function of receiving the data of each of the files sent by the client and subsequently saved in the indicated route.
Issue:
The server correctly receives a small part of the data, but then throws an error:
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\servidor_recv_archivo.py", line 53, in <module>
if msgp.decode() == "files":
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 5: invalid
start byte
server.py
import socket
import os
def bytes_to_int(b):
result = 0
for i in range(4):
result += b[i]<<(i*8)
return result
def makeExp(client):
while True:
FolderName = client.recv(1024).decode()
NumberFiles = client.recv(1024).decode()
print(FolderName,NumberFiles)
if not os.path.exists(FolderName):
os.mkdir(FolderName)
for element in range(int(NumberFiles)):
size = client.recv(4)
size = bytes_to_int(size)
current_size = 0
buffer = b""
while current_size < size:
data = client.recv(1024)
if not data:
break
if len(data) + current_size > size:
data = data[:size-current_size]
buffer += data
current_size += len(data)
with open(str(element),"wb") as f:
f.write(buffer)
break
ip = "192.168.8.8"
port = 5555
data = (ip,port)
listen = 2
server = socket.socket()
server.bind(data)
server.listen(listen)
client,direction = server.accept()
while True:
try:
msgp = client.recv(1024)
print(msgp)
client.sendall("Msg recv".encode())
if msgp.decode() == "files":
makeExp(client)
except ConnectionResetError:
print("{} ".format(direction))
break
client.py
import socket
import os
def convert_to_bytes(length):
result = bytearray()
result.append(length&255)
for i in range(3):
length = length>>8
result.append(length&255)
return result
def makeFolder(client):
rute = "C:/Users/AngelHp/Desktop/Documentos"
FolderName = os.path.basename("C:/Users/AngelHp/Desktop/Documentos")
NumberFiles = str(len(os.listdir("C:/Users/AngelHp/Desktop/Documentos")))
client.sendall(FolderName.encode())
client.sendall(NumberFiles.encode())
for element in (os.listdir(rute)):
length = os.path.getsize(rute+"/"+element)
client.send(convert_to_bytes(length))
with open(rute+"/"+element,"rb") as infile:
d = infile.read(1024)
while d:
client.send(d)
d = infile.read(1024)
ip = "192.168.8.8"
port = 5555
client = socket.socket()
client.connect((ip,port))
while True:
msg = input("> ")
if msg != "files": #Al oprimir el boton guarar en serv, lanzara la funcion crearExpServ
client.sendall(msg.encode())
reply = client.recv(1024).decode()
print(reply)
elif msg == "files":
print("ok")
makeFolder(client)
@mark - edited
import socket
with socket.socket() as s:
s.bind(('',8000))
s.listen(1)
with s.accept()[0] as c:
chunks = []
while True:
chunk = c.recv(4096)
if not chunk: break
chunks.append(chunk)
for i in range(2):
with open('image{}.png'.format(str(i)),'wb') as f:
f.write(b''.join(chunks))
cieent.py
import socket
import os
with socket.socket() as s:
s.connect(('localhost',8000))
for elemento in os.listdir("img"):
print(elemento)
with open("img"+"/"+elemento,'rb') as f:
s.sendall(f.read())