加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在Python中发送数据Curl / Json

发布时间:2020-12-20 11:31:41 所属栏目:Python 来源:网络整理
导读:我试图在 python中发出这两个请求: 要求1: curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth1","widget": "id1","title": "Something1","text": "Some text","moreinfo": "Subtitle" }' serverip 要求2: vsphere_dict = {} v
我试图在 python中发出这两个请求:

要求1:

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

要求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

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

更新:

我无法处理的部分是pass auth和widget.我试过以下没有成功:

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)**

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

任何想法我如何正确传递auth和小部件?

更新:

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

使用此代码更正卷曲请求:

curl -X POST -H "Content-Type: application/json" -d '{ "auth_token": "auth","widget": "widgetid","title": "Something","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": "token","moreinfo": "Subtitle" }

并请求使用此代码

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)

发送不起作用的:

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"}

解决方法

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

也许尝试这样的事情:

import requests
import simplejson as json

url = "http://ip:port"
data = {'auth_token': 'auth1','text': 'Some text','moreinfo': 'Subtitle'}
headers = {'Content-type': 'application/json'}
r = requests.post(url,headers=headers)

如果API请求身份验证:

r = requests.post(url,headers=headers,auth=('user','pass'))

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读