1

我需要使用 grequests 发出异步 POST 请求。

我的帖子正文(在 json 中)是这样的:

[{'params': {'source': 'widget',
   'id': 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif',
   'groupId': '@self',
   'nolog': 'true',
   'userId': '@viewer'},
  'method': 'pos.plusones.get',
  'id': 'p',
  'jsonrpc': '2.0',
  'apiVersion': 'v1',
  'key': 'p'}]

我需要将[0]['params']['id']键的值更改为我将制作的许多 POST 的不同 URL。

所以我在做:

myrequests = (grequests.post(POST_URL, data=fgp(a_url) for a_url in all_urls)

我的生成器理解中的fgp()方法是一种[0]['params']['id']a_url传递给它的方法,在我发送的 POST 正文中。

当我将请求映射到响应时:

myresponses = grequests.map(myrequests)

这就是我得到的,与请求一样多(显然)。

Traceback (most recent call last):
  File "/home/ashk/.virtualenvs/cosignp3/src/gevent/gevent/greenlet.py", line 340, in run
    result = self._run(*self.args, **self.kwargs)
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/grequests.py", line 71, in send
    self.url, **merged_kwargs)
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/sessions.py", line 451, in request
    prep = self.prepare_request(req)
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/sessions.py", line 382, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/models.py", line 307, in prepare
    self.prepare_body(data, files, json)
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/models.py", line 456, in prepare_body
    body = self._encode_params(data)
  File "/home/ashk/.virtualenvs/cosignp3/lib/python3.4/site-packages/requests/models.py", line 89, in _encode_params
    for k, vs in to_key_val_list(data):
ValueError: too many values to unpack (expected 2)
<Greenlet at 0x7f0f7cbf33d8: <bound method AsyncRequest.send of <grequests.AsyncRequest object at 0x7f0f7c90d080>>(stream=False)> failed with ValueError

编辑:问题已解决:-

requests我必须玩弄,并像在模块中一样放入标题。

我将 headers kwarg 参数设置为无编码和内容类型:

(grequests.post(POST_URL, data=fgp(a_url, j=True), headers={'Accept-Encoding':'none', 'Content-Type':'application/json'}) for a_url in urls)

现在我得到了正确的输出:

In [64]: resps[0].json()
Out[64]: 
{'result': {'isSetByViewer': False,
  'kind': 'pos#plusones',
  'metadata': {'type': 'URL', 'globalCounts': {'count': 0.0 }},
  'id': 'http://example.com/dfg',
  'abtk': 'xxxxxxxxxxxxxxx'},
 'id': 'p'}

注意:输出编辑了一点以隐藏一些数据。

4

2 回答 2

5

您在grequests.post所显示的那一行中缺少一个接近的括号。但是,假设它实际上在行尾:

myrequests = (grequests.post(POST_URL, data=fgp(a_url) for a_url in all_urls))

这意味着您只生成一个post调用,但具有多个data. 我认为你的意思是:

myrequests = (grequests.post(POST_URL, data=fgp(a_url)) for a_url in all_urls)
于 2015-04-23T11:38:51.710 回答
3

您正在尝试发送JSON数据;通过requests使用json参数而不是data

myrequests = (grequests.post(POST_URL, json=fgp(a_url)) for a_url in all_urls)
于 2015-04-23T12:02:28.210 回答