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 .................................................................... ....................................................................
server.py
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()
client.py
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()
while data != bytes(''.encode()):withwhile len(data) > 0:while data:works as well.socket.senddoes not guarantee all bytes are sent. You have to check the return value. You can also try.sendall().