Send image to server over sockets - python

I'm trying to send image to setver, but something goes wrong. I cannot open the recived file.
Server:
with open("image.jpg", "wb") as fw:
print("Receiving..")
while True:
data = c.recv(1024)
image += data
if data == b'BEGIN':
continue
elif data == b'ENDED':
break
else:
fw.write(image)
fw.close()
print("Received..")
Client:
with open('/home/pi/Desktop/image_to_send.jpg', 'rb') as fs:
self.soc.send(b'BEGIN')
while True:
data = fs.read(1024)
self.soc.send(data)
if not data:
break
self.soc.send(b'ENDED')
fs.close()

First mistake: you add BEGIN and ENDED to image and you save it. Besides if you will add data to imag and write it in every loop then you will save the same part of image in file many times.
with open("image.jpg", "wb") as fw:
print("Receiving..")
while True:
data = c.recv(1024)
if data == b'BEGIN':
continue
elif data == b'ENDED':
break
else:
fw.write(data)
fw.close()
print("Received..")
Second mistake: you have wrong indentions in client and you run send('ENDED') after first send(data)
with open('/home/pi/Desktop/image_to_send.jpg', 'rb') as fs:
self.soc.send(b'BEGIN')
while True:
data = fs.read(1024)
self.soc.send(data)
if not data:
break
self.soc.send(b'ENDED')
fs.close()
I would send it without BEGIN and ENDED. When client closes connection then server should receive zero data or EOF - so you could check if not data: in server and close file.

Related

How to copy folder from server to client in python

I'm trying to copy the folder from my server to the client the file is at the x directory.want to know how to set the file path in the server.
server side
filename = '/home/Desktop/features'
f = open(filename, 'rb')
while True:
l = f.read(buff_size)
while (l):
self.sock.sendall(l)
# print('Sent ',repr(l))
l = f.read(buff_size)
if not l:
f.close()
self.sock.close()
break
client side
with open('red_fi', 'rb') as f:
print('file opened client ')
time.sleep(3)
a = True
while a:
print('receiving data...')
data = s.recv(buff_size)
# print('data=%s', (data))
if not data:
f.close()
# time.sleep(3)
print('file closed client')
a = False
break
# write data to a file
f.write(data)
# time.sleep(2)
print('Successfully received the file')
print(id_list)
s.close()
I want the features folder to be copied from server to client.

Simple Python Server Client File Transfer

I have this simple python server-client file transfer project going on.
There are two parts to each side. First, the client sends a file to server for the first part. Server then appends a line and sends back the file to client in the second part.
My issue is that for some reason, the server code is stuck on receiving whenever I have the return file code in it. If I should comment out the second section of the code, the server receives all the file sent by client. Otherwise it freezes on receiving. And yes, client did send it.
You can ignore all the print commands, just there to see where the problem is.
servercode:
import socket
ssFT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssFT.bind((socket.gethostname(), 8756))
ssFT.listen(1)
while True:
(conn, address) = ssFT.accept()
text_file = 'fileProj.txt'
#Receive, output and save file
with open(text_file, "wb") as fw:
print("Receiving..")
while True:
print('receiving')
data = conn.recv(1024)
print('Received: ', data.decode('utf-8'))
if not data:
print('Breaking from file write')
break
fw.write(data)
print('Wrote to file', data.decode('utf-8'))
fw.close()
print("Received..")
#Append and send file
print('Opening file ', text_file)
with open(text_file, 'ab+') as fa:
print('Opened file')
print("Appending string to file.")
string = b"Append this to file."
fa.write(string)
fa.seek(0, 0)
print("Sending file.")
while True:
data = fa.read(1024)
conn.send(data)
if not data:
break
fa.close()
print("Sent file.")
break
ssFT.close()
client code:
import socket
csFT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csFT.connect((socket.gethostname(), 8756))
text_file = 'passphrase.txt'
#Send file
with open(text_file, 'rb') as fs:
#Using with, no file close is necessary,
#with automatically handles file close
while True:
data = fs.read(1024)
print('Sending data', data.decode('utf-8'))
csFT.send(data)
print('Sent data', data.decode('utf-8'))
if not data:
print('Breaking from sending data')
break
fs.close()
#Receive file
print("Receiving..")
with open(text_file, 'wb') as fw:
while True:
data = csFT.recv(1024)
if not data:
break
fw.write(data)
fw.close()
print("Received..")
csFT.close()
I tested your code locally with Python 3.
The problem that I saw was with conn.recv in the server code. Because of the conn.recv blocks the connection and it is waiting for more data.
The solution that I found was sending commands to server informing about BEGIN and END of the data transfer, like that:
client.py
#Send file
with open(text_file, 'rb') as fs:
#Using with, no file close is necessary,
#with automatically handles file close
csFT.send(b'BEGIN')
while True:
data = fs.read(1024)
print('Sending data', data.decode('utf-8'))
csFT.send(data)
print('Sent data', data.decode('utf-8'))
if not data:
print('Breaking from sending data')
break
csFT.send(b'ENDED') # I used the same size of the BEGIN token
fs.close()
server.py
with open(text_file, "wb") as fw:
print("Receiving..")
while True:
print('receiving')
data = conn.recv(32)
if data == b'BEGIN':
continue
elif data == b'ENDED':
print('Breaking from file write')
break
else:
print('Received: ', data.decode('utf-8'))
fw.write(data)
print('Wrote to file', data.decode('utf-8'))
fw.close()
print("Received..")
Client.py complete code:
https://pastebin.com/LySsgEe4
Server.py complete code:
https://pastebin.com/KADZpqkM
I hope to help!
The way I solved this same issue is by sending the file size first, then the server can stop waiting as soon as it receives the whole file. I dunno whether there is a better solution or not but this works like a charm:
BUFFER_SIZE = 1024
client.py
import os
fsize = os.path.getsize(text_file)
csFT.send(str(fsize).encode('utf-8'))
with open(text_file, 'rb') as fs:
data = fs.read(BUFFER_SIZE)
while data:
csFT.send(data)
data = fs.read(BUFFER_SIZE)
server.py
with open(text_file, 'wb') as fw:
msg = ssFT.recv(BUFFER_SIZE)
fsize = int(msg.decode('utf-8'))
rsize = 0
while True:
data = ssFT.recv(BUFFER_SIZE)
rsize = rsize + len(data)
fw.write(data)
if rsize >= fsize:
print('Breaking from file write')
break

File Server Upload Python

File Server Download Problem Python 2.5.1
So i am working on a File Server as a hobby project. I am having some problems though. I can use the client to successfully upload the file to the server but say the file to upload is 50,000 bytes (50 mbs) it will only upload like 49,945 bytes then if i try opening it, it says its corrupt. If i close the server it goes to 50,000 then works. Is there a way to fix this without the server needing to close and reopen?
(Downloading Doesnt Have this Problem)
Full Client Code:
Client
Full Server:
Server
Client Upload Function:
def Uploader(s):
IsReal = True
data = "UploaderReady"
if data == "UploaderReady":
List = []
FilePath = dir_path = os.path.dirname(os.path.realpath(__file__))
List.append(os.listdir(FilePath))
FileUpload = raw_input("Pick a file? -> ")
for Item in List:
if FileUpload == Item:
IsReal = True #checks if item exists
if IsReal == True:
File = open(FileUpload,'rb')
bytestosend = File.read(1024)
FileSize = os.path.getsize(FileUpload)
s.send(FileUpload)
s.send(str(FileSize))
s.send(bytestosend)
while bytestosend != "":
bytestosend = File.read(8192)
s.send(bytestosend)
print"Processing"
File.close()
time.sleep(1.5)
s.send("COMPLETE")
print"File Successfully Uploaded"
time.sleep(2)
print" \n " * 10
Main()
if IsReal == "False":
print"Item doesn't Exist"
time.sleep(2)
print" \n " * 10
s.close()
Main()
Server Upload Function:
Todo = sock.recv(1024)
if Todo == "U":
print str(addr)+" Uploading"
UploadingThread = threading.Thread(target=Uploader,args=(c,c,))
UploadingThread.start()
def Uploader(c,s):
filename = s.recv(1024)
filesize = s.recv(1024)
f = open(filename,'wb')
totalRecv = 0
while totalRecv < filesize:
FileContent = s.recv(8192)
totalRecv += len(FileContent)
f.write(FileContent)
print"Download Complete"
f.close()
s.close()
You close the client connection on the server side, but never close it on the client side as Cory Shay said.
Instead of closing it though, you need to shutdown the socket and signal it is done writing with s.shutdown(socket.SHUT_WR)
Here's how it should look for the client:
def Uploader(s):
IsReal = True
data = "UploaderReady"
if data == "UploaderReady":
List = []
FilePath = dir_path = os.path.dirname(os.path.realpath(__file__))
List.append(os.listdir(FilePath))
FileUpload = raw_input("Pick a file? -> ")
for Item in List:
if FileUpload == Item:
IsReal = True #checks if item exists
if IsReal == True:
File = open(FileUpload,'rb')
bytestosend = File.read(1024)
FileSize = os.path.getsize(FileUpload)
s.send(FileUpload)
s.send(str(FileSize))
s.send(bytestosend)
while bytestosend != "":
bytestosend = File.read(8192)
s.send(bytestosend)
print"Processing"
s.shutdown(socket.SHUT_WR) # End the writing stream
print(s.recv(1024)) # Expecting the server to say 'upload complete'
s.close() # close the socket
File.close()
time.sleep(1.5)
s.send("COMPLETE")
s.close() #To close connection after uploading
print"File Successfully Uploaded"
time.sleep(2)
print" \n " * 10
Main()
and the server:
def Uploader(c,s):
filename = s.recv(1024)
filesize = s.recv(1024)
f = open(filename,'wb')
totalRecv = 0
while totalRecv < filesize:
FileContent = s.recv(8192)
totalRecv += len(FileContent)
f.write(FileContent)
s.send("Upload Complete!") # Tell client the upload is complete
print"Download Complete"
f.close()
s.close() # Close the socket
Also, you are passing the server Uploader 2 identical arguments, and only using one, instead you should just pass one:
UploadingThread = threading.Thread(target=Uploader,args=(c,c,))
# should be
UploadingThread = threading.Thread(target=Uploader,args=(c,))
Similarly, your password thread only needs 2:
c, addr = s.accept()
print"Client Connection: <"+str(addr)+">"
PasswordThread = threading.Thread(target=Password,args=(c,addr))
def Password(c,addr):
c.send("WAITINGPASSWORD")
PASSWORD = "123"
password = c.recv(1024)
and your checking password function can be simpler:
def Password(c,addr):
password = "123"
c.send("WAITINGPASSWORD")
attempt = c.recv(1024)[::-1]
if attempt == password:
doStuff()

File transferring, File can't be opend after transfer

I am currently working on a file transfer server and ran into
a problem. I am able to transfer the file completly and it works perefect,
But when the client that received the file cannot open it through python.
What I mean is that if I transferr a file, I can see it in the dic of the client the received it, but it cannot open it and I get an:IOError that the file doesn't exist.
The server:
def download_manager(self, sock, ADDR, name):
sock.send('Starting file download: '+name)
# Getting the socket that has the file
# Getting the user ip address
BUFSIZ = 1024
fileSock = socket(AF_INET, SOCK_STREAM)
fileSock.connect(ADDR)
print 'connected;'
# Starting the file request protocol.
tries = "2"
# sending number of retries.
fileSock.send(tries + "," + name)
sock.send(tries)
for i in range(int(tries)):
# Accepting the start message.
data, size = fileSock.recv(BUFSIZ).split(',')
sock.send(size)
size = int(size)
if data == 'Start':
fileSock.send('ok')
# Getting first data from the supplier.
data = fileSock.recv(BUFSIZ)
# Sending the data to the request client.
sock.send(data)
current_size = BUFSIZ
while current_size <= size:
# Getting data from the supplier.
data = fileSock.recv(BUFSIZ)
# The server "sleeps" in order to keep a synchronization between the client and the server.
# The client works slower than the server(It needs to save the file as well.
time.sleep(0.0001)
# Sending the data to the request client.
sock.send(data)
current_size += BUFSIZ
print current_size
# Receive for the request client the end message.
#sock.send(data)
data = sock.recv(1024)
if data == 'ok':
fileSock.send(data)
break
else:
fileSock.send(data)
else:
sock.send("wrong")
fileSock.close()
The sender client, Just the relevant part:
print "connection from:", addr
# From here on, the server works by the File-Transfer RFC.
# Accepting number of retries from the server and the name of the file.
data = clientsock.recv(self.BUFSIZ)
tries, name = data.split(',')
# Opening File.
f = file(name, mode='rb')
if not tries:
clientsock.close()
continue
try:
for i in range(int(tries)):
# Sending the file size.
clientsock.send('Start,'+str(self.file_size(name)))
# Accepting an ok.
ok = clientsock.recv(self.BUFSIZ)
if not ok:
continue
if ok == "ok":
# Sending the file.
clientsock.send(f.read(-1))
# Waiting for confirmation.
ok = clientsock.recv(self.BUFSIZ)
if ok == "ok":
break
else:
continue
f.close()
except IOError as e:
f.close()
print e
# An error has occurred, closing the socket.
#clientsock.send('End,None')
clientsock.close()
The recieve Client:
def get_file(self, name):
"""
This function receives and saves the requested file from the server.
:param name: The name of the file( How it is saved )
:return: None.
"""
name = name.split('.')
tries = int(self.sock.recv(self.BUFSIZ))
progress = 0
for i in range(tries):
f = file(name[0] + '.' + name[1], mode='wb')
final_size = int(self.sock.recv(self.BUFSIZ))
data = self.sock.recv(self.BUFSIZ)
f.write(data)
current_size = self.BUFSIZ
while current_size <= final_size:
progress = (float(current_size)/final_size)
if progress > 0.01:
self.app.progress = progress
data = self.sock.recv(self.BUFSIZ)
f.write(data)
current_size += self.BUFSIZ
f.close()
print "Current: " + str(current_size)
print "real: " + str(self.file_size(name[0] + '.' + name[1]))
print "Wanted: " + str(final_size)
self.app.progress = None
if self.file_size(name[0] + '.' + name[1]) == final_size:
print 'ok'
self.sock.send('ok')
break
else:
print 'Bad'
os.remove(name[0] + '.' + name[1])
self.sock.send('Bad')
continue
Any Help is welcomed!

python socket send files

trying out a new way to send files. The client will be run every 10 mins to ask server to send what's new in last 10 mins. What I have so far work 40% of the time. I can't figure out so far why that is the case.
server main loop:
while 1:
conn, addr = s.accept()
last_ai = get_last_sent_ai()
new_ai = get_new_ai(last_ai)
ai_notes = ''
''' send # of file '''
print "sending length ---------"
conn.sendall('{0}'.format(len(new_ai)))
for ai_file in new_ai:
ai_file = ai_file.rstrip()
if not os.path.isfile(ai_file): continue
f = open(ai_file, 'rb')
ai_notes = f.read()
f.close()
print "sending file infor " + '{0:<10d}:{1:>40s}'.format(len(ai_notes), ai_file)
ready_flag = conn.recv(5)
if ready_flag == "Ready":
conn.sendall(ai_notes)
if len(new_ai) > 0:
update_last_sent(new_ai[-1])
else:
print 'nothing to send'
conn.sendall(' ')
conn.close()
s.close()
Client main loop:
if num_f == 0: sys.exit(0)
while num_f > 0:
f_info = ''
f_info = s.recv(50)
f_len,f_name_tmp = f_info.split(':')
f_len = int(f_len)
s.sendall("Ready")
f_name = f_name_tmp
f = open(f_name, 'wb')
recvd = 0
while recvd < f_len:
chunk = s.recv(min(f_len - recvd, 1024))
if chunk == '':
raise RuntimeError("socket connection broken")
f.write(chunk)
recvd = recvd + len(chunk)
f.close()
num_f -= 1
s.close()
update:
the issue seems to be gone after I went back and changed how the send and receive are done. There gotta be some kind of blocking going on when it hangs, so I followed the python doc and modified the code and all the tests so far are working.

Categories