[Update] WebSocket: Implement availability check for WebSocket server and refactor related logic

This commit is contained in:
misyaguziya
2025-05-26 16:15:04 +09:00
parent ed27a8c7ba
commit 88c27a9c70
3 changed files with 31 additions and 32 deletions

View File

@@ -8,6 +8,7 @@ from logging.handlers import RotatingFileHandler
from ctranslate2 import get_supported_compute_types
import requests
import ipaddress
import socket
def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
try:
@@ -16,6 +17,25 @@ def isConnectedNetwork(url="http://www.google.com", timeout=3) -> bool:
except requests.RequestException:
return False
def isAvailableWebSocketServer(host:str, port:int) -> bool:
"""WebSocketサーバーのポートが使用中かどうかを確認する"""
response = True
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as chk:
try:
# SO_REUSEADDRを設定してソケットの再利用を許可
chk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
chk.bind((host, port))
# シャットダウン前にリッスン状態にする必要はない
chk.close()
except Exception:
response = False
except Exception:
errorLogging()
response = False
return response
def isValidIpAddress(ip_address: str) -> bool:
try:
ipaddress.ip_address(ip_address)