Python 将文件从 Linux 复制到 WINdows [英] Python copy files from Linux to WIndows

查看:112
本文介绍了Python 将文件从 Linux 复制到 WINdows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个网站,它有一个表单,可以捕获用户数据并在用户数据上运行一些 cgi.cgi 的第一步是它需要将文件从 linux webserver 复制到 windows 机器.服务器将使用 Active Directory 角色帐户作为复制凭据.我曾希望简单地使用这样的东西:

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

不幸的是,当我在 bash 中运行该命令时,我收到了关于密码无效的错误.理想情况下,我会使用这种方法挂载远程 windows c$ 共享,然后复制文件,但如果它们更有意义,我愿意尝试其他模块.

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

推荐答案

我使用了 pysmb 中的 SMBConnection 类 (https://pythonhosted.org/pysmb/api/smb_SMBConnection.html).非常简单,无需安装.

I used the SMBConnection class found in pysmb (https://pythonhosted.org/pysmb/api/smb_SMBConnection.html). Very simple and no need for mounting.

 conn = SMBConnection(user, pw, myname, srv, use_ntlm_v2 = True)
 conn.connect(ip, port=139)
 file2transfer = open(filename,"r")
 conn.storeFile(share,path + filename, file2transfer, timeout=30 )

确保用户拥有文件共享的登录权限.

Make sure that the user has logon rights to the fileshare.

这篇关于Python 将文件从 Linux 复制到 WINdows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆