使用Python访问简单的API

使用Python访问简单的API,python,api,curl,urllib2,pycurl,Python,Api,Curl,Urllib2,Pycurl,我有一个简单的api端点: http://example.de/create.json 我需要通过邮寄发送以下信息: -正文 -网址 -用户名 此任务的curl命令如下所示: curl --data-urlencode "text=Beispieltext" -d "url=http://google.de" -d "username=User1" http://example.de/create.json 我想在我的代码中实现这一点。我想向api提交数据,并使用api的答案。 实际上我不知

我有一个简单的api端点:

http://example.de/create.json
我需要通过邮寄发送以下信息:

-正文

-网址

-用户名

此任务的curl命令如下所示:

curl --data-urlencode "text=Beispieltext" -d "url=http://google.de" -d "username=User1" http://example.de/create.json
我想在我的代码中实现这一点。我想向api提交数据,并使用api的答案。 实际上我不知道从哪里开始。我读了很多关于Pycurl、urllib2和subprocess的书,也试了很多,但现在我什么都不懂了:D

你会怎么做?
我需要帮助,我不知道从哪里开始

非常简单。应该是这样的:

import pycurl
import StringIO

c = pycurl.Curl()
c.setopt(c.URL, 'http://example.de/create.json')
output = StringIO.StringIO()
c.setopt(c.WRITEFUNCTION, output.write)
c.setopt(c.POSTFIELDS, 'your post data')
c.perform()

结果在output.getvalue()中。完成后别忘了输出.close()。

libcurl(见右栏)

是我最喜欢的curl库calls@AlexL我试试看。谢谢如何定义我的帖子数据?e、 g.c.setopt(c.POSTFIELDS,'text='+mytext)不起作用!如何在其中使用变量?