我很难从python程序将数据上传到weather underground
我用一个python程序将数据上传到weather underground,但不知什么原因,有一天它停止了工作。我创建了以下较小的版本来解决问题 此程序返回“上载不成功” 有趣的是,如果我获取路径和Http地址并将它们放入浏览器中,它将成功通过。这意味着对我来说,密码和站点ID都很好,还有其他一些东西阻止了成功的传输 节目如下:我很难从python程序将数据上传到weather underground,python,http,upload,wunderground,Python,Http,Upload,Wunderground,我用一个python程序将数据上传到weather underground,但不知什么原因,有一天它停止了工作。我创建了以下较小的版本来解决问题 此程序返回“上载不成功” 有趣的是,如果我获取路径和Http地址并将它们放入浏览器中,它将成功通过。这意味着对我来说,密码和站点ID都很好,还有其他一些东西阻止了成功的传输 节目如下: import subprocess import re import sys import time from datetime import datetime
import subprocess
import re
import sys
import time
from datetime import datetime
from time import sleep
import httplib
import smbus
import math
stationid = "xxxxxx"
password = "xxxxx"
temperature= 78.2
conn = httplib.HTTPConnection("rtupdate.wunderground.com")
path ="/weatherstation/updateweatherstation.php?ID=" + stationid + "&PASSWORD=" + password + "&dateutc=" + str(datetime.utcnow()) + "&tempf=" + str(temperature) + "&softwaretype=RaspberryPi&action=updateraw&realtime=1&rtfreq=2.5"
conn.request("GET", "path")
print path
sleep(2)
res = conn.getresponse()
# checks whether there was a successful connection (HTTP code 200 and content of page contains "success")
if ((int(res.status) == 200) & ("success" in res.read())):
print "Successful Upload"
print "Temperature F=", temperature
else:
print "%s -- Upload not successful, check username, password, and formating.. Will try again in 6 seconds"
print "TempF =", temperature
如果使用命令运行此命令以打印响应和原因,则会得到以下结果:
(404, 'Not Found')
<html><head><title>404 Not Found</title><head><body><h1>Not Found</h1>The requested URL <code>path</code> was not found on this server.<br></body></html
如果我采用组件:
http://rtupdate.wunderground.com/weatherstation/updateweatherstation.php?ID=xxxxxx&PASSWORD=xxxxxx&dateutc=2013-09-07 23:20:30.920773&tempf=78.2&softwaretype=RaspberryPi&action=updateraw&realtime=1&rtfreq=2.5
然后把它放在浏览器里运行,效果很好
有人能告诉我这里发生了什么吗?我通过以下修改使您的代码正常工作:
+ conn.request("GET", "path") should be conn.request("GET", path)
#notice path should be a variable
+ you need to escape the date in query string (I used urllib.quote):
path ="/weatherstation/updateweatherstation.php?ID=" + stationid + "&PASSWORD=" + password + "&dateutc=" + urllib.quote(str(datetime.utcnow())) + "&tempf=" + str(temperature) + "&softwaretype=RaspberryPi&action=updateraw&realtime=1&rtfreq=2.5"
片段:
conn = httplib.HTTPConnection("rtupdate.wunderground.com")
path ="/weatherstation/updateweatherstation.php?ID=" + stationid + "&PASSWORD=" + password + "&dateutc=" + urllib.quote(str(datetime.utcnow())) + "&tempf=" + str(temperature) + "&softwaretype=RaspberryPi&action=updateraw&realtime=1&rtfreq=2.5"
conn.request("GET", path)
我得到无效密码id |密码和/或id不正确,这正是我需要的。我已经纠正了路径问题,我只是把我的帖子搞砸了……但是url lib和url lib.quote的使用和你的预测完全一样。我非常感谢你解决了三周来的难题。