开发者

Downloading a File Protected by NTLM/SSPI Without Prompting For Credentials Using Python on Win32?

开发者 https://www.devze.com 2022-12-18 02:30 出处:网络
I need to download a file on a corporate Sharepoint site using CPython. Existing codebase prevents me from using Ironpython without porting the code, so .NET\'s WebClient library is out. I also want t

I need to download a file on a corporate Sharepoint site using CPython. Existing codebase prevents me from using Ironpython without porting the code, so .NET's WebClient library is out. I also want to download the file without prompting the user to save and without prompting the user for network credentials. I tried other libraries, but they all had short-comings:

  • urllib2 plus python-ntlm: requires user/pass to be provided
  • COM automation of Internet Explorer: requires the user to click 'Save'
  • subprocess using wget or cURL: couldn't get either to authenticate without requesting user/pass

I couldn't find anything in pywin32 that looks like it hooks into urllib2 or provides equivalent fu开发者_StackOverflow中文版nctionality. So, is there a way to download the file without requesting credentials and without prompting the user to click 'Save'?


I ended up finding some VB code from a Microsoft support page that uses a function from urlmon.dll I replicated it with a single line of ctypes code and it accomplished exactly what I needed it to do.

ctypes.windll.urlmon.URLDownloadToFileA(0,url,local_file_name,0,0)
  • url is the location of the resource (in this case, an Excel file on a Sharepoint site)
  • local_file_name is the local path and name of the file to be saved.

This passed credentials across the wire with no prompts.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号