Search code examples
pythonurllib

should I call close() after urllib.urlopen()?


I'm new to Python and reading someone else's code:

should urllib.urlopen() be followed by urllib.close()? Otherwise, one would leak connections, correct?


Solution

  • The close method must be called on the result of urllib.urlopen, not on the urllib module itself as you're thinking about (as you mention urllib.close -- which doesn't exist).

    The best approach: instead of x = urllib.urlopen(u) etc, use:

    import contextlib
    
    with contextlib.closing(urllib.urlopen(u)) as x:
       ...use x at will here...
    

    The with statement, and the closing context manager, will ensure proper closure even in presence of exceptions.