Copying files to remote computer with Python 2.6 (or higher)

Last edited on

The multiprocessing module was added to Python's standard library with Python 2.6. This module makes it quite easy to create client/server applications using Python.

Here is an example that shows how to copy a file from a local computer to a remote computer:

import subprocess
import sys
import multiprocessing.managers

host = "127.0.0.1"
port = 8989
password = "Secret"
commands = ["echo %USERPROFILE%", "dir \\", "echo $HOME", "ls /", "non_existing_command"]

def write_file(file_name, file_contents):
    fh = None
    try:
        fh = open(file_name, "wb")
        fh.write(file_contents)
        return True
    except:
        return False
    finally:
	if fh is not None:
	    fh.close()

class RemoteManager(multiprocessing.managers.BaseManager):
    pass

RemoteManager.register("write_file", write_file)

def start_server(port, password):
    print "Listening for incoming connections..."
    mgr = RemoteManager(address=('', port), authkey=password)
    server = mgr.get_server()
    server.serve_forever()

def write_file_remote(host, port, password, file_name_remote, file_contents):
    mgr = RemoteManager(address=(host, port), authkey=password)
    mgr.connect()
    print mgr.write_file(file_name_remote, file_contents)._getvalue()
    print

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "--server":
        start_server(port, password)
    elif len(sys.argv) > 3 and sys.argv[1] == "--client":
	fh = None
	try:
	    fh = open(sys.argv[2], "rb")
	    file_contents = fh.read()
	finally:
	    if fh is not None:
		fh.close()
        file_name_remote = sys.argv[3]
        write_file_remote(host, port, password, file_name_remote, file_contents)
    else:
        print "usage: python " + sys.argv[0] + " [ --server | --client <local_file_name> <remote_file_name>]"

Example execution:

C:\U\me\bin> start /b python remotecp.py --server

C:\U\me\bin> Listening for incoming connections...

C:\U\me\bin> python remotecp.py --client C:\Users\me\Desktop\test.txt C:\Users\me\Desktop\test2.txt

True