我正在尝试使用python 2.6.8 urllib2库从sharepoint服务器下载excel文件。 由于Sharepoint受密码保护,我也传递用户名和密码。
我的代码看起来像这样。
import urllib2
import os.path
theurl = 'https://Mycompany.sharepoint.com/_layouts/15/WopiFrame.aspx?sourcedoc=XXXX&file=IPS_cleaned.xlsx&action=defaultclear'
username = ''
password = ''
headers={'User-agent' : 'Mozilla/5.0'}
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
req = urllib2.Request(theurl, None, headers)
pagehandle = urllib2.urlopen(req).read()
with open('/home/neilshah/newfile.xlsx',"wb") as code:
code.write(pagehandle.read())
它给了我以下错误。
urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>
请帮我解决这个问题。
答案 0 :(得分:0)
尝试这样的事情:
import urllib2,base64
request = urllib2.Request(theurl)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)