5

I'm trying to convert my python script from issuing a curl command via os.system() to using requests. I thought I'd use pycurl, but this question convinced me otherwise. The problem is I'm getting an error returned from the server that I can see when using r.text (from this answer) but I need more information. Is there a better way to debug what's happening?

for what it's worth I think the issue revoles around converting my --data flag from curl/pycurl to requests. I've created a dictionary of the params i was passing to --data before. My guess is that one of those isn't valid but how can I get more info to know for sure?

example:

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

The output I'm getting is a pretty generic html message that I've seen before when I've queried the server in the wrong way. So I know I'm reaching the right server at least.

I'm not really expecting any output. This should just post to the server and update a field in the DB.

4

2 回答 2

5

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. 标头是键/值对。标题以空的新行结束。空行表示没有更多的标头,然后是有效负载/正文的开始。
  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

(资源)

标题:

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",

于 2013-09-27T19:13:53.480 回答
0

对象返回的值request包含 下的请求信息.request

例子:

r = requests.request("POST", url, ...)

print("Request headers:", r.request.headers)
print("Request body:", r.request.body)

print("Response status code:", r.status_code)
print("Response text:", r.text.encode('utf8'))
于 2020-07-07T19:10:24.163 回答