在 Python 中发送数据 Curl/Json

Spa*_*ish 5 python dashing

我正在尝试在 python 中发出这 2 个请求:

请求 1:

 curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth1", "widget":   "id1", "title": "Something1",  "text": "Some text", "moreinfo": "Subtitle" }'   serverip
Run Code Online (Sandbox Code Playgroud)

请求 2:

 vsphere_dict = {}
 vsphere_dict['server_name'] = "servername"
 vsphere_dict['api_version'] = apiVersion
 vsphere_dict['guest_count'] = guestCount
 vsphere_dict['guest_on']    = guestOnLen
 vsphere_dict['guest_off']   = guestOffLen

 #Convert output to Json to be sent
 data = json.dumps(vsphere_dict)

 curl -X POST -H "Content-Type: application/json" -d 'data' serverip
Run Code Online (Sandbox Code Playgroud)

它们似乎都不起作用。有什么办法可以用 Python 发送它们吗?

更新:

我无法处理的部分是通行证和小部件。我尝试了以下但没有成功:

import urllib2
import urllib

vsphere_dict = dict(
    server_name="servername",
    api_version="apiVersion",
    guest_count="guestCount",
    guest_on="guestOnLen",
    guest_off="guestOffLen",
)

url = "http://ip:port"

auth = "authid89"
widget = "widgetid1"

# create request object, set url and post data
req = urllib2.Request(auth,url, data=urllib.urlencode(vsphere_dict))
# set header
req.add_header('Content-Type', 'application/json')
# send request
response = urllib2.urlopen(req)**
Run Code Online (Sandbox Code Playgroud)

导致“urllib2.HTTPError:HTTP 错误 500:内部服务器错误”

有什么想法可以正确传递身份验证和小部件吗?

更新:

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

使用此代码更正 curl 请求:

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

发送这个确实有效:

 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": "token", "widget": "widgetid", "title": "Something", "text": "Some text", "moreinfo": "Subtitle" }
Run Code Online (Sandbox Code Playgroud)

并使用此代码请求

  import requests
  import simplejson as json

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

发送这个不起作用:

 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": "auth1", "moreinfo": "Subtitle", "widget": "id1",  "title": "Something1"}
Run Code Online (Sandbox Code Playgroud)

Nic*_*cci 8

Requests为您提供了在 Python 中处理 HTTP 请求的最简单但(非常)强大的方法。

也许尝试这样的事情:

import requests
import simplejson as json

url = "http://ip:port"
data = {'auth_token': 'auth1', 'widget': 'id1', 'title': 'Something1', 'text': 'Some text', 'moreinfo': 'Subtitle'}
headers = {'Content-type': 'application/json'}
r = requests.post(url, data=json.dumps(data), headers=headers)
Run Code Online (Sandbox Code Playgroud)

如果 API 请求身份验证:

r = requests.post(url, data=json.dumps(data), headers=headers, auth=('user', 'pass'))
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅 [请求身份验证]。


Gam*_*iac 2

当然,使用 Python-Requests,这是一个用于发送像 Curl 这样的请求的 Python 库。您可以查看复杂的帖子请求部分。

或者,如果您想在 Python 中使用curl,则可以使用pyCurl