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


spending time studying pycurl , libcurl documentation, still can't find (simple) way, how http status message (reason-phrase) in pycurl.

status code 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" 

i've found solution myself, need, more robust (works http).

it's based on fact captured headers obtained pycurl.headerfunction include 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" 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -