Python论坛  - 讨论区

标题:[zeuux-py:176] 关于urllib2的认证问题--无法认证成功 选项

2009年09月28日 星期一 17:58

di yang yangdi1227在gmail.com
星期一 九月 28 17:58:54 CST 2009

我们内网是需要认证的,我用IE通过认证时完整的抓了个包,内容如下:
GET /Pages/Default.aspx HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel, application/
vnd.ms-powerpoint, application/msword, application/x-ms-application,
application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml
+xml, */*

Accept-Language: en-US,zh-cn;q=0.5

UA-CPU: x86

Accept-Encoding: gzip, deflate

If-Modified-Since: Mon, 28 Sep 2009 09:38:01 GMT

User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; User-
agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;
http://bsalsa.com) ; .NET CLR 2.0.50727; CIBA; .NET CLR
3.0.4506.2152; .NET CLR 3.5.30729)

Host: www.intra.nsfocus.com

Connection: Keep-Alive

Authorization: NTLM
TlRMTVNTUAADAAAAGAAYAHwAAAAYABgAlAAAAAoACgBIAAAADAAMAFIAAAAeAB4AXgAAAAAAAAC-sAAAABYKIogUBKAoAAAAPaQBuAHQAcgBhAHkAYQBuAGcAZABpAEYAMwBCADgAQgBBAEUAOAA2AD-cAMAA4ADQARgA3AIxddlJ/

WVEhAAAAAAAAAAAAAAAAAAAAAMkhdPtqgH0rwYm0Ox5EDEVBL9d0WiK95w==

Cookie: MSOWebPartPage_AnonymousAccessCookie=80;
PasswordAgeChecked=True

HTTP/1.1 200 OK

Cache-Control: private, max-age=0

Content-Length: 51472

Content-Type: text/html; charset=utf-8

Expires: Sun, 13 Sep 2009 09:46:30 GMT

Last-Modified: Mon, 28 Sep 2009 09:46:30 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

MicrosoftSharePointTeamServices: 12.0.0.6211

X-AspNet-Version: 2.0.50727

Set-Cookie: WSS_KeepSessionAuthenticated=80; path=/

Set-Cookie: MSOWebPartPage_AnonymousAccessCookie=80; expires=Mon, 28-
Sep-2009 10:16:30 GMT; path=/

Date: Mon, 28 Sep 2009 09:46:30 GMT

我尝试用urllib2去认证,但是总不成功,代码如下:
import urllib2
username = 'xxx'
password ='xxx'
theurl = 'http://www.intra.nsfocus.com/Pages/Default.aspx'
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password('NTLM',theurl,username,password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
try:
    a = opener.open(theurl)
    print a.read()
except IOError,e:
    print e

结果是:HTTP Error 401: Unauthorized

这时从抓包看:
GET /Pages/Default.aspx HTTP/1.1

Accept-Encoding: identity

Host: www.intra.nsfocus.com

Connection: close

User-Agent: Python-urllib/2.5

HTTP/1.1 401 Unauthorized

Content-Length: 1327

Content-Type: text/html

Server: Microsoft-IIS/6.0

WWW-Authenticate: NTLM

X-Powered-By: ASP.NET

MicrosoftSharePointTeamServices: 12.0.0.6211

Date: Mon, 28 Sep 2009 09:36:22 GMT

Connection: close

想听听大家的经验,这块应该怎么做呢?
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: <http://www.zeuux.org/pipermail/zeuux-python/attachments/20090928/32a00bdb/attachment.html>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号