将ajax http post转换为python [英] convert an ajax http post to python
问题描述
我想使用Python将一些数据发送到服务器.
I want to send some data to a server using Python.
我有一个来自jQuery的有效代码片段,可以满足我的要求:
I have a working piece of code from jQuery which does what I want:
$.ajax({
type: "POST",
contentType: "text/plain",
url: "http://192.168.50.88/emoncms/nodes/1/tx/values",
data: "17,58,5,1569,0,3000,236"
});
我需要在Python中做同样的事情,但看不到如何做.这是我目前所拥有的:
I need to do the same thing in Python but I can't see how to do it. Here's what I've got at the moment:
import httplib, urllib
newdata = "17,58, 5,1569, 0, 3000, 236"
headers = {"Content-type": "text/plain"}
conn = httplib.HTTPConnection("192.168.50.88")
conn.request("POST", "/emoncms/nodes/1/tx/values", newdata)
response = conn.getresponse()
print response.status, response.reason
conn.close()
这将显示:"200 OK",因此基本帖子可以正常工作,但是我的数据无法到达服务器.
This prints: "200 OK" so the basic post is working but my data doesn't reach the server.
有人能指出我正确的方向吗?
Can anyone point me in the right direction?
推荐答案
使用请求代替.
import requests
requests.post(
'http://192.168.50.88/emoncms/nodes/1/tx/values',
data='17,58, 5,1569, 0, 3000, 236',
headers={'content-type': 'text/plain'}
)
您还可以通过此操作存储响应...
you can also store the response by doing this...
r = requests.post(...)
然后您可以从该r变量访问响应代码,任何附加的数据,标头等.
and then you can access the response code, any attached data, headers etc from that r variable.
http://docs.python-requests.org/en/latest/
这篇关于将ajax http post转换为python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!