dongzhe3573 2012-03-31 09:56
浏览 41
已采纳

fsockopen等效的perl

I've just run into a problem with my ISP who it seems refuses to allow me to use fsockopen telling me its a security risk and that I have to find an alternative suggesting that I use perl.

Does anyone have an alternative to fsockopen possibly using perl for the script below?

<?php

// fetches server information from minequery.
function fetch_server_info($ip, $port){
    $socket = fsockopen($ip, $port, $errno, $errstr, 0.5);

    if($socket === false){
        return false;   
    }

    fwrite($socket, "QUERY_JSON
");

    $responce = stream_get_contents($socket);

    return json_decode($responce, true);
}

?>
  • 写回答

1条回答 默认 最新

  • dongshen2903 2012-07-01 02:59
    关注

    It sounds like you want to get the response from the server into $responce without using any of the socket functions in PHP. If shell_exec() is enabled by your hosting provider, you can run an external program to do this instead. Here's an example using perl:

    <?php
    
    // fetches server information from minequery.
    function fetch_server_info($ip, $port){
        $script = <<<'EOF'
    use warnings;
    use strict;
    use IO::Socket;
    
    die if $#ARGV+1 != 2;
    my $ip = $ARGV[0];
    my $port = $ARGV[1];
    my $sock = IO::Socket::INET->new(PeerAddr => "$ip:$port") or die;
    print $sock "QUERY_JSON
    " or die;
    local $/ = undef;
    print <$sock>;
    EOF;
    
        $responce = shell_exec("echo '$script' | perl - $ip $port");
    
        if ($responce == NULL)
            return false;
    
        return json_decode($responce, true);
    }
    
    $>
    

    Or you could use netcat if they have it:

    <?php
    
    // fetches server information from minequery.
    function fetch_server_info($ip, $port){
        $responce = shell_exec("echo QUERY_JSON| nc $ip $port");
    
        if ($responce == NULL)
            return false;
    
        return json_decode($responce, true);
    }
    
    $>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度