如何使用grequests发出HTTP POST请求

时间:2014-01-12 18:56:39

标签: python grequests

我尝试了以下内容:

import grequests

urls = ['http://localhost/test', 'http://localhost/test']

params = {'a':'b', 'c':'d'}
rs = (grequests.post(u, params) for u in urls)
grequests.map(rs)

但是它说:

File "search.py", line 6, in <genexpr>
rs = (grequests.post(u, params) for u in urls)
TypeError: __init__() takes exactly 3 arguments (4 given)

我还需要将响应传递给回调进行处理。

1 个答案:

答案 0 :(得分:24)

grequests.post()函数只接受一个位置参数,即您发送该POST请求的URL。其余参数作为关键字参数。

所以你需要将grequests.post()调用为:

import grequests

urls = ['http://localhost/test', 'http://localhost/test']

params = {'a':'b', 'c':'d'}
rs = (grequests.post(u, data=params) for u in urls)
grequests.map(rs)