简体   繁体   English

在Win 7上使用Python访问PHP中的网络驱动器

[英]Accessing network drive in PHP using Python on Win 7

I am trying to access network drive share from python. 我正在尝试从python访问网络驱动器共享。 I have basic check: 我有基本检查:

if not os.access('\\\\path', os.W_OK):
     print 'Not accessible' 
else:
     print 'Accessible'

It works but when I call that script from php it says 'Not accessible'. 它可以工作,但是当我从php调用该脚本时,它说“无法访问”。

I thought that the problem could be due to the fact that PHP doesn't run under user account I am logged in with, but how do I specify user that has proper permissions? 我以为该问题可能是由于PHP无法在我登录时使用的用户帐户下运行,但是如何指定具有适当权限的用户呢?

I call python script through exec command: 我通过exec命令调用python脚本:

exec('python "path-to.py"');

I am using Win 7 machine. 我正在使用Win 7机器。 If I add RunAs /user:username password to the end of the exec command - nothing happens. 如果我在exec命令的末尾添加RunAs /user:username password -则不会发生任何事情。

Solution by OP. 由OP解决。

As PHP script didn't have access to a network share, I decided to map the network drive using PHP. 由于PHP脚本无法访问网络共享,因此我决定使用PHP映射网络驱动器。 To do so I used the following 2 lines: 为此,我使用了以下两行:

$WshNetwork = new COM("WScript.Network");
$WshNetwork->MapNetworkDrive("L:", '\\\\path', FALSE, 'domain\\username', 'password');

which I learned about on this thread: link (be aware there are two pages) 我在此线程上学到的: 链接 (请注意有两个页面)

When connecting for the first time using COM, I got the error: 首次使用COM连接时,出现错误:

Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed <...> 不允许同一用户使用多个用户名与服务器或共享资源建立多个连接<...>

which basically means that you are already connected to the network drive. 这基本上意味着您已经连接到网络驱动器。 To resolve it, I used net use command in the following combo: 要解决此问题,我在以下组合中使用了net use命令:

net use * /delete /noconfirm
taskkill /f /IM explorer.exe
explorer.exe
net use x: \\myServer\mySHaredFolder

which I found out here: link 我在这里找到的: 链接

You can use net use command to see what connections are remembered (= present). 您可以使用net use命令查看记住的连接(=存在)。 Once the connections are listed, you then use either net use /delete \\\\your_connection to delete the connection you want to, or net use * /delete if you want to delete all connections. 列出连接后,然后使用net use /delete \\\\your_connection删除想要的连接,或者net use * /delete如果要删除所有连接)。 After that you need to reboot your computer, but to avoid doing so you can simply restart the explorer.exe by first shutting it down on the console: taskkill /f /IM explorer.exe and then restarting: explorer.exe 之后,您需要重新启动计算机,但要避免重新启动计算机,只需先在控制台上关闭它,即可重新启动explorer.exetaskkill /f /IM explorer.exe ,然后重新启动: explorer.exe

I also had some 'dead links' mapped to the drives. 我还映射了一些“死链接”到驱动器。 How to delete 'dead links' I learned from here: 我从这里学到的如何删除“死链接”:

'How To Disconnect Non-Mapped UNC Path “Drives” in Windows' thread: link “如何断开Windows中未映射的UNC路径“驱动器”的连接 ”线程: 链接

which basically directs you to clear the registry: 基本上可以指导您清除注册表:

  1. Run cmd as administrator 以管理员身份运行cmd
  2. Type regedit 输入regedit
  3. Navigate to HKCU\\software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\mountPoints2 导航到HKCU\\software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\mountPoints2
  4. Delete suspect entries and reboot 删除可疑条目并重新启动

After that your Explorer (My Computer) should be clear and COM should work. 之后,您的资源管理器(“我的电脑”)应该清除并且COM应该可以工作。 PHP is now able to get output from python script which does whatever needed on the network drive. PHP现在可以从python脚本获取输出,该脚本可以完成网络驱动器上需要执行的所有操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM