使用Python脚本将数据发布到web服务器

使用Python脚本将数据发布到web服务器,python,python-2.7,http,post,Python,Python 2.7,Http,Post,我使用的是Python2.7.3,我试图将数据发布到本地web服务器。我发布的数据是我的树莓圆周率的温度读数。我知道url是正确的,因为如果我使用postman chrome插件,数据会成功发布,我会收到一条返回消息。在postman中,我只能使用表单数据,而不能使用x-www-form-urlencoded,这就是我的python脚本的内容类型设置。我可以将其更改为表单数据吗 Python代码: import os import glob import time import threadin

我使用的是Python2.7.3,我试图将数据发布到本地web服务器。我发布的数据是我的树莓圆周率的温度读数。我知道url是正确的,因为如果我使用postman chrome插件,数据会成功发布,我会收到一条返回消息。在postman中,我只能使用表单数据,而不能使用x-www-form-urlencoded,这就是我的python脚本的内容类型设置。我可以将其更改为表单数据吗

Python代码:

import os
import glob
import time
import threading
import urllib
import urllib2

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
    temperature = {'tempf':temp_f, 'tempc':temp_c}
        return temperature

def post():
    threading.Timer(1800.0, post).start()
    temperature = read_temp()
    data = temperature
    data['room'] = 'Server Room'
    print(data)

    data=urllib.urlencode(data)
    path='http://client.pathtophppage'    #the url you want to POST to
    req=urllib2.Request(path, data)
    req.add_header("Content-type", "application/x-www-form-urlencoded")
    page=urllib2.urlopen(req).read()


post()  
错误是:

pi@raspberrypi ~/Documents $ python Temperature.py 
{'tempc': 22.0, 'tempf': 71.6, 'room': 'Server Room'}
Traceback (most recent call last):
  File "Temperature.py", line 49, in <module>
    post()  
  File "Temperature.py", line 45, in post
    page=urllib2.urlopen(req).read()
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 407, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 439, in error
    result = self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 626, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python2.7/urllib2.py", line 407, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 445, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
pi@raspberrypi~/Documents$python Temperature.py
{'tempc':22.0,'tempf':71.6,'room':'Server room'}
回溯(最近一次呼叫最后一次):
文件“Temperature.py”,第49行,在
post()
post中第45行的文件“Temperature.py”
page=urllib2.urlopen(req.read)()
文件“/usr/lib/python2.7/urllib2.py”,urlopen中的第127行
return\u opener.open(url、数据、超时)
文件“/usr/lib/python2.7/urllib2.py”,第407行,打开
响应=方法(请求,响应)
http_响应中的文件“/usr/lib/python2.7/urllib2.py”,第520行
“http”、请求、响应、代码、消息、hdrs)
文件“/usr/lib/python2.7/urllib2.py”,第439行出错
结果=自身调用链(*args)
文件“/usr/lib/python2.7/urllib2.py”,第379行,在调用链中
结果=func(*args)
http\u error\u 302中的文件“/usr/lib/python2.7/urllib2.py”,第626行
返回self.parent.open(新建,超时=请求超时)
文件“/usr/lib/python2.7/urllib2.py”,第407行,打开
响应=方法(请求,响应)
http_响应中的文件“/usr/lib/python2.7/urllib2.py”,第520行
“http”、请求、响应、代码、消息、hdrs)
文件“/usr/lib/python2.7/urllib2.py”,第445行出错
返回自我。调用链(*args)
文件“/usr/lib/python2.7/urllib2.py”,第379行,在调用链中
结果=func(*args)
文件“/usr/lib/python2.7/urllib2.py”,第528行,默认为http\u error\u
raise HTTPError(请求获取完整url(),代码,消息,hdrs,fp)
urllib2.HTTPError:HTTP错误500:内部服务器错误
节省时间,将此库用于httpRequests

简单应用程序

import requests

url = 'http://url.com'
query = {'field': value}
res = requests.post(url, data=query)
print(res.text)

日志记录表明服务器出现HTTP500错误。将服务器置于调试模式并查看为什么会出错是有意义的。我的猜测是,is期望得到的POST数据与它得到的不同,并且它没有实现正确的HTTP400请求检查。使用我选择的答案,我能够从我的PHP页面收到响应。虽然响应是关于空约束的错误,这是我下一个要修复lol的问题。网站将如何处理该请求?如何将该数据放在网站上?