Visualizzazione dei risultati da 1 a 4 su 4

Discussione: webserver in perl?

  1. #1

    webserver in perl?

    Allora...ho eseguito questo codice:

    codice:
    #!/perl
    #
    # Server
    
    use strict;
    use warnings;
    use IO::Socket::INET;
    
    my $answer;
    
    my $server = IO::Socket::INET->new(LocalPort => '81',
                                       Type      => SOCK_STREAM,
                                       Reuse     => 1,
                                       Listen    => 1 ) || die "$!\n";
    
    while ( my $client = $server->accept() ) {
    
       print $client "The time is now: " . scalar(localtime(time())) . "\n";
       close $client;
          
    }
    
    close($server);
    ebbene l'ho eseguito,poi sono andato nel browser ed ho digitato: http://127.0.0.1:81
    Il browser mi ha restituito la data corrente!

    Vuol dire che in Perl potrei essere in grado di programmare un piccolo web server?
    E a questo web server potrei fargli eseguire gli scripts .cgi?

    ad esempio quando digito http://127.0.0.1:81/pages/page.html lo script mi restituisce: GET /pages/page.html HTTP1.1

    Credo che intanto mi studiero un pò il protocollo HTTP1.1

    Ciao!

  2. #2
    Certo che è possibile, ma chiaramente le prestazioni sarebbero ridicole.
    Marco Allegretti
    shishii@tiscalinet.it
    Lang: PERL, PHP, SQL.
    Linux user n° 268623 Fedora Core 10, Fedora Core 6, Debian Sarge on mips

  3. #3
    che differenza c'è tra:

    codice:
    while ( my $client = $server->accept() ) {
    
       $answer = <$client>;
       print $answer;
       print $client "The time is now: " . scalar(localtime(time())) . "\n";
       close $client;
          
    }
    e:

    codice:
    while ( $server->listen() ) {
    
       $client = $server->accept();
    
       $answer = <$client>;
       print $answer;
       print $client "The time is now: " . scalar(localtime(time())) . "\n";
       close $client;
          
    }
    ?

  4. #4
    potreste spiegarmi anche questo codice?

    codice:
    while ($client = $server->accept()) { 
      $client->autoflush(1); 
      print $client "Welcome to $0; type help for command list.\n"; 
      $hostinfo = gethostbyaddr($client->peeraddr); 
      printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost; 
      print $client "Command? "; 
      while ( <$client>) { 
         next unless /\S/; # blank line 
         if (/quit|exit/i) { last; } 
         elsif (/date|time/i) { printf $client "%s\n", scalar localtime; } 
         elsif (/who/i ) { print $client `who 2>&1`; } 
         elsif (/cookie/i ) { print $client `/usr/games/fortune 2>&1`; } 
         elsif (/motd/i ) { print $client `cat /etc/motd 2>&1`; } 
         else { 
          print $client "Commands: quit date who cookie motd\n"; 
         } 
    
         } continue { 
            print $client "Command? "; 
         } 
    close $client; 
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.