Difference between revisions of "Perl Batch Client"

From Textserver wiki
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="perl" line="1" > </syntaxhighlight>")
 
Line 1: Line 1:
 
<syntaxhighlight lang="perl" line="1" >
 
<syntaxhighlight lang="perl" line="1" >
 +
#! /usr/bin/perl
 +
 +
########################################################################
 +
#
 +
#  Example client to submit a batch request to TextServer
 +
# SERVICENAME service, and wait for the job to be finished.
 +
#
 +
#  Input must be a ZIP file containing one or more text files to analyze
 +
#  Output will be a ZIP file containing results for each input file in
 +
# the requested format (XML, json, conll)
 +
#
 +
########################################################################
 +
 +
use strict;
 +
 +
# import needeed libraries
 +
use HTTP::Request::Common;
 +
use LWP::UserAgent;
 +
use XML::Twig;
 +
use XML::LibXML;
 +
 +
# get query parameters
 +
print "Input ZIP file: ";
 +
my $fname=<>; chomp $fname;
 +
print "Output ZIP file: ";
 +
my $outfname=<>; chomp $outfname;
 +
print "Language: ";
 +
my $lang=<>; chomp $lang;
 +
print "Output format (xml,json,conll,naf): ";
 +
my $out=<>; chomp $out;
 +
print "TextServer username: ";
 +
my $user=<>; chomp $user;
 +
print "TextServer password: ";
 +
my $pwd=<>; chomp $pwd;
 +
 +
# URL for the requested service
 +
my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
 +
my $service = "SERVICENAME";
 +
 +
# create user agent
 +
my $ua = LWP::UserAgent->new;
 +
 +
# send request to server
 +
my $resp = $ua->post($TextServer_URL."/processQuery/".$service,
 +
                    Content_Type => 'form-data',
 +
                    Content  => [
 +
                                  username => $user, 
 +
                                  password => $pwd, 
 +
                                  file => [$fname],
 +
                                  language => $lang, 
 +
                                  output => $out,
 +
                                  interactive => "0"
 +
                                ]
 +
                    );
 +
 +
# check for success in connection
 +
die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success;
 +
 +
# Server response should include a job tokenID, retrieve it                                               
 +
my $twig= new XML::Twig();
 +
$twig->parse($resp->decoded_content);
 +
my $tkid = $twig->root->text("job_token_id");
 +
print "Job sumbitted. Token id: [$tkid]\n";
 +
 +
# Periodically poll server until batch job is ended.                                                     
 +
my $nseconds=20;
 +
while (1) {
 +
 +
    print "Sleeping for $nseconds seconds\n";
 +
    sleep($nseconds);
 +
 +
    # send request to check for job termination
 +
    print "Polling server for job completion...\n";
 +
    $resp = $ua->post($TextServer_URL."/resultRetrieve",
 +
                      Content_Type => 'form-data',
 +
                      Content  => [username => $user, tokenID => $tkid] );
 +
   
 +
    # if status=200, we a got ZIP response, the job is done, exit loop.
 +
    if ($resp->is_success) { last; }
 +
 +
    # status!=200, check the error code
 +
    if ($resp->code==503 && $resp->decoded_content =~ /^\[TS\-125\]/ ) {
 +
        # http status 503, textserver code TS-125 means the job is not finished yet. keep waiting
 +
        print "Job not finished yet\n";
 +
    }
 +
    else {
 +
        # some actual error happened. abort
 +
        die $resp->status_line." - ".$resp->decoded_content."\n"
 +
    }
 +
}
 +
 +
# print zipped respose to output file
 +
print "Job finished. Saving results to $outfname\n";
 +
open(my $outf, '>', $outfname);
 +
print $outf $resp->decoded_content;
 +
close $outf;
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 13:13, 20 January 2016

 1 #! /usr/bin/perl
 2 
 3 ########################################################################
 4 #
 5 #  Example client to submit a batch request to TextServer 
 6 # SERVICENAME service, and wait for the job to be finished.
 7 #
 8 #  Input must be a ZIP file containing one or more text files to analyze
 9 #  Output will be a ZIP file containing results for each input file in
10 # the requested format (XML, json, conll)
11 #
12 ########################################################################
13 
14 use strict;
15 
16 # import needeed libraries
17 use HTTP::Request::Common;
18 use LWP::UserAgent;
19 use XML::Twig;
20 use XML::LibXML;
21 
22 # get query parameters
23 print "Input ZIP file: ";
24 my $fname=<>; chomp $fname;
25 print "Output ZIP file: ";
26 my $outfname=<>; chomp $outfname;
27 print "Language: "; 
28 my $lang=<>; chomp $lang;
29 print "Output format (xml,json,conll,naf): ";
30 my $out=<>; chomp $out;
31 print "TextServer username: "; 
32 my $user=<>; chomp $user;
33 print "TextServer password: "; 
34 my $pwd=<>; chomp $pwd;
35 
36 # URL for the requested service
37 my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
38 my $service = "SERVICENAME";
39 
40 # create user agent
41 my $ua = LWP::UserAgent->new;
42 
43 # send request to server
44 my $resp = $ua->post($TextServer_URL."/processQuery/".$service,
45                      Content_Type => 'form-data',
46                      Content  => [
47                                   username => $user,   
48                                   password => $pwd,   
49                                   file => [$fname],
50                                   language => $lang,  
51                                   output => $out,
52                                   interactive => "0"
53                                  ]
54                     );
55 
56 # check for success in connection
57 die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success;
58 
59 # Server response should include a job tokenID, retrieve it                                                
60 my $twig= new XML::Twig();
61 $twig->parse($resp->decoded_content);
62 my $tkid = $twig->root->text("job_token_id");
63 print "Job sumbitted. Token id: [$tkid]\n";
64 
65 # Periodically poll server until batch job is ended.                                                       
66 my $nseconds=20;
67 while (1) {
68 
69     print "Sleeping for $nseconds seconds\n";
70     sleep($nseconds);
71 
72     # send request to check for job termination
73     print "Polling server for job completion...\n";
74     $resp = $ua->post($TextServer_URL."/resultRetrieve",
75                       Content_Type => 'form-data',
76                       Content  => [username => $user, tokenID => $tkid] );
77     
78     # if status=200, we a got ZIP response, the job is done, exit loop.
79     if ($resp->is_success) { last; }
80 
81     # status!=200, check the error code
82     if ($resp->code==503 && $resp->decoded_content =~ /^\[TS\-125\]/ ) {
83         # http status 503, textserver code TS-125 means the job is not finished yet. keep waiting
84         print "Job not finished yet\n"; 
85     }
86     else {
87         # some actual error happened. abort
88         die $resp->status_line." - ".$resp->decoded_content."\n"
89     }
90 }
91 
92 # print zipped respose to output file
93 print "Job finished. Saving results to $outfname\n"; 
94 open(my $outf, '>', $outfname);
95 print $outf $resp->decoded_content;
96 close $outf;