1

I want to do a simple file transfer program that transfer file from client to server. The server sends a path to the client and the client send the file to the server. But I don't know how to create the file in the server part. In fact, I send all the file in binary mode but after that I receive every bytes in the server mode I don't know how to make the file from the bytes that I receive .

Client Part

sock = 0
path = " "


def open_socket():
    global sock
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(("localhost", 9000))


def transfer_file(path):
    f = open(path, "rb").read()
    sock.send(f)


def main():
    open_socket()
    path = sock.recv(1024)



if __name__ == "__main__":
    main()

Server Part

def open_connection():
    global client
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(("0.0.0.0", 9000))
    sock.listen(1)
    client, address = sock.accept()


def file_transfer():
    path = raw_input("Enter path of file: ")
    client.send(path)
    while True:
        file_from_client = client.recv(1024)


def main():
    open_connection()
    file_transfer()



if __name__ == "__main__":
    main()

Thanks you !

Rony Cohen
  • 87
  • 2
  • 14
  • is there any reason not to use http or sftp or any other existing protocols to send file to the server? Here's a [code example how to upload a local file via ssh using fabric](http://stackoverflow.com/q/5314711/4279) – jfs Nov 26 '16 at 20:45

1 Answers1

2

You need a bit more work on your sockets including using sendall instead of send (which may send less than the full buffer) and figuring out a way for the reading side to know when data has finished sending. Since you don't have message boundaries that would let you ask for multiple files, my example closes the socket, letting the other side know the transfer is done. I also got rid of global variables. This will transfer one file:

client.py

import socket

def open_socket():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(("localhost", 9000))
    return sock

def transfer_file(sock, path):
    f = open(path, "rb").read()
    sock.sendall(f)

def main():
    sock = open_socket()
    path = sock.recv(1024)
    transfer_file(sock, path)
    sock.shutdown(socket.SHUT_RDWR)
    sock.close()

if __name__ == "__main__":
    main()

server.py

import socket
import os

def open_connection():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(("0.0.0.0", 9000))
    sock.listen(1)
    client, address = sock.accept()
    sock.close()
    return client

def file_transfer(client):
    path = raw_input("Enter path of file on client: ")
    local_path = os.path.join(os.getcwd(), os.path.basename(path))
    print 'will write to local file', local_path
    client.send(path)
    with open(local_path + '.tmp', 'wb') as outfile:
        while True:
            block = client.recv(1024)
            if not block:
                break
            outfile.write(block)
    os.rename(local_path + '.tmp', local_path)
    print 'wrote', os.stat(local_path).st_size, 'bytes'

def main():
    client = open_connection()
    file_transfer(client)
    client.close()



if __name__ == "__main__":
    main()
tdelaney
  • 73,364
  • 6
  • 83
  • 116