通过 php 中的特定端口 (fsockopen) ping 服务器
函数 checkServer($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;
测试
服务器是google.sk,checkTimeout 是10 秒。这实际上是有效的,但是当我尝试在循环中运行它大约 50 次,并执行其他操作(mysql 查询和类似的操作)时,它并不慢,但它会导致我的 CPU 负载 100%,直到脚本结束。这是一个让我的 cpu 疯狂的 apache 进程...所以我想问你是否对此有任何想法。也许一些提示如何在 python 或 bash 等中执行相同的操作将受到赞赏。
谢谢您的回复:)
function checkServer($domain, $port=80)
{
global $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;
}
the testserver is google.sk, and checkTimeout is 10 seconds. This actually works, but when i try to run it in a loop for about 50 times, and do other stuff (mysql queries and things like that), it's not slow, but it causes 100% load of my CPU until the script ends. It's a single apache proccess that drives my cpu crazy ... So i wanted to ask you if you have any ideas about it. maybe some tip how to do the same in python or bash or so will be appreciated.
Thank you for the responses :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 CURL
这是如何将 fsockopen 转换为 CURL 的示例
PHP fsockopen 到curl 转换
祝你好运
Use CURL
this is an example how to conversion fsockopen to CURL
PHP fsockopen to curl conversion
Good luck