3

Python 3.4, Django 1.7, Windows apache 2.4.12

I am trying to list all files on a Windows shared drive (which is restricted to certain users), and later write couple of files to the shared drive.

I am using os.listdir to do this. It works well if I just run the web app on my machine, but once it's deployed on the actual server it will stop working.

The problem is the permission on the Windows shared drive. User has to login first so I do have their username and password.

My question is how to supply os.listdir with username and password?

I tried os.listdir('//windows/share/drive/dir@domanin/username:password') but the system will try to look for the file instead of passing username and password.

Does any one know how to solve this? Or I need to map the drive (how do I map drive with credential?), list files, write files, then disconnect the mapped drive?

Thank you so much.

wolf97084
  • 270
  • 4
  • 22
  • That is not really a python problem, but a Windows problem. UNC paths cannot handle username and password in the path (they are not URLs). Considering this, the question seems a possible duplicate of https://stackoverflow.com/questions/8172079/copy-files-over-network-via-file-share-user-authentication – dhke Mar 04 '15 at 21:33

2 Answers2

6

You should take a look on net use Windows command. It lets you to mount any network resources with given credentials.

Before accessing network resource, you can execute from Python net use command, like this:

net use \\computername\path\to\dir /user:username password

If credentials are correct, network resource will be accessible, and os.listdir() will work.

There can be some issues if given resource was already mounted with different credentials. In such case you should unmount them first (net use \\computername\path\to\dir /delete)

Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
  • thank you. I am running into unmounting issue. Is there anyway to tell if a user has permission on that drive instead of mounting the drive? – wolf97084 Mar 05 '15 at 13:20
  • You can try to mount network resource (calling ```net use \\computername\path\to\dir```) without credentials — in this case credentials of user, which runs Python code will be used. If the operaction will fail — it means user has no permission. – Ihor Pomaranskyy Mar 05 '15 at 13:26
  • Another question, if I try to map drive in the web app, it will try to map the drive on the server or the local user machine? – wolf97084 Mar 05 '15 at 14:09
  • The first: you don't "map drive", you mount network resource. The second: the network resource is mounted to the computer, where you run ```net use```. Try to play with ```net use``` in the command prompt at your local computer to find out how it works. ```net use``` without any options will show you a list of mounted resources (N.B. if resource is mounted is doesn't necessarily means it is accessible). ```/delete``` key lets you to dismount resource. And by calling ```net use``` with path to resouce (and, optionally, credentials) lets you mount given resource. – Ihor Pomaranskyy Mar 05 '15 at 14:29
  • 1
    I appreciate all your help! This is the right answer however doesn't solve my problem due to org network setup. I just cannot map drives on the server. :( Works totally find on my local machine. – wolf97084 Mar 06 '15 at 16:44
0

According to the Python manual os.listdir(path) only takes the path name as the argument. It probably works for you locally using the Django test server as permissions are different in that environment than using a production webserver like Apache or Nginx. Try looking at the following pre-existing related SO questions and answers - they might offer you some guidance, especially on a Win box:

What is the best way to map windows drives using Python?

python copy files to a network location on Windows without mapping a drive

Community
  • 1
  • 1
tatlar
  • 3,080
  • 2
  • 29
  • 40