Project

General

Profile

run_wps_from_client.py

Runs a very simple WPS client on one of the services at climate4impact - Maarten Plieger, 10/31/2016 03:14 PM

Download (2.86 KB)

 
1
#!/bin/python
2

    
3
"""
4

5
Obtain an access token:
6
- Login with a google account at https://climate4impact.eu/impactportal/account/OAuth2.jsp 
7
- Generate via https://climate4impact.eu/impactportal/tokenapi?service=account&request=generatetoken
8
- Copy paste the accesstoken value in the "token" variable below.
9

10
Author: Maarten Plieger, KNMI, 2016-10-04
11
"""
12

    
13
token="put_your_access_token_here" # without a correct access token only public services do work, like this one.
14

    
15

    
16
 
17

    
18
wpsendpoint="https://climate4impact.eu/impactportal/WPS/"+token+"/?"
19

    
20
inputa=11;
21
inputb=22;
22
operator="divide" # Any of add, substract, multiply, divide
23

    
24
print "Calculating ["+str(inputa)+" "+str(operator)+" "+str(inputb)+"]"
25

    
26
import xml.etree.ElementTree as et
27
import urllib2
28
import time
29
from StringIO import StringIO
30

    
31
""" Parses URL to XML tree """
32
def getandparsexml(url):
33
  response = urllib2.urlopen(url)
34
  html = response.read()
35
  it = et.iterparse(StringIO(html))
36
  for _, el in it:
37
      if '}' in el.tag:
38
          el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
39
  root = it.root
40
  return root
41
  
42
# Execute the ultimatequestionprocess aynchronousely and obtain the statusLocation for progress
43
datainputs = "inputa="+str(inputa)+";inputb="+str(inputb)+";operator="+str(operator)
44
root = getandparsexml(wpsendpoint+"service=WPS&version=1.0.0&request=execute&storeExecuteResponse=true&status=true&identifier=binaryoperatorfornumbers_10sec&datainputs="+datainputs)    
45
monitorURL = root.attrib['statusLocation']
46

    
47
print "Process executed checking " +monitorURL
48

    
49
# Get starttime of process, sometimes the monitorURL is not immediately available. Iterate until a valid creationtime is available would be good practice.
50
numberOfTries=10
51
while numberOfTries>0:
52
  numberOfTries = numberOfTries-1 
53
  try:
54
    root = getandparsexml(monitorURL)
55
    numberOfTries = -1
56
    print "Process started at "+str(root[1].attrib['creationTime'])
57
  except:
58
    time.sleep(1)
59
    pass
60

    
61
# Wait for process accepted to pass...
62
ProcessAcceptedFound = True
63

    
64
while ProcessAcceptedFound == True:
65
  root = getandparsexml(monitorURL)
66
  ProcessAcceptedFound = False
67
  for elem in root.iter(tag="ProcessAccepted"):
68
    ProcessAcceptedFound = True;
69
    print elem.tag, elem.attrib
70
  time.sleep(1)
71

    
72

    
73
# Now Monitor progress, polling the statuslocation frequently
74
percentage = 0
75
while percentage<100:
76
  root = getandparsexml(monitorURL)
77
  try:
78
    for elem in root.iter(tag="ProcessStarted"):
79
      percentage = float(elem.attrib['percentCompleted'])
80
    for elem in root.iter(tag="ProcessSucceeded"):
81
      percentage = 100
82
      break
83
  except:
84
    percentage=100.0
85
    pass
86
  print "Progress: "+str(percentage)+ " complete."
87
  time.sleep(1)
88

    
89
# Obtain the answer  
90
root = getandparsexml(monitorURL)
91
try:
92
  print "The answer for ["+str(inputa)+" "+str(operator)+" "+str(inputb)+"] is "+(root[2][0][2][0].text)
93
except:
94
  print "Processing failed: "+root[1][0][0][0][0].text
95
  pass