Hot Pastrami has asked for the wisdom of the Perl Monks concerning the following question:

How would one go about allowing a Perl script to listen on a specific HTTP port? I am certain there are a number of modules for this purpose, but I am not familiar with them; This is something I've never fiddled with, so any info anyone can provide would be gratefully accepted.

Hot Pastrami

Replies are listed 'Best First'.
Re: Listening on an HTTP port
by ryddler (Monk) on Jan 04, 2001 at 23:48 UTC

    Check the docs on IO::Socket. Meantime here's a snip I played with to experiment with basic server listening...

    #!/usr/bin/perl -w use strict; use IO::Select; use IO::Socket; my ($data, $fh); my $ipc_select = IO::Select->new(); my $IPC_SOCKET = new IO::Socket::INET(Listen => 5, LocalAddr => 'localhost', LocalPort => 9000, Proto => "tcp" ); print "SOCKET = $IPC_SOCKET\n"; $ipc_select->add($IPC_SOCKET); print "Listening...\n"; while (1) { if (my @ready = $ipc_select->can_read(.01)) { foreach $fh (@ready) { if($fh == $IPC_SOCKET) { #add incoming socket to select my $new = $IPC_SOCKET->accept; $ipc_select->add($new); print "incoming connection...\n"; } else { # Process socket if (recv $fh,$data,1024,0) { print $fh $data; print "$data"; } else { $ipc_select->remove($fh); $fh->close; } } } } }

    You can run this script, and then telnet in on port 9000 (or whatever other port you set it to listen on in 'localport') and it will echo your typing...

    ryddler

Re: Listening on an HTTP port
by eg (Friar) on Jan 04, 2001 at 23:33 UTC

    Are you looking for HTTP::Daemon? Either that or you can roll your own -- see the section on sockets in the perlipc manpage.

    I remember reading once that someone had put a little http daemon on the back of his business card as sort of a japh.

Re: Listening on an HTTP port
by Trinary (Pilgrim) on Jan 04, 2001 at 23:37 UTC
    I'm kinda confused on this. http generally is on port 80, do you want to run a script that sits on the port and listens to requests, with no web server running? That's pretty easy. perldoc IO::Socket::INET, part of standard libs, set args to new() to taste.Example:

    use IO::Socket; my $listen_socket = IO::Socket::INET->new(LocalPort => 80, Listen => 10, Proto => 'tcp', Reuse => 1); while (my $connection=$listen_socket->accept) { #do whatever you want. }
    But only one program can sit on a port at a time (I think), so if you're running a web server and want to check for connects, it gets a lil more complicated. Easiest thing to do for that would be to check on your apache (or whatever) access_log for when connects come in, or something similar.

    Trinary

Re: Listening on an HTTP port
by EvanK (Chaplain) on Jan 04, 2001 at 23:34 UTC
    I would suggest using the IO::Socket module (I'd suggest v1.18 or higher) and use the listen() function:

    ______________________________________________
    It's hard to believe that everyone here is the result of the smartest sperm.

Re: Listening on an HTTP port
by princepawn (Parson) on Apr 03, 2001 at 19:52 UTC
    There is also the Perl webserver, HTTPi.