简体   繁体   中英

Accessing network drive in PHP using Python on Win 7

I am trying to access network drive share from 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'.

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?

I call python script through exec command:

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

I am using Win 7 machine. If I add RunAs /user:username password to the end of the exec command - nothing happens.

Solution by OP.

As PHP script didn't have access to a network share, I decided to map the network drive using 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:

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 * /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). 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. 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

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

which basically directs you to clear the registry:

  1. Run cmd as administrator
  2. Type regedit
  3. Navigate to 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. PHP is now able to get output from python script which does whatever needed on the network drive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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