How to copy folder from server to client in python - 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.

Related

How to transfer more files rather than just one using python?

I made a file transferrer and the problem is that out of unknown reason it lets me only send one file... When I try the second time the program works fine, but the file doesn't show up in directory. It would mean a lot to me if someone helped me out.
Code for sender:
import os
import shutil
import socket
import time
# Creating a socket.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((socket.gethostname(), 22222))
sock.listen(5)
print("Host Name: ", sock.getsockname())
# Accepting the connection.
client, addr = sock.accept()
original = ""
print("When you want to stop with file uploading type -> STOP <-")
while (1):
original = input(str("Filepath:"))
if (original!="STOP"):
filename = os.path.basename(original)
target = r'C:\Users\Gasper\Desktop\transfer\filename'
path = target.replace('filename', filename)
new_file = shutil.copy(original, path)
# Getting file details.
file_name = filename
file_size = os.path.getsize(file_name)
# Sending file_name and detail.
client.send(file_name.encode())
client.send(str(file_size).encode())
# Opening file and sending data.
with open(file_name, "rb") as file:
c = 0
# Running loop while c != file_size.
while c <= file_size:
data = file.read(1024)
if not data:
break
client.sendall(data)
c += len(data)
os.remove(filename)
else:
break
print("File Transfer Complete!")
input("Press enter to exit...")
# Closing the socket.
sock.close()
Code for receiver:
import socket
import time
host = input("Host Name: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Trying to connect to socket.
try:
sock.connect((host, 22222))
print("Connected Successfully")
except:
print("Unable to connect")
exit(0)
# Send file details.
file_name = sock.recv(100).decode()
file_size = sock.recv(100).decode()
# Opening and reading file.
with open("./rec/" + file_name, "wb") as file:
c = 0
# Starting the time capture.
start_time = time.time()
# Running the loop while file is recieved.
while c <= int(file_size):
data = sock.recv(1024)
if not data:
break
file.write(data)
c += len(data)
# Ending the time capture.
end_time = time.time()
print("File transfer Complete!")
input("Press enter to exit...")
# Closing the socket.
sock.close()
Example:
Filepath: C\Users\Admin\Desktop\Directory\File1.txt(I put in the first file path and it transfers successfully)
Filepath: C\Users\Admin\Desktop\Directory\File2.txt(I put in the second file path and it doesnt transfer at all)

Socket can't read large files. Python

Im doing a project where it connect to a server directory and read all their files.
So i just wrote a code to download the server files whenever i click on it. I can read small size file with no issues, but when i want to view a large content, only 1 line can be view and store to a list. Although i cant read all the large content, but im able to download it with no issues.
I can read and download all small size content with no issues:
The large file that i want to read from:
Output that i got when i read the large files. (Cant read all the content but i successfully to download the full content file)
CLIENT CODE:
def mouseHover(event):
x = lbox.curselection()[0]
file = lbox.get(x)
ext = (".txt", ".csv")
if file.endswith(ext):
self.s.send(("fdown~" + file).encode("utf-8")) #must have
data = self.s.recv(1024).decode("utf-8")
if data[:6] == 'EXISTS':
filesize = data[6:]
self.s.send("OK".encode("utf-8"))
f = open(file, 'wb') # must have
data = (self.s.recv(1024))
totalRecv = len(data)
f.write(data)
while int(totalRecv) < int(filesize):
data = self.s.recv(1024)
totalRecv += len(data)
f.write(data)
sys.stdout.write("\r|" + "█" * int((totalRecv / float(filesize)) * 50) + "|{0:.2f}".format(
(totalRecv / float(filesize)) * 100) + "% ")
sys.stdout.flush()
time.sleep(0.01)
print("\nDownload Complete!")
f.close()
global data2
data2= data.splitlines()
print(data2)
self.text.delete('1.0', tk.END)
self.text.insert(tk.END, data)
else:
messagebox.showinfo("WARNING", "Currently only .txt/csv file is supported.")
lbox.bind("<<ListboxSelect>>", mouseHover)
SERVER CODE:
# Create a Socket ( connect two computers)
def create_socket():
try:
global host
global port
global s
host = ""
port = 9999
s = socket.socket()
except socket.error as msg:
create_socket()
# Binding the socket and listening for connections
def bind_socket():
try:
global host
global port
global s
s.bind((host, port))
s.listen(5)
except socket.error as msg:
bind_socket()
# send file list
def flist(conn):
try:
arr = pickle.dumps(os.listdir())
conn.send(arr)
except:
conn.send(('Error').encode("utf-8"))
# accept file from server
def fdown(filename, conn):
try:
data = conn.recv(1024).decode("utf-8")
if data[:6] == 'EXISTS':
filesize = data[6:]
conn.send("OK".encode("utf-8"))
f = open(filename, 'wb')
data = (conn.recv(1024))
totalRecv = len(data)
f.write(data)
while int(totalRecv) < int(filesize):
data = conn.recv(1024)
totalRecv += len(data)
f.write(data)
f.close()
except:
conn.send(('Error').encode("utf-8"))
# send file
def fup(filename, conn):
if os.path.isfile(filename):
conn.send(str.encode("EXISTS " + str(os.path.getsize(filename))))
filesize = int(os.path.getsize(filename))
userResponse = conn.recv(1024).decode("utf-8")
if userResponse[:2] == 'OK':
with open(filename, 'rb') as f:
bytesToSend = f.read(1024)
conn.send(bytesToSend)
totalSend = len(bytesToSend)
while int(totalSend) < int(filesize):
bytesToSend = f.read(1024)
totalSend += len(bytesToSend)
conn.send(bytesToSend)
else:
conn.send("ERROR".encode("utf-8"))
# main
def main(s):
while True:
data = (s.recv(1024)).decode("utf-8").split('~')
if data[0] == 'fdown':
fup(data[1], s)
elif data[0] == 'fup':
fdown(data[1], s)
elif data[0] == 'flist':
flist(s)
break
else:
s.send(".".encode('utf-8'))
# Establish connection with a client (socket must be listening)
def socket_accept():
conn, address = s.accept()
main(conn)
conn.close()
create_socket()
bind_socket()
socket_accept()
After you read each block of data, concatenate it to a string containing the whole file. Then at the end of the loop you can split this into lines and display it in the text box.
There's also no need to limit your recv() calls to 1000 bytes. Use filesize - totalRecv as the limit, and it will receive as much as is available, but not go past the end of the file.
There's no need to convert filesize to int every time through the loop, do it once when you assign the variable.
def mouseHover(event):
x = lbox.curselection()[0]
file = lbox.get(x)
ext = (".txt", ".csv")
if file.endswith(ext):
self.s.send(("fdown~" + file).encode("utf-8")) #must have
data = self.s.recv(1024).decode("utf-8")
if data[:6] == 'EXISTS':
filesize = int(data[6:])
self.s.send("OK".encode("utf-8"))
all_data = ''
with open(file, 'wb') as f: # must have
totalRecv = 0
while totalRecv < filesize:
data = self.s.recv(filesize - totalRecv)
totalRecv += len(data)
all_data += data
f.write(data)
sys.stdout.write("\r|" + "█" * int((totalRecv / filesize) * 50) + "|{0:.2f}".format(
(totalRecv / filesize) * 100) + "% ")
sys.stdout.flush()
time.sleep(0.01)
print("\nDownload Complete!")
global data2
data2= all_data.splitlines()
print(data2)
self.text.delete('1.0', tk.END)
self.text.insert(tk.END, all_data)
else:
messagebox.showinfo("WARNING", "Currently only .txt/csv file is supported.")
lbox.bind("<<ListboxSelect>>", mouseHover)

Python socket get files from for loop [WinError 10056]

When i try to get files from the server, i am getting the first file without a problem than getting this error on the second one; OSError: [WinError 10056] A connect request was made on an already connected socket
Client.py
files={
"file1":"10.10.10.10",
"files2":"10.10.10.15"
}
json={
"filename" : ""
}
while 1:
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for file,ip in files:
json["filename"] = file
requestJSON = json.dumps(json)
socket.connect((ip, 3875))
socket.send(bytes(requestJSON, encoding='utf-8'))
with open("loc/" + file, 'w') as f:
while True:
print('receiving data...')
data = socket.recv(1024)
if not data:
break
f.write(data)
f.close()
socket.close()
Server.py
while 1:
conn, addr = socket.accept()
msg=conn.recv(1024)
file=json.loads(msg.decode('utf-8'))
fp = "/loc/" + file["filename"]
f = open(fp, 'rb')
d = f.read(1024)
while d:
clientSock.send(d)
l = f.read(1024)
f.close()
f = open(fp, 'rb')
d = f.read(1024)
while d:
clientSock.send(d)
l = f.read(1024)
f.close()
Should not be there d = f.read(1024) ?

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()

Categories