【问题标题】:accessing files via python using a service account使用服务帐户通过 python 访问文件
【发布时间】:2018-05-30 10:15:28
【问题描述】:

我正在使用 windows server 2012 r2。 我在服务器上有一些文件。我有一个单独的服务帐户,它可以读取文件。我想要做的是使用 python 通过网络共享访问文件(欢迎任何其他建议),但只能通过服务帐户。

PS:我无法使用 RDP。

【问题讨论】:

  • 所以,我使用了 command = "net use /user:" + USER + " " + REPOSITORY_PATH + " " + PASS.. 它可以工作

标签: python windows ubuntu-16.04 pywin32


【解决方案1】:

此任务的底层 WINAPI[MS.Docs]: WNetAddConnection2W function 系列的一部分。

[GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions 包装器是 [ActiveState]: Module win32wnet(它不是官方文档(我现在找不到任何文档)- 我不知道 URL 的有效期有多长,但这是我能找到的最好的)。

我准备了一个简单的例子。

code00.py

#!/usr/bin/env python3

import sys
import os
import pywintypes
import win32wnet


CONNECT_INTERACTIVE = 0x00000008

HOST_NAME = "192.168.1.3"
SHARE_NAME = "Work"
SHARE_FULL_NAME = os.path.sep * 2 + os.path.sep.join((HOST_NAME, SHARE_NAME))
SHARE_USER = "cfati"
SHARE_PWD = "********"


def main():
    net_resource = win32wnet.NETRESOURCE()
    net_resource.lpRemoteName = SHARE_FULL_NAME
    flags = 0
    #flags |= CONNECT_INTERACTIVE
    print("Trying to create connection to: {:s}".format(SHARE_FULL_NAME))
    try:
        win32wnet.WNetAddConnection2(net_resource, SHARE_PWD, SHARE_USER, flags)
    except pywintypes.error as e:
        print(e)
    else:
        print("Success!")


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

注意事项

  • 密码被混淆了(很明显)
  • 这是最简单的功能(相当于您的命令),但该功能可以做的更多:

    • 我想指出一件事。如果你:

      • 输入一些无效的凭据,然后
      • 删除flags |= CONNECT_INTERACTIVE

      然后会弹出一个凭据对话框

输出

(py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use
New connections will be remembered.

There are no entries in the list.


(py35x64_test) e:\Work\Dev\StackOverflow\q050602112>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code00.py
Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32

Trying to create connection to: \\192.168.1.3\Work
Success!

(py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use
New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK                     \\192.168.1.3\Work        Microsoft Windows Network
The command completed successfully.


(py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use * /delete /y
You have these remote connections:

                    \\192.168.1.3\Work
Continuing will cancel the connections.

The command completed successfully.


(py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use
New connections will be remembered.

There are no entries in the list.

【讨论】:

  • 当然不是。 pywin32Win 特定的。这个问题还有 windows 标签(没有提到会涉及 Lnx)。
  • 对 Linux 版本有任何想法吗?
  • 此文件共享协议是 Win 特定的 (SMB / CIFS)。从 Lnx 可以使用 Samba。我不知道如何从 Python 访问它(是否有 pysmb 或类似的模块)。但无论如何,我认为这可能是另一个问题的主题。
猜你喜欢
  • 1970-01-01
  • 2019-07-25
  • 2021-09-05
  • 2020-05-13
  • 2019-08-03
  • 1970-01-01
  • 2017-11-17
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多