soap_client.py
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Michał Lenart
# This is SOAP client for Multiservice platform.
# It is available on the same license as the Multiservice itself.
#
from optparse import OptionParser
import time
import logging
import sys
import codecs
import suds
logging.basicConfig(level=logging.INFO)
sys.stdin = codecs.getreader('utf8')(sys.stdin)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
def _getClient(host, port):
url = 'http://%s:%d/WebService-1.0/ClarinWS?wsdl' % (host, port)
return suds.client.Client(url)
def _getParts(client, serviceNames):
partsList = client.factory.create('RequestPartsList')
for serviceName in serviceNames:
part = client.factory.create('RequestPart')
part['serviceName'] = serviceName.decode('utf8')
partsList.part.append(part)
return partsList
def go():
parser = OptionParser()
parser.add_option('-p', '--port', type='int', action='store',
dest='port', default=80,
help='multiservice port; default: 80')
parser.add_option('--host', type='string', action='store',
dest='host', default='ws.multiservice.nlp.ipipan.waw.pl',
help='multiservice host; default: ws.multiservice.nlp.ipipan.waw.pl')
(opts, args) = parser.parse_args()
if len(args) == 0:
print "Processing chain was not specified!"
return
client = _getClient(opts.host, opts.port)
token = client.service.analyzeChain(
text=sys.stdin.read(),
parts=_getParts(client, args),
inputFormat='TEXT',
outputFormat='TEXT')
logging.info('TOKEN ' + str(token))
status = client.service.getStatus(token)
logging.info(u'STATUS ' + str(status))
while not status in ['DONE', 'FAILED']:
time.sleep(0.1)
status = client.service.getStatus(token)
logging.info(u'STATUS ' + str(status))
if status == 'DONE':
res = client.service.getResult(token)
print res
else:
res = client.service.getException(token)
logging.error('ERROR')
print >> sys.stderr, res
if __name__ == '__main__':
go()