作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 python 程序将数据上传到 weather underground,然后不知何故有一天它停止工作了。我创建了以下较小的版本来尝试解决它。
这个程序返回“上传不成功”
有趣的是,如果我获取路径和 Http 地址并将它们放入我的浏览器中,它会成功通过。这对我来说意味着密码和站点 ID 没问题,还有其他一些东西阻止了成功传输。
程序如下:
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)
关于python - 我很难从我的 python 程序上传数据到地下天气,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679108/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!