在python中模拟curl

时间:2013-12-04 15:24:10

标签: python curl

我试图在python中模拟这个curl命令:

curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth", "widget": "widgetid", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://domain.com

到目前为止,我已尝试使用python请求发送它,但每次订单更改时都会发送。

有没有办法在python中模拟这个?

更新:

要查看有什么不同,我已在本地启动了nc服务器。结果如下:

使用以下代码更正卷曲请求:

 curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth", "widget": "widgetid", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }' http://localhost:8123

发送确实有效:

 POST / HTTP/1.1
 User-Agent: curl/7.21.0 (i386-redhat-linux-gnu) libcurl/7.21.0 NSS/3.12.10.0 zlib/1.2.5  libidn/1.18 libssh2/1.2.4
 Host: localhst:8123
 Accept: */*
 Content-Type: application/json
 Content-Length: 165

 { "auth_token": "auth", "widget": "widgetid", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }

请求使用此代码

  import requests
  import simplejson as json

  url = "http://localhost:8123"
  data = {'auth_token': 'auth', 'widget': 'id1', 'title': 'Something1', 'text': 'Some   text', 'moreinfo': 'Subtitle'}
  headers = {'Content-type': 'application/json'}
  r = requests.post(url, data=json.dumps(data), headers=headers)

发送不起作用

 POST / HTTP/1.1
 Host: localhst:8123
 Content-Length: 108
 Content-type: application/json
 Accept-Encoding: gzip, deflate, compress
 Accept: */*
 User-Agent: python-requests/2.0.1 CPython/2.7.0 Linux/2.6.35.14-106.fc14.i686

 {"text": "Some text", "auth_token": "auth", "moreinfo": "Subtitle", "widget": "id1",  "title": "Something1"}

0 个答案:

没有答案