简体   繁体   English

python机械化访问Sharepoint网站

[英]python mechanize to access Sharepoint website

I'm trying to access Sharepoint using mechanize but i got a 401 error. 我正在尝试使用机械化访问Sharepoint,但出现401错误。 Here's the code i'm using: 这是我正在使用的代码:

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

Did i miss anything? 我想念什么吗?

Did you happen to try Python Ntlm for accessing SharePoint? 您是否碰巧尝试使用Python Ntlm访问SharePoint?

Examples in the Ntlm doc will explain how to use it with Urllib2. Ntlm文档中的示例将说明如何将其与Urllib2一起使用。 Pasted below the code for using NTLM authentication using 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()

Try with: 尝试:

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

instead of 代替

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

This should work if your sharepoint server provides Basic Auth. 如果您的Sharepoint服务器提供基本身份验证,则此方法应该有效。

Looking at the usage in the mechanize docs you only need to specify the username (eg 'john_doe' , try this 查看机械化文档中的用法,您只需要指定用户名即可(例如'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

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

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