2

我正在使用一个 python 程序将数据上传到地下天气,然后不知为什么有一天它停止了工作。我创建了以下较小的版本来尝试解决它。

该程序返回“上传不成功”

有趣的是,如果我把路径和 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

把它放在浏览器中运行它,它工作正常吗??

谁能告诉我这里发生了什么?

4

1 回答 1

1

通过进行以下修改,我能够使您的代码正常工作:

+ 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)
于 2013-09-09T16:34:53.403 回答