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

Hi

I am trying to set up a web service in Perl. This should provide a function for clients to upload their files. I am new to web services. To start with, I have created a simple web service which will add two numbers. Here is the server side code:

use Frontier::RPC2; my $rpc = new Frontier::RPC2; my $methods = {'sum' => \&sum}; sub sum { my ($arg1, $arg2) = @_; return $arg1 + $arg2; } print "Content-type: text/xml\n\n"; my $buffer = ""; # Reads the request in from STDIN up to CONTENT_LENGTH if (defined $ENV{"REQUEST_METHOD"} && $ENV{"REQUEST_METHOD"} eq 'POST' +) { if (exists($ENV{"CONTENT_TYPE"}) && $ENV{"CONTENT_TYPE"} eq 't +ext/xml' ) { read(STDIN, $buffer, $ENV{"CONTENT_LENGTH"}); } } my $responce = $rpc->serve($buffer, $methods); print $responce . "\n";

And here is the client side code:

use Frontier::Client; my $url = "http://192.168.1.207/cgi-bin/ws.pl"; my @args = (5, 2); my $client = new Frontier::Client( url => $url, debug => 0); print $client->call('sum', @args);

Please let me know how I should go about for providing an upload function. If there is also any URL from which I can get to know this information kindly let me know.

Thanks
Srikanth

Replies are listed 'Best First'.
Re: Web services - Upload
by osunderdog (Deacon) on Oct 19, 2006 at 07:42 UTC

    I haven't ever played with Frontier. But thought I would give it a whirl. It appears to work as advertised...

    Server

    #!/usr/bin/perl # sum() server use strict; use warnings; use Frontier::Daemon; my $d = Frontier::Daemon->new( methods => { sum => \&sum, }, LocalAddr => '10.0.1.2', LocalPort => 1080, ); sub sum { my ($arg1, $arg2) = @_; return $arg1 + $arg2; }

    Client

    #!/usr/bin/perl # Testing sum() use strict; use warnings; use Frontier::Client; my $url = "http://10.0.1.2:1080/RPC2"; my @args = (2,5); my $client = Frontier::Client->new( url => $url, debug => 0, ); print "$args[0] + $args[1] = ", $client->call('sum', @args), "\n";

    Output

    $perl Client.pl 2 + 5 = 7

    Architecture?

    Perhaps if you could explain a little about the architecture that you're shooting for, it would help us discern what would be the best approach for you to accomplish your goal. Are you, perhaps, interested in something that would work from a web server as a cgi?

    Hazah! I'm quitting!

    $g = "FSF SUCKS"; $g =~ s/F/C/g; print $g;

      Hi osunderdog

      Here is the purpose: The client will upload files to the server. But he will not do that through a web brouwer. Instead he will use the methods provided by my server-web-services in his program. I think CGI is not a right choice for this. Currently the client program will be in perl, but in future it can be anything. Any idea how to go about from here?

      Thanks
      Srikanth

        More information is certainly better than less. ;)

        What will happen to the file once it is received by the server... Some further processing? Stored off for another application to pickup?

        I guess I'm trying to avoid telling you to build a server to receive the file, because those already exist. You could use ssh or ftp or something to receive the file using an already existing protocol. Just off the top of my head (and not to be mistaken for actual experience with this approach) I would have the client connect to the server... upon receiving a command from the client, the server would fire up ftp Net::FTPServer configured to accept connections on an agreed upon port. The client would then invoke ftp to connect to the agreed upon port. The client would transmit the file to the server. The client closes the ftp connection... the server takes back control after the ftp server indicates it has received the file or shutsdown.

        In fact, after looking at Net::FTPServer, it may be something that you could extend to do exactly what you need...

        Just some pseudo-random thoughts.

        Hazah! I'm Employed!

Re: Web services - Upload
by neilwatson (Priest) on Oct 19, 2006 at 15:44 UTC
    Here is a basic cgi program that allows file uploads. You'll want to add lots of error checking. On the client side you could write a user agent that fills in the webform for the user.
    #!/usr/bin/perl #limits size of upload $CGI::POST_MAX=1024 * 100; use File::Copy; use CGI qw/:standard/; use strict; use warnings; # output content header print "Content-type: text/html\n\n"; # get filename args my $cgi = new CGI or die "new CGI: $!\n"; my $csvfile = $cgi->param('inputfile'); # must match form name my $caption = $cgi->param('caption'); # table caption # start HTML print <<EOD; <html> <body> <h1>Your HTML Table</h1> EOD $\ = "\n"; # set output record separator print '<table border cellspacing=0 cellpadding=5>'; print '<caption>' . $caption . '</caption>'; while (<$csvfile>) { chomp; # strip record separator my @Fld = split(/,/, $_); my $X = 0; print '<tr>'; while ($X <= $#Fld) { if ($X == 0) { print "\t <th>" . $Fld[$X] . '</th>'; } else { print "\t <td>" . $Fld[$X] . '</td>'; } ++$X; } print '</tr>'; } print <<EOD; </table> <p>Use the view source function on your browser to download a copy of +the HTML code. </body> </html> EOD

    Neil Watson
    watson-wilson.ca