求助: 怎么把这个 curl 命令用 python requests 搞定?

2013-05-20 01:39:32 +08:00
 y
命令如下:

curl -X POST -d '{ "method" : "test", "params" : [ { "detail" : "Hello_world"} ] }' http://example.com

在 bash 里面执行是能得到想要的结果的。但希望在 python 里面搞。

其中 data 的部分是 { "method" : "test", "params" : [ { "detail" : "Hello_world"} ] }

我不太清楚怎么用 python requests 处理 [ { "detail" : "Hello_world"} ] 这个部分。

现在的做法是 os.system 写到一个文件里,再把文件里的内容读出来,囧。
16466 次点击
所在节点    Python
5 条回复
mengzhuo
2013-05-20 01:55:19 +08:00
确实囧得很
json模块不行吗?
Jet
2013-05-20 04:22:13 +08:00
import httplib
import json

p = { "method" : "test", "params" : [ { "detail" : "Hello_world"} ] }
headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/plain"}
conn = httplib.HTTPConnection("127.0.0.1", 8081)
conn.request("POST","", json.dumps(p), headers)

不知道理解是否正确。
hit9
2013-05-20 09:12:08 +08:00
In [1]: import requests

In [2]: data = {
...: 'method': 'test',
...: 'params': [
...: {'detail':'hello world'}
...: ]
...: }

In [3]: requests.post("http://example.com",data=data)
Out[3]: <Response [200]>
xyly624
2013-05-20 13:41:44 +08:00
zhengjian
2017-07-15 01:37:46 +08:00
推荐一个网站: https://curl.trillworks.com/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/69444

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX