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

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


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 pycurlimport cStringIOimport recurl = 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)# -> 200status_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"


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


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_code200

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