python mechanize访问Sharepoint网站

时间:2011-05-17 04:08:33

标签: python mechanize

我正在尝试使用mechanize访问Sharepoint,但我收到401错误。这是我正在使用的代码:

import mechanize

url = "http://sharepoint:8080/foo/bar/foobar.aspx"

br.addheaders = [('User-agent', 'Mozilla/4.0(compatible; MSIE 7.0b; Windows NT 6.0)')]
br.add_password(url, 'domain\\user', 'myPassword')
r = br.open(url)
html = r.read()

我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

您是否碰巧尝试使用Python Ntlm来访问SharePoint?

Ntlm doc中的示例将解释如何将其与Urllib2一起使用。粘贴在使用mechanize使用NTLM身份验证的代码下方。

import mechanize
from ntlm import HTTPNtlmAuthHandler
pass_manager = mechanize.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(pass_manager)

browser = mechanize.Browser()
browser.add_handler(auth_NTLM)

r = browser.open(url)
html = r.read()

答案 1 :(得分:0)

尝试:

br.addheaders = [('User-agent', 'Mozilla/4.0(compatible; MSIE 7.0b; Windows NT 6.0)'), ('Authorization', 'Basic %s:%s' % ('domain\\user', 'myPassword'))]

而不是

br.addheaders = [('User-agent', 'Mozilla/4.0(compatible; MSIE 7.0b; Windows NT 6.0)')]

如果您的sharepoint服务器提供Basic Auth,这应该有效。

答案 2 :(得分:0)

查看mechanize docs中的用法,您只需指定用户名(例如'john_doe',试试这个

...
br.add_password(url, 'username_string', 'myPassword')
r = br.open(url)
html = r.get_data() # r.get_data() can be called many times without calling seek