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