26 lines
754 B
Python
26 lines
754 B
Python
"""
|
|
This module implements some builtin tools.
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
from typing import Literal
|
|
|
|
async def builtin_network_ping(host: str) -> str:
|
|
"""Ping `host` to check if it is reachable."""
|
|
try:
|
|
param = '-n' if sys.platform.lower() == 'windows' else '-c'
|
|
cmd = ['ping', param, '1', host]
|
|
process = await asyncio.create_subprocess_exec(
|
|
*cmd,
|
|
stdout=asyncio.subprocess.PIPE,
|
|
stderr=asyncio.subprocess.PIPE
|
|
)
|
|
stdout, stderr = await process.communicate()
|
|
if process.returncode == 0:
|
|
return f"{host} is reachable"
|
|
else:
|
|
return f"{host} is unreachable"
|
|
except Exception as e:
|
|
return f"Exception happened: {e}"
|