gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:38:13 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com