use 127.0.0.1 instead of localhost

localhost can cause some issues on badly behaved or misconfigured systems,
and 127 seems simpler.
This commit is contained in:
Min RK
2016-02-03 10:30:09 +01:00
parent 6c072bdb3d
commit f626d2f6e5
7 changed files with 14 additions and 47 deletions

View File

@@ -195,35 +195,3 @@ def url_path_join(*pieces):
return result
def localhost():
"""Return localhost or 127.0.0.1"""
if hasattr(localhost, '_localhost'):
return localhost._localhost
binder = connector = None
try:
binder = socket.socket()
binder.bind(('localhost', 0))
binder.listen(1)
port = binder.getsockname()[1]
def accept():
try:
conn, addr = binder.accept()
except ConnectionAbortedError:
pass
else:
conn.close()
t = Thread(target=accept)
t.start()
connector = socket.create_connection(('localhost', port), timeout=10)
t.join(timeout=10)
except (socket.error, socket.gaierror) as e:
warnings.warn("localhost doesn't appear to work, using 127.0.0.1\n%s" % e, RuntimeWarning)
localhost._localhost = '127.0.0.1'
else:
localhost._localhost = 'localhost'
finally:
if binder:
binder.close()
if connector:
connector.close()
return localhost._localhost