simple server

D

D Borland

Hi,

I am looking to create database (probably) using SQLlite and perl and it's
interface. What i would like to know is there any way with perl for me to
create a simple server, so i could say open a local file form the harddrive
in my webbrowser, then click on a link to execute a perl script which will
in turn return data to the current browser process.

Hope i explained that ok.

With Thanks

D Borland
 
J

James Taylor

Hi Alan,

CPAN is always the first port of call for such a requirement,
rather than usenet, no?

Yes agreed, which is why my answer included various links into
CPAN. This was my way of steering the OP in the right direction
without resorting to the kind of curtness and jaded sarcasm that
has sadly become all too common in this group. (Not pointing
the finger at you, of course). :)
Supporting CGI - supporting it properly, in accordance with the
rules of HTTP and of the CGI spec - is not exactly "simple",

Sure, I accept that. On the other hand, by the tone of the
OP's question, it seems likely that even a simple calling
mechanism would suffice for his purposes. I guess we'll
never know unless he comes back with further clarification.
My gut feeling still says this: Apache is a competent server[*],
it's available for a wide range of platforms,

But not mine, which is exactly why it might be useful to have an
off the shelf HTTP server written in Perl to play with.

I accept, of course, that Apache (or equivalent) would be
the best choice on a platform for which it was available.
 
S

Si Ballenger

my perl question was how do i do that i perl, not with Apache, et cetera, or
another server. I was wondering if perl has an ability to do that.

That was what i asked wasn't it?

D Borland

That was the question. Seems Tad sat on his broom stick a little
too soon. ;-)
 
B

Bob Walton

D Borland wrote:

....
I am looking to create database (probably) using SQLlite and perl and it's
interface. What i would like to know is there any way with perl for me to
create a simple server, so i could say open a local file form the harddrive
in my webbrowser, then click on a link to execute a perl script which will
in turn return data to the current browser process. ....
D Borland
....


It isn't very good, but it is an all-Perl web server. Put it in the
same directory as your HTML files (*.html) and Perl CGI files (*.cgi):

use HTTP::Daemon;
use IPC::Open2;
$|=1;
my $server=HTTP::Daemon->new(LocalPort=>80,LocalAddr=>'localhost');
print "Please contact me at <URL:", $server->url, ">\n";
while($client=$server->accept){
while(my $answer=$client->get_request){
$ans=$answer->as_string;
@ans=split /\n/,$ans;
$client->autoflush;
if($answer->method eq 'GET'){
$path=$answer->url->path;
(error,last) unless $path=~s#^/##;
if($path=~/html$/i){
$client->send_file_response($path);
last;
}
if($path=~/cgi$/i){
$ENV{REQUEST_METHOD}=$answer->method;
$query=$answer->url->query;
$ENV{CONTENT_LENGTH}=length($query);
$ENV{QUERY_STRING}=$query;
$out=`perl $path`;
$out=~s/.*?\n\n//s; #remove HTTP header
print $client $out;
last;
}
}
if($answer->method eq 'POST'){
$path=$answer->url->path;
(error,last) unless $path=~s#^/##;
if($path=~/cgi$/i){
$query=$answer->url->query;
$ENV{REQUEST_METHOD}=$answer->method;
for(@ans){
$ENV{CONTENT_LENGTH}=$1 if /Content-Length: (\d+)/;
$ENV{HTTP_REFERRER}=$1 if /Referer: (.*)/;
}
$query=$ans[-1];
undef $CGI;undef $OUT;
$pid=open2($CGI,$OUT,"perl","$path") or error;
print $OUT $query;
close $OUT;
@out=<$CGI>;
waitpid $pid,0;
$out=join "\n",@out;
$out=~s/.*?\n\n//s; #remove HTTP header
print $client $out;
last;
}
}
last;
}
print "CLOSE: ", $client->reason, "\n" if $client->reason;
undef $client;
}

sub error{
$client->error(RC_FORBIDDEN);
print "An error occurred in $ans\n";
}
 
T

Tore Aursand

my perl question was how do i do that i perl, not with Apache, et cetera, or
another server. I was wondering if perl has an ability to do that.

First of all, don't top-post. Secondly, you asked if there is "any way
with perl for me to create a simple server". Seems to me like you want to
write a web server in Perl. Don't do that. Use Apache (for example),
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top