python urllib2 can open localhost but not 127.0.0.1

Set a no_proxy or NO_PROXY environment key with 127.0.0.1, optionally with localhost too:

import os

os.environ['no_proxy'] = '127.0.0.1,localhost'

On Windows the ProxyOverride key in the HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings registry is consulted as well, you probably have localhost registered as exception. Check your proxy settings to verify this.

Leave a Comment