HiDRA-assets / cache /ProTrek /utils /server_tool.py
ppxscal's picture
Upload folder using huggingface_hub
99e16c7 verified
raw
history blame contribute delete
983 Bytes
import socket
# Check whether a port is in use
def check_port_in_use(port, host='127.0.0.1'):
s = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
s.connect((host, int(port)))
return True
except socket.error:
return False
finally:
if s:
s.close()
# Get the IP address of the server
def get_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
# Check whether a server is active
def check_port(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
try:
result = sock.connect_ex((ip, port))
if result == 0:
return True
else:
return False
except Exception as e:
print(e)
return False
finally:
sock.close()