python - 如何调试请求库?

标签 python curl python-requests

我正在尝试将我的 python 脚本从通过 os.system() 发出 curl 命令转换为使用请求。我以为我会使用 pycurl,但是 this question否则说服了我。问题是我在使用 r.text ( from this answer ) 时看到服务器返回错误,但我需要更多信息。有没有更好的方法来调试正在发生的事情?

对于它的值(value),我认为问题围绕着将我的 --data 标志从 curl/pycurl 转换为请求。我已经创建了一个我之前传递给 --data 的参数的字典。我的猜测是其中一个无效,但我怎样才能获得更多信息才能确定?

例子:

headers2 = {"Accept":"*/*", \
"Content-Type":"application/x-www-form-urlencoded", \
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36", \
"Origin":"https://somedomain.com", \
"X-Requested-With":"XMLHttpRequest", \
"Connection":"keep-alive", \
"Accept-Language":"en-US,en;q=0.8", \
"Referer":"https://somedomain.com/release_cr_new.html?releaseid=%s&v=2&m=a&prev_release_id=%s" % (current_release_id, previous_release_id), \
"Host":"somedomain.com", \
"Accept-Encoding":"gzip,deflate,sdch", \
"Cookie":'cookie_val'}

for bug_id in ids:
  print bug_id
  data = {'dump_json':'1','releaseid':current_release_id, 'v':'2','m':'a','prev_release_id': previous_release_id,'bug_ids': bug_id, 'set_cols':'sqa_status&sqa_updates%5B0%5D%5Bbugid%5D=' + bug_id + '&sqa_updates%5B0%5D%5Bsqa_status%5D=6'}
  print 'current_release_id' , data['releaseid']
  print 'previous_release_id', data['prev_release_id']
  r = requests.post(post_url, data=json.dumps(data), headers=headers2)
  print r.text

我得到的输出是一个非常通用的 html 消息,我以前以错误的方式查询服务器时看到过。所以我知道我至少到达了正确的服务器。

我真的不期待任何输出。这应该只发布到服务器并更新数据库中的字段。

最佳答案

http 响应剖析

示例(加载此页面)

HTTP/1.1 200 OK
Cache-Control: public, max-age=60
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Fri, 27 Sep 2013 19:22:41 GMT
Last-Modified: Fri, 27 Sep 2013 19:21:41 GMT
Vary: *
X-Frame-Options: SAMEORIGIN
Date: Fri, 27 Sep 2013 19:21:41 GMT
Content-Length: 12706

<!DOCTYPE html>
<html>
... truncated rest of body ...
  1. 第一行是状态行,由状态代码状态文本组成。
  2. header 是键/值对。标题以一个空的新行结束。空行表示没有更多的 header ,后面是有效负载/正文的开头。
  3. body 使用消息的其余部分。

下面说明如何提取这3个部分:

状态行

使用以下命令获取从服务器发回的状态行

>>> bad_r = requests.get('http://httpbin.org/status/404')
>>> bad_r.status_code
404

>>> bad_r.raise_for_status()
Traceback (most recent call last):
  File "requests/models.py", line 832, in raise_for_status
    raise http_error
requests.exceptions.HTTPError: 404 Client Error

(source)

标题:

r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')
# response headers: 
r.headers
# request headers:
r.request.headers

正文

使用r.text

后请求编码

您在请求中发送给服务器的“内容类型”应该与您实际发送的内容类型相匹配。在您的情况下,您发送的是 json,但告诉服务器您发送的是表单数据(如果您未指定,这是默认设置)。

从上面显示的标题:

"Content-Type":"application/x-www-form-urlencoded",

但是您的 request.post 调用设置了 data=json.dumps(data) 这是 JSON。标题应该说:

"Content-type": "application/json",

关于python - 如何调试请求库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057531/

相关文章:

Python Requests requests.exceptions.SSLError : [Errno 8] _ssl. c:504: EOF 发生违反协议(protocol)

python - 尝试抓取 Instacart : Python

python - pydoop vs hadoopy - hadoop python 客户端

python - 如何打印我认为是对象的东西?

Python:Scikit Learn MLPClassifier 放入管道时出错

curl - Centos Curl Http 错误 :404 Page does Not Exists

python - 在行到达时从压缩的、分块的 HTTP 流中有效地读取行

python - 每当使用无服务器关闭 ec2 时发送电子邮件

curl - Apache Spark 休息 API

python - 如何向 pycurl 发出 HEAD 请求