soap_client.py
2.18 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
#!/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-SNAPSHOT/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='port to run the service; default: 80')
parser.add_option('--host', type='string', action='store',
dest='host', default='ws.multiservice.nlp.ipipan.waw.pl',
help='host to run the service; default: ws.multiservice.nlp.ipipan.waw.pl')
(opts, args) = parser.parse_args()
t1 = time.time()
client = _getClient(opts.host, opts.port)
token = client.service.analyzeChain(
text=sys.stdin.read(),
parts=_getParts(client, args),
inputFormat='TEXT',
outputFormat='TEI')
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))
t2 = time.time()
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()