Read Remote File with Access Permissions

佐手、 提交于 2019-12-29 01:29:13

问题


I am trying to read a file on a network server (from a Windows XP machine), which I would normally access by asking for \\ServerName\dirPath\ in the run dialog.

Right now, I have to write a program that reads the file off the server and returns some results on my PC.

The problem that I'm facing is that the login name on my PC does not have a login account on the server. As a result, I am unable to read the file on the server when I tried:

f = open(r'\\server\path\to\file', 'r')

I know that I can fix this by adding a user account to the server or to my PC, but short of those solutions, is there a way for me to pass login credentials to the server?

I am running Windows XP on my PC and some flavor of Linux on the server

Thank you


回答1:


You say it's a Samba share - have a look at PySmbClient. That way, you can do something like this:

client = smbclient.SambaClient(server="servername", share="sharename",
    username="foo", password="bar", domain="baz")
f = smb.open('/path/to/file')
data = f.read()
f.close()

Alernatives are available, such as PySamba.




回答2:


Impersonation using the win32 modules might work for you. See this ActiveState Recipe

I have used the Technet Runas command line tool manually before. In a pinch you could use that with a subprocess call to copy the file to a local temp file.



来源:https://stackoverflow.com/questions/9202326/read-remote-file-with-access-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!