问题描述
我正在尝试用python发出这2个请求:
I`m trying to make those 2 requests in 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发送它们吗?
Neither of them seems to work. Is there any way I can send them in Python?
更新:
我无法处理的部分是通过身份验证和小部件。我尝试了以下操作,但没有成功:
The part that I cannot handle is the pass auth and widget. I have tried the following without success:
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:内部服务器错误
Resulting in "urllib2.HTTPError: HTTP Error 500: Internal Server Error"
任何想法我可以正确传递身份验证和窗口小部件?
Any ideas how I can pass the auth and widget correctly?
更新:
要查看有什么不同,我已经开始了nc本地服务器。结果如下:
To see what is different I have started a nc server locally. Here are the results:
使用以下代码纠正卷曲请求:
Correct curl request using this code:
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": "token", "widget": "widgetid", "title": "Something", "text": "Some text", "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"}
推荐答案
请求为您提供了处理Python中HTTP请求的最简单但又非常(非常)强大的方法。
Requests provides you with the simplest and yet (very) powerful way to deal with HTTP requests in Python.
也许可以尝试以下方法:
Maybe try something like this:
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)
如果API请求身份验证:
If the API requests authentication:
r = requests.post(url, data=json.dumps(data), headers=headers, auth=('user', 'pass'))
请参见[要求授权]以获取详细信息。
See [Requests auth] for details.
这篇关于用Python发送数据Curl / Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!