Difference between revisions of "Batch Results Retrieval in Python"
From Textserver wiki
(Created page with "<syntaxhighlight lang="python" line="1" > #! /usr/bin/python ######################################################################## # # Example client to submit a batch re...") |
(No difference)
|
Revision as of 12:26, 21 January 2016
1 #! /usr/bin/python
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 # import required libraries
15 import urllib2
16 # You may need to install "poster" python module for these two:
17 from poster.encode import multipart_encode
18 from poster.streaminghttp import register_openers
19 import xml.etree.ElementTree
20 import time
21
22 # Register the streaming http handlers with urllib2
23 register_openers()
24
25 # set query elements
26 user = raw_input('TextServer Username: ')
27 tkid = raw_input('TextServer Job Token ID: ')
28 outfname = raw_input('Output ZIP file: ')
29
30 # service URL
31 TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"
32
33 # prepare request to poll for completion and retrieve results
34 datagen, headers = multipart_encode({'username' : user,
35 'tokenID' : tkid
36 })
37 request = urllib2.Request(TextServer_URL + "/resultRetrieve",
38 datagen,
39 headers)
40
41 try:
42 # send request to check for results
43 print "Polling server for job completion..."
44 resp = urllib2.urlopen(request).read()
45
46 except urllib2.HTTPError, e:
47 if (e.code == 503 and e.read()[0:8] == "[TS-125]" ) :
48 # http status 503, textserver code TS-125 means the job is not finished yet.
49 print "Job not finished yet"
50 else :
51 # some actual error happened. abort
52 print e, "-", e.read()
53 exit()
54
55
56 # no error, save results to output file
57 outf=open(outfname, 'w+')
58 print >>outf, resp
59 outf.close()
60 print "Job finished. Results saved to",outfname