Python Batch Client
From Textserver wiki
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 fname = raw_input('Input ZIP file: ')
27 lang = raw_input('Language: ')
28 out = raw_input('Output format (xml,json,conll,naf): ')
29 user = raw_input('TextServer Username: ')
30 pwd = raw_input('TextServer Password: ')
31
32 # Encode query in a form-data.
33 # 'headers' contains the necessary Content-Type and Content-Length.
34 # 'datagen' is a generator object that yields the encoded parameters.
35 datagen, headers = multipart_encode({'username' : user,
36 'password' : pwd,
37 'file' : open(fname,"rb"),
38 'language' : lang,
39 'output' : out,
40 'interactive':'0'
41 } )
42 # service URL
43 TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"
44 service = "SERVICENAME"
45
46 # Create the Request object
47 request = urllib2.Request(TextServer_URL + "/processQuery/" + service,
48 datagen,
49 headers)
50
51 try:
52 # Actually do the request, and get the response
53 resp = urllib2.urlopen(request).read()
54
55 except urllib2.HTTPError, e:
56 # handle connection errors
57 print e, "-", e.read()
58 exit()
59
60 # Server response should include a job tokenID, retrieve it
61 dom = xml.etree.ElementTree.XML(resp)
62 tkid = dom.text
63 print "Job sumbitted. Token id=",tkid