python - 我很难从我的 python 程序上传数据到地下天气

标签 python http upload wunderground

我正在使用 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/

相关文章:

python : Convert from C-Char to Int

java - 使用 java servlet 的 http 流式传输

Html - 无法加载外部 http.jpg 作为图像源

java - UrlConnection.getHeaderField(字符串名称) 返回 null

javascript - 为什么 "HTML &lt;input type=' file'/>"has "files"属性在 JavaScript 中?

python - 这段代码执行后栈会不会为空?

python - 设置类组成对象的属性的功能

wordpress - 来自 Network.HTTP.simpleHTTP 的 ErrorClosed 异常——尝试使用 haxr 通过 XML-RPC 上传图像

python - Bottle on Python,如何做出自定义响应

javascript - 从头开始jQuery多文件上传