[Tutor] python + sharepoint

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] python + sharepoint

Brad Hudson
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@ilhsf001h001]# cat get_file.py
#!/usr/bin/env python

import urllib2

url = 'http://myserver.com/myfile.txt'

result = urllib2.urlopen(url).readlines()

print result
#
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] python + sharepoint

Brad Hudson
On Fri, Mar 2, 2012 at 11:03 AM, Brad Hudson <[hidden email]> 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@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
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] python + sharepoint

Steven D'Aprano-8
In reply to this post by Brad Hudson
Brad Hudson 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?

What's a sharepoint document library?

How would you retrieve a text file from it *without* using Python?

[...]
> Note: url is changed below for obvious reasons, but the traceback is untouched.

Not obvious to me.


[...]
> urllib2.HTTPError: HTTP Error 400: Bad Request

Does this help?

http://www.checkupdown.com/status/E400.html


My wild guess is that sharepoint (whatever that is) is expecting something in
the url request that you're not giving; or that it doesn't like your useragent
and/or referer and lies about the problem.



--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] python + sharepoint

Steven D'Aprano-8
In reply to this post by Brad Hudson
Brad Hudson wrote:

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

Ah.

Well that explains why your version using urllib2 failed -- you don't give a
username or password, and sharepoint (whatever that is!) requires one.

You might have said.

You can read the Fine Manual, which describes how to fetch Internet resources:

http://docs.python.org/howto/urllib2.html

That is written for Python 2.7, here's one for 2.4:

http://docs.python.org/dev/howto/urllib2.html


Since you need to authenticate, this will probably be helpful:

http://www.voidspace.org.uk/python/articles/authentication.shtml



--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor