Find solution

Is it necessary to close connections in urllib2?

Check our answers to ‘Is it necessary to close connections in urllib2?’ - we found 39 replies and comments relevant to this matter. The best answers are submitted by users of Stack Overflow, Yahoo! Answers and Quora.

Best solution

  • How do you close connections and reuse connections with urllib2?

    This is a two part question. The first is how do you properly close a connection with urllib2? I have seen a number of examples and I have adopted the best solution I could find. However, there appears to be a problem closing the files. Currently I use contextlib's closing() as follows: try: with closing(self.opener.open(self.address, None, self.timeout)) as page: self.data = page.read() except: # bail out.. However, I still get "too many open files" error after a long time on OSX....

    Answer:

    I would recommend dropping urllib2, and trying out the fantastic Requests library. It automatically...

    Mark as irrelevant Undo

Other solutions