在php中通过特定端口(fsockopen)ping服务器

2024-09-28 01:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

函数检查服务器($domain,$port=80) { 全局$checkTimeout,$testServer

$status = 0;

$starttime = microtime(true);
$file = @fsockopen ($domain, $port, $errno, $errstr, $checkTimeout);
$stoptime = microtime(true);

if($file)
{
    fclose($file);
    $status = ($stoptime - $starttime) * 1000;
    $status = floor($status);
}
else
{
    $testfile = @fsockopen ($testServer, 80, $errno, $errstr, $checkTimeout);
    if($testfile)
    {
        fclose($testfile);
        $status = -1;
    }
    else
    {
        $status = -2;
    }
}

return $status;

}

测试服务器是谷歌.sk,checkTimeout为10秒。这实际上是可行的,但是当我尝试在一个循环中运行大约50次,并执行其他操作(mysql查询之类的事情),它并不慢,但它会导致我的CPU 100%负载,直到脚本结束。一个apache进程让我的cpu疯狂。。。如果你有什么想法的话。也许有一些技巧可以帮助你在python或bash中实现同样的功能。在

感谢您的回复:)


Tags: 服务器trueportdomainstatusfilestarttimetestfile

热门问题