I have a difficulty uploading data to weather underground from my python program

2k Views Asked by At

I was using a python program to upload data to weather underground and then for no clear reason it stopped working one day. I created the following smaller version to try to trouble shoot it.

This program returns "upload not successful"

Interestingly, if I take the path and Http address and put them in my browser it goes through successfully This means to me that the password and station ID are fine, there is some other thing blocking the successful transmission.

Here is the program:

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

If I run this with a command to print the response and reason, I get the following:

(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

If I take the components:

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

and put that in a browser and run it, it works fine??

Can anyone tell me what is going on here?

1

There are 1 best solutions below

1
On

I was able to get your code to work by making the following modifications:

+ 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"

Snippet:

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)