使用 Python 和 urllib2 进行 Windows 身份验证 [英] Windows Authentication with Python and urllib2

查看:25
本文介绍了使用 Python 和 urllib2 进行 Windows 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从需要我的 Windows 用户名和密码的网页中获取一些数据.

I want to grab some data off a webpage that requires my windows username and password.

到目前为止,我有:

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

urllib2 支持吗?我找到了 Python NTLM,但这需要我输入用户名和密码. 有没有办法以某种方式获取身份验证信息(例如,像 IE 那样,或 Firefox,如果我更改了 network.automatic-ntlm-auth.trusted-uris 设置).

Is this supported by 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 (e.g. like IE does, or Firefox, if I changed the network.automatic-ntlm-auth.trusted-uris settings).

所以我现在有了这个:

# 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."

这是从 socket_server.py 中很好地翻录的(参见 此处).但是我收到错误 400 - 错误的请求.有人有进一步的想法吗?

which is pretty well ripped from socket_server.py (see here). But I get an error 400 - bad request. Does anyone have any further ideas?

谢谢,

多姆

推荐答案

假设您在 Windows 上编写客户端代码并且需要无缝 NTLM 身份验证,那么您应该阅读 Mark Hammond 的 hook in NTLM 来自 python-win32 邮件列表的帖子,它基本上回答了相同的问题.这指向 Python Win32 扩展中包含的 sspi 示例代码(包含在 ActivePython 和其他可以在此处下载).

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).

这篇关于使用 Python 和 urllib2 进行 Windows 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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