Python copy files from Linux to WIndows
<p>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:</p>

<p><pre><code></p>

<pre><code>mount -t cifs -o username=someUsername,password=somePasword //someMachine/someShare /someMountPoint
</code></pre>

<p></pre></code></p>

<p>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.</p>

<p>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.
<pre><code></p>

<pre><code>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")
</code></pre>

<p></pre></code></p>

<p>The solution was not to mount the share but rather to copy on the fly using smbclient.  The command I'm using refers to an authfile which contains an account with the relevant permissions in the form:</p>

<pre><code>username = yourUsername
password = yourPassword
domain = yourDomain
</code></pre>

<p>The permissions on this file are set to 500.</p>

<p>The smbclient command is then used to create a directory on the remote machine and copy files to that directory.</p>

<pre><code>smbclient //hostname/c$ -A /authfile -c "mkdir someDir; cd someDir/; lcd /folderToCopyFrom; prompt; recurse; mput *; exit;"
</code></pre>

<p>Thank you all for the advice, most helpful!</p>

python linux windows cifs