barak manos barak manos - 1 year ago 103
Python Question

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

I am fetching data from a URL using


from urllib2 import urlopen
conn = urlopen(url)
data =

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 Source

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.