2

If there's an easier, cleaner way of doing this, please let me know. I'm trying to replicate this call:

curl -s -u user -k "https://10.10.10.10:8001/h/data/ps4"

This call requires a password. What I've done to try to mimic this is:

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "https://10.10.10.10:8001"
password_mgr.add_password(None, top_level_url, 'user', 'pass')
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(urllib2.HTTPHandler, handler)
request = urllib2.Request('https://10.10.10.10:8001/h/data/ps4')
print urllib2.urlopen(request).read()

However, this doesn't seem to work, as I get:

File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Unauthorized

The output from the curl command returns a list of strings (e.g., ["a","b","c"]) -- Ideally I'd like to store this into a python list somehow, but I can't get past the first bit. What am I doing wrong?

6
  • 3
    Yes there is. Use the requests lib. docs.python-requests.org/en/latest Commented Oct 24, 2014 at 15:15
  • Same as @VincentBeltman Python's HTTP handling is rather low level. You want to use 3rd party modules for most uses. Commented Oct 24, 2014 at 15:16
  • "Cleaner" is pretty subjective. I suggest we close this as opinion based. Flagged. Commented Oct 24, 2014 at 15:20
  • @DanielAmaya - did you even read the question? Commented Oct 24, 2014 at 15:21
  • 1
    @DanielAmaya - No, you didn't read my question. The question itself has nothing to do with my first statement in the body of the question.. read past the first line before flagging. Commented Oct 24, 2014 at 15:24

1 Answer 1

3

If you want to use the requests library, you can do it like this (from the documentation):

>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>

There is a more detailed article on how requests can do authentication here.

Sign up to request clarification or add additional context in comments.

5 Comments

I appreciate this a bunch - however I'm unsure how to replicate the -k parameter in my curl call through this? edit sike, found it: verify=False
So, this appears to work, however when I try to print r.text it's just blank... however print r is equivalent to yours, and r.status_code is 200 as well.
What type of data is returned? You could try r.content (binary data) or r.json() if it's a json response
r.content is blank as well - it should just return lists of strings like ["a","b","c"] ["d","e","f"]
Nevermind - it appears our service is down and no one noticed. curl isn't working either now. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.