Initial commit
This commit is contained in:
25
tools/builtin.py
Normal file
25
tools/builtin.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
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}"
|
||||
Reference in New Issue
Block a user