Reputation: 413
everything should be alright, but received file is always being damaged the data is matched without any difference remove the hash tag from print(data) if you want to see binary and compering by yourself .................................................................... ....................................................................
import socket, threading, os
from time import sleep
host, port = '127.0.0.1', 442
class transfer :
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
def __init__(self):
self.mysocket.bind((host, port))
print(' Server is ready ..')
self.mysocket.listen(5)
conn, addr = self.mysocket.accept()
file_name = 'test.webm'
size = os.path.getsize(file_name)
print(' file size : {}'.format(str(size)))
send_thread = threading.Thread(target = self.send_file, args=(file_name, size, conn, addr, ))
send_thread.start()
def send_file(self, file_name, size, conn, addr):
with open(file_name, 'rb') as file:
data = file.read(1024)
conn.send(data)
while data != bytes(''.encode()):
#print(data)
data = file.read(1024)
conn.send(data)
print(' File sent successfully.')
Transfer = transfer()
import socket, sys, threading
from time import sleep
host, port = '127.0.0.1', 442
class recv_data :
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mysocket.connect((host, port))
def __init__(self):
data = self.mysocket.recv(1024)
f = open('newfile.webm', 'wb')
while data != bytes(''.encode()):
#print(data)
data = self.mysocket.recv(1024)
f.write(data)
re = recv_data()
Upvotes: 2
Views: 4500
Reputation: 5808
In the client here:
def __init__(self):
data = self.mysocket.recv(1024)
f = open('newfile.webm', 'wb')
while data != bytes(''.encode()):
#print(data)
data = self.mysocket.recv(1024)
f.write(data)
the program never writes the result of the first recv
into the file. That data is thrown away and replaced by the result of the second recv
, which becomes the first data written into the file.
To fix, move the f.write
above the second recv
call.
Upvotes: 2