barak manos barak manos - 3 months ago 49
Python Question

Is it possible to "refresh" a connection created with urllib2.urlopen?

I am fetching data from a URL using

urllib2.urlopen
:

from urllib2 import urlopen
...
conn = urlopen(url)
data = conn.read()
conn.close()


Suppose the data did not "come out" as I had expected.

What would be the best method for me to read it again?

I am currently repeating the whole process (open, read, close).

Is there a better way (some sort of connection-refresh perhaps)?

Answer

When you call urlopen on a URL, Python makes an HTTP GET request and returns the response; each of these request-response pairs are by nature separate connections. You have to repeat the process for every URL you want to request, although you don't really have to close your urlopen response.