cpan

����˥��󥹥ѥ��䤵��ơ�

Python �� Twisted �Ǥ��ߤˤ�񤱤� Web ������
ï�Ǥ����Ū��ñ�� Web �����Ф�񤯤��Ȥ��ǽ�ˤ���Τ� Python �� Twisted �Ȥ����ե졼�������ȹ礻�Ǥ���

Pure Perl�ǽ񤫤줿Web Server�Ϥ��줳�����ۤɤ���ΤǤ������¤�LWP�ˤ⥵���С����ñ�˼�������Module���Ѱդ���Ƥ��ơ����줬HTTP::Daemon�Ǥ���

�㤨�С��֥饦���Υꥯ�����Ȥ򤪤����֤��ˤ��륵���С��ϡ��ʲ��ΤȤ���Ȥʤ�ޤ���

#!/usr/local/bin/perl -T
use strict;
use warnings;
use HTTP::Daemon;
use HTTP::Date;

my $d = HTTP::Daemon->new(
    LocalAddr => '0.0.0.0',
    LocalPort => shift || 8080
) or die $!;

while ( my ( $c, $peer_addr ) = $d->accept ) {
    while ( my $req = $c->get_request ) {
        my $header = HTTP::Headers->new( 'Content-Type' => 'text/plain' );
        my $res = HTTP::Response->new( 200, 'OK', $header, $req->as_string );
        $c->send_response($res);
        print_log($peer_addr, $req, $res);
    }
    $c->close;
    undef($c);
}

# you don't need this unless you need logging
sub print_log {
    use Socket qw/sockaddr_in inet_ntoa/;    # to deparse $peer_addr
    use bytes ();                            # for length
    my ( $peer_addr, $req, $res ) = @_;
    my ( $port, $iaddr ) = sockaddr_in($peer_addr);
    my $remote_addr = inet_ntoa($iaddr);
    my $remote_user = $req->headers->authorization_basic || '-';
    $remote_user =~ s/:.*//o;
    printf qq(%s %s - [%s] "%s %s %s" %d %d\n), 
        $remote_addr, $remote_user, time2str( time() ),
         $req->method, $req->url, $req->protocol,
         $res->code, bytes::length( $res->content );
}
__END__

���stdout��Common Log���Ǥ��褦�˽񤭤ޤ�������log�������פʤ�print_log()�����פʤΤǡ�20��̤����Web Server�ν���夬��Ǥ���

�������HTTP::Daemon�Ȥ���̾����ȿ���ơ��ץ�������daemon��������ޤǤϤ��Ƥ���ʤ��Τǡ��ܳ�Ū��Web Server��񤯤ʤ�Cookbook����17�Ϥ򻲹ͤˤ����ꡢPOE��Ȥ��ʤꤹ��٤��Ǥ������ճ��ʤȤ����ˤ���������Ƥ�����Ȥ���HTTP::Daemon����夲������Ǥ���

Enjoy!

Dan the Daemon