简体   繁体   English

使用Python和urllib2进行Windows身份验证

[英]Windows Authentication with Python and urllib2

I want to grab some data off a webpage that requires my windows username and password. 我想从需要我的Windows用户名和密码的网页上获取一些数据。

So far, I've got: 到目前为止,我有:

opener = build_opener()
try:
    page = opener.open("http://somepagewhichneedsmywindowsusernameandpassword/")
    print page
except URLError:
    print "Oh noes."

Is this supported by urllib2? 这是urllib2支持的吗? I've found Python NTLM , but that requires me to put my username and password in. Is there any way to just grab the authentication information somehow (eg like IE does, or Firefox, if I changed the network.automatic-ntlm-auth.trusted-uris settings). 我找到了Python NTLM ,但这需要我输入我的用户名和密码。有没有办法只是以某种方式获取身份验证信息(例如IE,或Firefox,如果我更改了network.automatic-ntlm-auth.trusted-uris设置)。

Edit after msander's answer 在msander的回答之后编辑

So I've now got this: 所以我现在得到了这个:

# Send a simple "message" over a socket - send the number of bytes first,
# then the string.  Ditto for receive.
def _send_msg(s, m):
    s.send(struct.pack("i", len(m)))
    s.send(m)

def _get_msg(s):
    size_data = s.recv(struct.calcsize("i"))
    if not size_data:
        return None
    cb = struct.unpack("i", size_data)[0]
    return s.recv(cb)

def sspi_client():
    c = httplib.HTTPConnection("myserver")
    c.connect()
    # Do the auth dance.
    ca = sspi.ClientAuth("NTLM", win32api.GetUserName())
    data = None
    while 1:
        err, out_buf = ca.authorize(data) # error 400 triggered by this line
        _send_msg(c.sock, out_buf[0].Buffer)

        if err==0:
            break

        data = _get_msg(c.sock)

    print "Auth dance complete - sending a few encryted messages"
    # Assume out data is sensitive - encrypt the message.
    for data in "Hello from the client".split():
        blob, key = ca.encrypt(data)
        _send_msg(c.sock, blob)
        _send_msg(c.sock, key)
    c.sock.close()
    print "Client completed."

which is pretty well ripped from socket_server.py (see here ). 这很好地从socket_server.py (见这里 )。 But I get an error 400 - bad request. 但我得到一个错误400 - 错误的请求。 Does anyone have any further ideas? 有没有人有任何进一步的想法?

Thanks, 谢谢,

Dom 大教堂

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. 假设您正在Windows上编写客户端代码并需要无缝的NTLM身份验证,那么您应该从python-win32邮件列表中读取Mark Hammond 在NTLM帖子中的Hooking,它基本上回答了同样的问题。 This points at the sspi example code included with the Python Win32 extensions (which are included with ActivePython and otherwise can be downloaded here ). 这指向Python Win32扩展中包含的sspi示例代码( ActivePython中包含这些代码,也可以在此处下载 )。

There are several forms of authentication that web sites can use. 网站可以使用多种形式的身份验证。

  1. HTTP Authentication. HTTP身份验证。 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. 有一个“授权”标题随页面一起告诉浏览器(或使用urllib2的程序)该怎么做。

    In this case, you must configure your urlopener to provide the answers that the authorization header needs to see. 在这种情况下,您必须配置urlopener以提供授权标头需要查看的答案。 You'll need to build either an HTTPBasicAuthHandler or HTTPDigestAuthHandler . 您需要构建HTTPBasicAuthHandlerHTTPDigestAuthHandler

    AuthHandlers require a PasswordManager . AuthHandlers需要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. 这个密码管理器可能有一个硬编码的用户名和密码(很常见),或者它可能很聪明,可以从某些Windows API中找出你的Windows密码。

  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. 这是Web应用程序将您引导到包含您使用用户名和密码填写的表单的页面。 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. 在这种情况下,您的Python程序必须使用urllib2来执行POST( 带数据请求 ),其中数据是正确填写的表单。 The reply to the post usually contains a cookie, which is what allows you further access. 对帖子的回复通常包含一个cookie,允许您进一步访问。 You don't need to worry much about the cookie, urllib2 handles this automatically. 你不需要担心cookie,urllib2会自动处理这个问题。

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. 来自page.info()的响应包括所有标头(在page.info() )以及页面内容。

Read HTTP Authentication in Python 在Python中读取HTTP身份验证

How would one log into a phpBB3 forum through a Python script using urllib, urllib2 and ClientCookie? 如何使用urllib,urllib2和ClientCookie通过Python脚本登录phpBB3论坛?

How do you access an authenticated Google App Engine service from a (non-web) python client? 如何从(非Web)python客户端访问经过身份验证的Google App Engine服务?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM