Python copy files from Linux to WIndows

mega_mouse picture mega_mouse · Oct 10, 2016 · Viewed 10.8k times · Source

I'm building a website which has a form which captures user data and runs some cgi on the user data. One of the first steps of the cgi is that it needs to copy files from the linux webserver to windows machines. The server would be using an active directory role acount for the copy credential. I had hoped to simply use something like this:

mount -t cifs -o username=someUsername,password=somePasword //someMachine/someShare /someMountPoint

Unfortunately I get errors about the password attributed being invalid when I run that command in bash. Ideally I would use this method to mount the remote windows c$ share and then copy the files but I'm willing to try other modules if they make more sense.

I had something like this but it doesn't work, creates the necessary temporary directories but never mounts anything. I'm happy to try using something else but would love to know what's wrong here.

import subprocess
import random


def makeDir():
    tempDir = random.randrange(111111,999999)
    subprocess.Popen(["mkdir","/mntDir/"+str(tempDir)])
    return tempDir

def mountShare(hostname, username, password):
    mountDir = makeDir()
    try:
        subprocess.Popen(["mount","-t","cifs", "-o",
                      "username="+username+",password="+password,
                      "//"+hostname+"/c$",
                      "/mntDir/"+mountDir])
    except:
        print("Mounting failed")

Answer

Jean-François Fabre picture Jean-François Fabre · Oct 10, 2016

First, drop the exception block as it hides error details, anyway Popen and other subprocess methods only throw exceptions when they cannot start commands (because of command not found), which means that mount is actually called.

Second, you really don't need Popen, but call (and as a bonus you get the return code directly)

rc = subprocess.call(["mount","-t","cifs", "-o",
                      "username="+username+",password="+password,
                      "//"+hostname+"/c$",
                      "/mntDir/"+mountDir])
if rc:
   print("mount failed")

In your case, the problem is the general exception block.

This method:

def makeDir():
    tempDir = random.randrange(111111,999999)
    subprocess.Popen(["mkdir","/mntDir/"+str(tempDir)])
    return tempDir

returns an integer, so if you remove the exception block you get error because you're adding a string with an integer (TypeError: Can't convert 'int' object to str implicitly). It's a simple mistake that you could have seen if it hadn't for the stupid exception catch which mislead you.

But with the generic try/except block without any argument, you just get mount failed useless message. never protect your statements with try:/except:, that's counter-productive.

If you really want to do that,do this:

try:
    some_command
except Exception as e:
    # print detailed exception, not just "error"
    print("Something went wrong "+str(e))

Now to sum it up, here's a fixed version of your code (with some slight improvements as a bonus):

import subprocess,os
import random


def makeDir():
    # directly create directory name as a string
    tempDir = "/mntDir/{}".format(random.randrange(111111,999999))
    # no need for a subprocess, python handles this well!
    os.mkdir(tempDir)
    # returns the absolute directory name, as string
    return tempDir

def mountShare(hostname, username, password):
    mountDir = makeDir()
    rc = subprocess.call(["mount","-t","cifs", "-o",
                      "username="+username+",password="+password,
                      "//"+hostname+"/c$",
                      mountDir])
    if rc!=0:
        print("Mounting failed")