函数检查服务器($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中实现同样的功能。在
感谢您的回复:)
使用CURL
这是一个如何将fsockopen转换为CURL的示例 PHP fsockopen to curl conversion
祝你好运
相关问题 更多 >
编程相关推荐