Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Waiting for receiving data

Reply
Thread Tools

Waiting for receiving data

 
 
Anjanesh Lekshminarayanan
Guest
Posts: n/a
 
      11-23-2009
fp = urllib.urlopen(url)
data = fp.read()

Retrieving XML data via an XML service API.
Very often network gets stuck in between. No errors / exceptions.

CTRL+C

File "get-xml.py", line 32, in <module>
fp = urllib.urlopen(url)
File "/usr/lib/python2.6/urllib.py", line 87, in urlopen
return opener.open(url)
File "/usr/lib/python2.6/urllib.py", line 206, in open
return getattr(self, name)(url)
File "/usr/lib/python2.6/urllib.py", line 348, in open_http
errcode, errmsg, headers = h.getreply()
File "/usr/lib/python2.6/httplib.py", line 1048, in getreply
response = self._conn.getresponse()
File "/usr/lib/python2.6/httplib.py", line 974, in getresponse
response.begin()
File "/usr/lib/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.6/httplib.py", line 349, in _read_status
line = self.fp.readline()
File "/usr/lib/python2.6/socket.py", line 397, in readline
data = recv(1)
KeyboardInterrupt

Is there I can do to try something else if its taking too long to
retrieve from the network ? Like kill previous attempt and retry ?

Thanks
Anjanesh Lekshmnarayanan
 
Reply With Quote
 
 
 
 
Gerald Walker
Guest
Posts: n/a
 
      11-24-2009
Anjanesh Lekshminarayanan wrote:
> fp = urllib.urlopen(url)
> data = fp.read()
>
> Retrieving XML data via an XML service API.
> Very often network gets stuck in between. No errors / exceptions.
>
> CTRL+C
>
> File "get-xml.py", line 32, in <module>
> fp = urllib.urlopen(url)
> File "/usr/lib/python2.6/urllib.py", line 87, in urlopen
> return opener.open(url)
> File "/usr/lib/python2.6/urllib.py", line 206, in open
> return getattr(self, name)(url)
> File "/usr/lib/python2.6/urllib.py", line 348, in open_http
> errcode, errmsg, headers = h.getreply()
> File "/usr/lib/python2.6/httplib.py", line 1048, in getreply
> response = self._conn.getresponse()
> File "/usr/lib/python2.6/httplib.py", line 974, in getresponse
> response.begin()
> File "/usr/lib/python2.6/httplib.py", line 391, in begin
> version, status, reason = self._read_status()
> File "/usr/lib/python2.6/httplib.py", line 349, in _read_status
> line = self.fp.readline()
> File "/usr/lib/python2.6/socket.py", line 397, in readline
> data = recv(1)
> KeyboardInterrupt
>
> Is there I can do to try something else if its taking too long to
> retrieve from the network ? Like kill previous attempt and retry ?
>
> Thanks
> Anjanesh Lekshmnarayanan




import socket
from urllib2 import urlopen

# A one-hundredths of a second (0.01) timeout before socket throws
# an exception to demonstrate catching the timeout.
# Obviously, this you will set this greater than 0.01 in real life.
socket.setdefaulttimeout(0.01)

# example xml feed
xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml"

try:
data = urlopen(xml_source)
except urllib2.URLError, e:
print 'URLError = ' + str(e.reason)



 
Reply With Quote
 
 
 
 
Gerald Walker
Guest
Posts: n/a
 
      11-24-2009

>
> import socket
> from urllib2 import urlopen
>
> # A one-hundredths of a second (0.01) timeout before socket throws
> # an exception to demonstrate catching the timeout.
> # Obviously, this you will set this greater than 0.01 in real life.
> socket.setdefaulttimeout(0.01)
>
> # example xml feed
> xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml"
>
> try:
> data = urlopen(xml_source)
> except urllib2.URLError, e:
> print 'URLError = ' + str(e.reason)


Also, if you are using multiple threads to retrieve the xml source(s)
and any thread blocks due to network problems, the thread can go way by
itself after the default timeout expires.


 
Reply With Quote
 
Gerald Walker
Guest
Posts: n/a
 
      11-24-2009

>
> Also, if you are using multiple threads to retrieve the xml source(s)
> and any thread blocks due to network problems, the thread can go way by
> itself after the default timeout expires.
>
>


Typo, edited for clarity:
That is: "..the thread can go *away* by itself after the default timeout
expires." You don't need to explicitly kill it.


 
Reply With Quote
 
Gerald Walker
Guest
Posts: n/a
 
      11-24-2009

>
> import socket
> from urllib2 import urlopen
>
> # A one-hundredths of a second (0.01) timeout before socket throws
> # an exception to demonstrate catching the timeout.
> # Obviously, this you will set this greater than 0.01 in real life.
> socket.setdefaulttimeout(0.01)
>
> # example xml feed
> xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml"
>
> try:
> data = urlopen(xml_source)
> except urllib2.URLError, e:
> print 'URLError = ' + str(e.reason)


Oops, the above doesn't run. This version works:

import socket
import urllib2

# A one-hundredths of a second (0.01) timeout before socket throws
# an exception to demonstrate catching the timeout.
# Obviously, this you will set this greater than 0.01 in real life.
socket.setdefaulttimeout(0.01)

# example xml feed
xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml"

try:
data = urllib2.urlopen(xml_source)
except urllib2.URLError, e:
print 'URLError = ' + str(e.reason)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ajax updatepanel not receiving data ( clearing data too ) fredd00 ASP .Net 0 06-01-2007 03:58 AM
ajax updatepanel not receiving data ( clearing data too ) fredd00 ASP .Net 0 05-31-2007 10:52 PM
ajax updatepanel not receiving data ( clearing data too ) fredd00 ASP .Net 0 05-31-2007 07:53 PM
sending data to another server and receiving data as response pintu ASP .Net Web Services 0 12-14-2006 09:36 AM
The incorrect content length problem with Apache, how to stop waiting for data, and send a reply in CGI? samkipers445r@yahoo.com Perl Misc 2 09-26-2005 12:03 PM



Advertisments