in reply to Simple TCP server recomendations
The learning curve for POE isn't really that big if you start with a simple problem like this. Here is a simple little POE-based script that can give you an easily expandable REST-style http interface for doing the kinds of things you want to do...
#!/usr/bin/perl use warnings; use strict; use POE qw( Component::Server::HTTP ); use HTTP::Status; my $VERSION = '0.01'; POE::Component::Server::HTTP->new( Port => 42421, ContentHandler => { "/" => \&uri_handler, }, Headers => { Version => $VERSION }, ); POE::Kernel->run(); sub uri_handler { my ( $request, $response ) = @_; my $path = $request->uri->path; $path =~ s/\W+/_/g; $path = "remote_request".$path; # see update $response->content_type( 'text/plain' ); if ( ! defined &$path ) { $response->code( RC_NOT_FOUND ); return RC_OK; } my $content = eval { no strict 'refs'; &$path }; if ( $@ ) { $response->code( RC_INTERNAL_SERVER_ERROR ); warn $@; $response->content( "ERROR: $@" ); return RC_OK; } $response->code( RC_OK ); $response->content($content); return RC_OK; } sub remote_request_version { return "$VERSION\n" }
In this little server, the uri path that is requested just gets turned into a sub name (so a request for /version becomes _version), and if that sub exists, then it's output is returned as the HTTP response. This way you can add new features just by adding new subroutines.
Update: merlyn is right, I oversimplified a little while posting this, and a little more care should be taken with the functions you expose to this type of application. I added "remote_request" to the beginning of any functions that are accessible from the web interface to mitigate this.
We're not surrounded, we're in a target-rich environment! |
---|
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Simple TCP server recomendations
by merlyn (Sage) on Oct 02, 2006 at 17:07 UTC | |
by jasonk (Parson) on Oct 03, 2006 at 12:21 UTC |