如何在 (py)curl 中获取 HTTP 状态消息?

发布于 2024-08-30 10:37:17 字数 431 浏览 7 评论 0原文

花了一些时间研究 pycurl 和 libcurl 文档,我仍然找不到一种(简单)方法,如何在 pycurl 中获取 HTTP 状态消息(原因短语)。

状态码很简单:

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"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流绪微梦 2024-09-06 10:37:17

我自己找到了一个解决方案,它可以满足我的需要,但可能更强大(适用于 HTTP)。

它基于这样一个事实:通过 pycurl.HEADERFUNCTION 获取的捕获标头包含状态行。

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"

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"
最初的梦 2024-09-06 10:37:17

这是一个旧线程,但我来这里寻找类似的信息。如果它只是您要查找的状态代码,例如 200、404、500 等,那么只需执行:

your_curl_handle.getinfo(pycurl.RESPONSE_CODE)

,它应该返回一个数字状态代码:)

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

琴流音 2024-09-06 10:37:17

我认为您可以使用 human_curl 库来创建简单的代码。

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

有关 human_curl 的完整文档,您可以在 https://github.com/Lispython/ human_curl 上获取

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

和影子一齐双人舞 2024-09-06 10:37:17

如果您只想要代码,则可以这样做,并假设您的 pycurl.Curl() 实例名为 curl (即 curl = pycurl.Curl()),你可以这样做,

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

但在我看来,最好的方法是自己解析标头,而不是让库给你提供一切。

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.

残龙傲雪 2024-09-06 10:37:17

尝试 BaseHTTPServer.BaseHTTPRequestHandler.responses,它应该包含一个错误代码字典,如 此页面中所述

希望这有帮助。

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

hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文