将CURL命令行转换为Python脚本

时间:2010-10-28 04:56:01

标签: python curl

让这个cmd line curl语句在python脚本中工作太麻烦...帮助!试图使用URLLIB。

curl -X POST“http://api.postmarkapp.com/email”\
-H“接受:application / json”\\ -H“Content-Type:application / json”\\ -H“X-Postmark-Server-Token:abcdef-1234-46cc-b2ab-38e3a208ab2b”\\ -v \
-d“{From:'sender@email.com',To:'recipient@email.com',主题:'邮戳测试',HtmlBody:'你好亲爱的邮戳用户。'}”< / p>

1 个答案:

答案 0 :(得分:5)

好的,你应该使用urllib2来提交实际的请求,但这里是代码:

import urllib
import urllib2

url = "http://api.postmarkapp.com/email"
data = "{From: 'sender@email.com', To: 'recipient@email.com', Subject:   'Postmark test', HtmlBody: 'Hello dear Postmark user.'}"
headers = { "Accept" : "application/json",
        "Conthent-Type": "application/json",
        "X-Postmark-Server-Token": "abcdef-1234-46cc-b2ab-38e3a208ab2b"}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

退房:urllib2 the unwritten manual

我收到401未经授权的回复,所以我猜它有效:)