How to get HTTP status message in (py)curl?

mykhal picture mykhal · Apr 27, 2010 · Viewed 21.5k times · Source

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"

Answer

mykhal picture mykhal · Apr 29, 2010

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"