[Tutor] python + sharepoint
Brad Hudson
brad.hudson at gmail.com
Fri Mar 2 23:14:44 CET 2012
On Fri, Mar 2, 2012 at 11:03 AM, Brad Hudson <brad.hudson at gmail.com> wrote:
> Can someone assist in a very basic task of retrieving a '.txt' file
> from a sharepoint document library using Python 2.4 standard
> libraries? I'm certain, I need to build up some form of authentication
> to pass, but do not know where to begin. Also, I do not want to
> attempt to customize or install additional packages to the server for
> this (Solaris).
>
> Note: url is changed below for obvious reasons, but the traceback is untouched.
>
> # ./get_file.py
> Traceback (most recent call last):
> File "./get_file.py", line 7, in ?
> result = urllib2.urlopen(url).readlines()
> File "/usr/lib/python2.4/urllib2.py", line 130, in urlopen
> return _opener.open(url, data)
> File "/usr/lib/python2.4/urllib2.py", line 364, in open
> response = meth(req, response)
> File "/usr/lib/python2.4/urllib2.py", line 471, in http_response
> response = self.parent.error(
> File "/usr/lib/python2.4/urllib2.py", line 402, in error
> return self._call_chain(*args)
> File "/usr/lib/python2.4/urllib2.py", line 337, in _call_chain
> result = func(*args)
> File "/usr/lib/python2.4/urllib2.py", line 480, in http_error_default
> raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
> urllib2.HTTPError: HTTP Error 400: Bad Request
> [root at ilhsf001h001]# cat get_file.py
> #!/usr/bin/env python
>
> import urllib2
>
> url = 'http://myserver.com/myfile.txt'
>
> result = urllib2.urlopen(url).readlines()
>
> print result
> #
Still no response on this one, but I did find a 'rather ugly' solution
using subprocess for wget to get a file from sharepoint...
#!/usr/bin/env python
import getpass
import re
import shlex
from subprocess import Popen, PIPE
# build sharepoint user/pw
# note: shlex.split requires both a raw and escaped domain string
user = r'DOMAIN\\ID'
pw = getpass.getpass('password: ')
url = 'http://myserver.com/myfile.txt'
cmd = 'wget --no-proxy --user=%s --password=%s -O- %s' % (user, pw, url)
# create the args for subprocess.Popen
args = shlex.split(cmd)
# get the tuple from the command
s = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()
# parse the stdout part of the tuple to get the exact list I want
vms = s[0].rstrip().split('\n')
vms[:] = (re.sub('\|.*$', '', vm.rstrip()) for vm in sorted(vms))
vms[:] = (vm for vm in vms if not re.search('primary', vm))
print vms
More information about the Tutor
mailing list