提交 6c63153f 编写于 作者: L Lukáš Doktor

avocado.utils.network: Optimize is_port_free a bit

We do quite a few loops, let's use boolean instead of string comparison.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 47cad02d
......@@ -38,18 +38,20 @@ def is_port_free(port, address):
:param port: Port number
:param address: Socket address to bind or connect
"""
sock = None
if address == "localhost":
if address == "localhost" or not address:
localhost = True
protocols = PROTOCOLS
else:
localhost = False
# sock.connect always connects for UDP
protocols = (socket.SOCK_STREAM, )
sock = None
try:
for family in FAMILIES:
for protocol in protocols:
try:
sock = socket.socket(family, protocol)
if address == "localhost":
if localhost:
sock.bind(("", port))
else:
sock.connect((address, port))
......@@ -57,7 +59,7 @@ def is_port_free(port, address):
except socket.error as exc:
if exc.errno in (93, 94): # Unsupported combinations
continue
if address == "localhost":
if localhost:
return False
sock.close()
return True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册