23

spending some time studying pycurl and libcurl documentation, i still can't find a (simple) way, how to get HTTP status message (reason-phrase) in pycurl.

status code is easy:

import pycurl
import cStringIO

curl = pycurl.Curl()
buff = cStringIO.StringIO()
curl.setopt(pycurl.URL, 'http://example.org')
curl.setopt(pycurl.WRITEFUNCTION, buff.write)
curl.perform()

print "status code: %s" % curl.getinfo(pycurl.HTTP_CODE)
# -> 200

# print "status message: %s" % ???
# -> "OK"

5 Answers 5

32

i've found a solution myself, which does what i need, but could be more robust (works for HTTP).

it's based on a fact that captured headers obtained by pycurl.HEADERFUNCTION include the status line.

import pycurl
import cStringIO
import re

curl = pycurl.Curl()

buff = cStringIO.StringIO()
hdr = cStringIO.StringIO()

curl.setopt(pycurl.URL, 'http://example.org')
curl.setopt(pycurl.WRITEFUNCTION, buff.write)
curl.setopt(pycurl.HEADERFUNCTION, hdr.write)
curl.perform()

print "status code: %s" % curl.getinfo(pycurl.HTTP_CODE)
# -> 200

status_line = hdr.getvalue().splitlines()[0]
m = re.match(r'HTTP\/\S*\s*\d+\s*(.*?)\s*$', status_line)
if m:
    status_message = m.groups(1)
else:
    status_message = ''

print "status message: %s" % status_message
# -> "OK"
Sign up to request clarification or add additional context in comments.

Comments

8

This is an old thread but I got here looking for similar information. If it is just the status code you're looking for, such as 200, 404, 500 etc. then just do:

your_curl_handle.getinfo(pycurl.RESPONSE_CODE)

which should return a numerical status code :)

2 Comments

This is the right way of checking for a status code
I am getting pycurl.error: cannot invoke getinfo() - no curl handle error.
2

I think that you can use human_curl library to create you code simple.

>>> import human_curl as hurl
>>> r = hurl.get('http://example.org')
>>> print r.status_code
200

Full documentation on human_curl you can get on https://github.com/Lispython/human_curl

1 Comment

"reason-phrase" is a term used by the HTTP protocol documention to refer to the human readable string that comes after the status_code. This example just returns the numeric status code.
0

If you only want the code, you can do, and assuming your pycurl.Curl() instance is called curl (ie. curl = pycurl.Curl()), you can do

curl.getinfo(pycurl.RESPONSE_CODE)
curl.getinfo(pycurl.HTTP_CODE)

But the nice way in my opinion is to parse the header yourself instead of letting libraries spoon-feed you everything.

Comments

-1

Try BaseHTTPServer.BaseHTTPRequestHandler.responses, it should contain an errorcode dictionnary as explained in this page.

hope this helps.

1 Comment

that's a possible solution.. i should have pointed out, that i want to have the status message from the server, which may be different, than the standardized one

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.