Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
if ( $file_name ) {
if ( -f $file_name and open FILE, '<', $file_name ) {
# Read from file
$content = join "", <FILE>;
close FILE;
}
else {
$content = "File not found";
$stat = 404;
}
if ( open FILE, '<', "/tmp/null" ) {
$content = join "", <FILE>; close FILE;
#!/usr/bin/perl
use strict;
use warnings;
$|++;
use IO::Socket::INET;
use EV;
my $sock = IO::Socket::INET->new(Listen => 10000, LocalPort => 8882, Blocking => 0, Proto => 'tcp') or die "Can't bind : $@\n";
my @evs;
push(@evs,EV::io $sock,EV::READ,\&accept);
sub accept
{
my $newsock = $sock->accept;
$newsock->blocking(0);
push(@evs,EV::io $newsock,EV::READ,\&request);
$evs[-1]->data(scalar(@evs)-1);
};
sub request
{
my $data;
$_[0]->fh->sysread($data,128);
my $content = "HTTP/1.1 200 OK\r\n"
. "Server: EV/2009-09-12\r\n"
. "Content-Type: text/html\r\n"
. "Connection: close\r\n\r\n"
. "<html><body><h1>Hello from Habr</h1></body></html>";
$_[0]->fh->syswrite($content);
$_[0]->fh->close;
undef $evs[$_[0]->data()];
undef $_[0];
};
EV::loop();
#ab -n 10000 -c 30 http://localhost:8882/
Transaction rate: 3847.76 trans/sec
Successful transactions: 25193
Failed transactions: 0
Transaction rate: 3783.66 trans/sec
Successful transactions: 119578
Failed transactions: 0
Requests per second: 3900.84 [#/sec] (mean)
Transaction rate: 4867.47 trans/sec
Successful transactions: 36506
Failed transactions: 0
Requests per second: 5647.83 [#/sec] (mean)
Transaction rate: 3111.36 trans/sec
Successful transactions: 15059
Failed transactions: 0
Requests per second: 3201.50 [#/sec] (mean)
- #!/usr/bin/perl
- use strict;
- use warnings;
- use LWP::Socket;
- my $sock = new LWP::Socket();
- die "Can't bind a socket" unless $sock->bind('127.0.0.1', '8080');
- $sock->listen(10);
- while ( my $socket = $sock->accept(3) ) {
- $socket->write( "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"
- . "<html><body><h1>Hello from Habr</h1></body></html>" );
- $socket->shutdown();
- }
- $sock->shutdown();
Почти-web-сервер своими руками