Handling IncompleteRead,URLError

Viewed 7293

it's a piece of web mining script.

def printer(q,missing):
    while 1:
        tmpurl=q.get()
        try:
            image=urllib2.urlopen(tmpurl).read()
        except httplib.HTTPException:
            missing.put(tmpurl)
            continue
        wf=open(tmpurl[-35:]+".jpg","wb")
        wf.write(image)
        wf.close()

q is a Queue() composed of Urls and `missing is an empty queue to gather error-raising-urls

it runs in parallel by 10 threads.

and everytime I run this, I got this.

  File "C:\Python27\lib\socket.py", line 351, in read
    data = self._sock.recv(rbufsize)
  File "C:\Python27\lib\httplib.py", line 541, in read
    return self._read_chunked(amt)
  File "C:\Python27\lib\httplib.py", line 592, in _read_chunked
    value.append(self._safe_read(amt))
  File "C:\Python27\lib\httplib.py", line 649, in _safe_read
    raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(5274 bytes read, 2918 more expected)

but I do use the except... I tried something else like

httplib.IncompleteRead
urllib2.URLError

even,

image=urllib2.urlopen(tmpurl,timeout=999999).read()

but none of this is working..

how can I catch the IncompleteRead and URLError?

1 Answers
Related