Python 使用pysmb将pdf文件发送到远程共享文件夹

Python 使用pysmb将pdf文件发送到远程共享文件夹,python,pdf,copy,paste,smb,Python,Pdf,Copy,Paste,Smb,我正在尝试编写一个函数,它允许我将本地pdf文件发送到远程网络共享 它需要在python中,因为我希望稍后使用代码将其作为模型的函数在django中使用。pysmb提供以下功能: storeFile(服务名称、路径、文件对象、超时=30) 将文件\u obj的内容存储在服务\u名称的路径上。如果远程服务器上已存在该文件,则将截断并覆盖该文件 参数: service\u name(string/unicode)–路径的共享文件夹的名称 path(字符串/unicode)–远程服务器上文件的路径。如

我正在尝试编写一个函数,它允许我将本地pdf文件发送到远程网络共享

它需要在python中,因为我希望稍后使用代码将其作为模型的函数在django中使用。pysmb提供以下功能:

storeFile(服务名称、路径、文件对象、超时=30)

将文件\u obj的内容存储在服务\u名称的路径上。如果远程服务器上已存在该文件,则将截断并覆盖该文件

参数

service\u name
(string/unicode)–路径的共享文件夹的名称

path
(字符串/unicode)–远程服务器上文件的路径。如果路径处的文件不存在,将创建该文件。否则,它将被覆盖。如果路径引用文件夹或无法打开文件进行写入,则会引发操作失败

file\u obj
–具有读取方法的类似文件的对象。数据将从文件_obj持续读取,直到EOF

返回
上载的字节数

我还不知道怎么解决这个问题。或者更确切地说,我必须解决两个子问题:

  • 将pdf文件放入
    文件\u obj
  • 正确确定
    服务\u名称
    路径
  • 对于1:我发现包
    shutil
    不太可能工作,因为它是为机器中的本地操作而设计的。什么适合这种情况?我看到了一个例子,客户端到主机之间总是有读写操作,反之亦然,但实际上如何使用PDF来实现呢?读写PDF不像.txt文件那么容易

    对于2:我的pdf文件在远程windows计算机上的目标的完整路径是(即,我在远程windows计算机上用右键单击->属性选中):

    abc123.example.com/shared_folder/shared_files_with_all/PersonA/mypdf.pdf

    service\u name
    应该是
    路径的目录名
    所以我选择了:

    service\u name=“共享文件夹”

    path=shared\u files\u与\u all/PersonA/

    a。我使用windows时是否需要使用两个反斜杠“\\”

    b。对于
    服务\u名称
    路径
    ,我得到以下错误:

    smb.smb_structs.OperationFailure: Failed to list path on service_name: Unable to connect to shared device
    
    这里有关于这个错误的说法。然而,不幸的是,我试过了,但没用

    最后是我的代码:

    import tempfile
    from smb.SMBConnection import SMBConnection
    
    # There will be some mechanism to capture userID, password, client_machine_name, server_name and server_ip
    # client_machine_name can be an arbitary ASCII string
    # server_name should match the remote machine name, or else the connection will be rejected
    
    userID = "userid"
    password = "mysecretpassword"
    client_machine_name = "clientname"
    server_name = "abc123.example.com"
    service_name = "shared_folder"
    path = "shared_files_with_all/PersonA/"
    ip = "xxx.xxx.xxx.xxx"
    
    
    conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True)
    assert conn.connect(ip, port=139)
    
    # Don't know how to put in the pdf in here yet
    file_obj = tempfile.NamedTemporaryFile()
    
    conn.storeFile(service_name, path, file_obj, timeout=30)
    file_obj.close()
    
    conn.close()