python我可以支持用户名和密码os.listdir吗? [英] python can I suppy username and password to os.listdir?

查看:415
本文介绍了python我可以支持用户名和密码os.listdir吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.4,Django 1.7,Windows apache 2.4.12



我正在列出Windows共享驱动器上的所有文件(仅限某些用户)然后将两个文件写入共享驱动器。



我正在使用os.listdir来执行此操作。如果我只是在我的机器上运行网络应用程序,它可以很好的工作,但一旦部署在实际的服务器上,它将停止工作。



问题是Windows上的权限共享驱动器用户必须首先登录,所以我有他们的用户名和密码。



我的问题是如何使用用户名和密码提供os.listdir?



我尝试过os.listdir '// windows / share / drive / dir @ domanin / username:password'),但系统会尝试查找文件,而不是传递用户名和密码。



有谁知道如何解决这个问题?或者我需要映射驱动器(如何将驱动器与凭证映射?),列表文件,写入文件,然后断开映射的驱动器?



非常感谢您。

解决方案

您应该看看 net use Windows命令。它允许您使用给定的凭据装载任何网络资源。



在访问网络资源之前,可以从Python执行 net use 命令,如下所示:

  net use \\computername\path\to\dir / user:用户名密码

如果凭据正确,网络资源将可访问,而 os .listdir()将工作。



如果给定的资源已经安装了不同的凭据,可能会出现一些问题。在这种情况下,您应先卸载它们( net use \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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.

解决方案

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)

这篇关于python我可以支持用户名和密码os.listdir吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆