NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Python error using urllib2 - need some help

July 11, 2011
by gplouffe
gplouffe's Avatar

I have built a 5 segment ledarray powered by the usb port and am experiencing a Python script problem that I need help with. My sign follows a user on twitter and the functionality is split between two pc's. One get the tweets and sends to a website and the other (small fit pc 1 running linux embedded in the led array box via MIFI on Verizon network) gets the text file from the website and pulls down to feed the sign. Problem is that Python throws an error intermittently and I don’t know how to resolve it. Sometimes it will run for 30 min, sometimes an hour or more. I think there may be a timeout with the urllib2 process. I recently added the “ket.setdefaulttimeout(5)” line but it did not help. Any help would be appreciated. I am opened to any other ways of pulling down this text file as well. I have cobbled this code together from other samples and may have done a bad job of it. I am opened to any changes. Thanks. Guy

Here is the error..

Traceback (most recent call last):
  File "Y:\guy.py", line 15, in <module>
    f = urllib2.urlopen(myurl) #define url to variable name
  File "C:\Python26\lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 395, in open
    response = meth(req, response)
  File "C:\Python26\lib\urllib2.py", line 508, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python26\lib\urllib2.py", line 433, in error
    return self._call_chain(*args)
  File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 516, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found

Here is my code

 /usr/lib/ python

 import serial
 import time

 import urllib2,socket
 import os

 serial = serial.Serial("com2", 115200) #testing on my windows box vs linux  for now.
 myurl = 'http://frostback.us/ledarray/lcd.txt'
 message = "startup led array message"
 socket.setdefaulttimeout(5)
 loopvar = 1
 while loopvar == 1: #never present a false statement so run forver
        f = urllib2.urlopen(myurl) #define url to variable name
        #while True:
        print message
        message = f.read()              #get url to variable
        mystring = str(message) + "   \n" #place contents of message to  mystring and add trailing spaces
        serial.write('z')
        serial.write('b')

                #Send message to LED array 
        for i in range(1, 2): #not sure what this does....
                for data in mystring:
                        if not data == '\n':
                                serial.write(data)
                                #Debug line follows
                                #print "Sent " + data + " at " + time.strftime("%H:%M")
                                response = serial.read()
                                #Debug line follows
                                #print "Received  " + response
                        if not response == 'n':
                                #print "Received: " + response
                                #This is a debug line, since I'm a bit fuzzy on some details
                                #     break

                                serial.write('a')
                #time.sleep(delay)
                pass
July 11, 2011
by gplouffe
gplouffe's Avatar

I have done a bit more research, looks like I may be opening too many ports that are not being closed. Will try httplib instead.

July 11, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi gplouffe,

That is likely the issue. Have you tried calling f.close() after you do the f.read(), it might be enough to fix this issue.

Humberto

Post a Reply

Please log in to post a reply.

Did you know that you can build a circuit to convert the "dit" and "dah" of Morse code back into letters automatically? Learn more...