Windows Authentication with Python and urllib2

こ雲淡風輕ζ 提交于 2019-11-30 03:41:36

Assuming you are writing your client code on Windows and need seamless NTLM authentication then you should read Mark Hammond's Hooking in NTLM post from the python-win32 mailing list which essentially answers the same question. This points at the sspi example code included with the Python Win32 extensions (which are included with ActivePython and otherwise can be downloaded here).

S.Lott

There are several forms of authentication that web sites can use.

  1. HTTP Authentication. This where the browser pops up a window for you to enter your username and password. There are two mechanisms: basic and digest. There is an "Authorization" Header that comes along with the page that tells a browser (or a program using urllib2) what to do.

    In this case, you must configure your urlopener to provide the answers that the authorization header needs to see. You'll need to build either an HTTPBasicAuthHandler or HTTPDigestAuthHandler.

    AuthHandlers require a PasswordManager. This password manager could have a hard-coded username and password (very common) or it could be clever and work out your Windows password from some Windows API.

  2. Application Authentication. This is where the web application directs you to a page with a form you fill in with a username and password. In this case, your Python program must use urllib2 to do a POST (a request with data) where the data is the form filled in properly. The reply to the post usually contains a cookie, which is what allows you further access. You don't need to worry much about the cookie, urllib2 handles this automatically.

How do you know which you have? You dump the headers on the response. The response from urllib2.openurl includes all the headers (in page.info()) as well as the page content.

Read HTTP Authentication in Python

How would one log into a phpBB3 forum through a Python script using urllib, urllib2 and ClientCookie?

How do you access an authenticated Google App Engine service from a (non-web) python client?

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