Instance

From PyWPS
Jump to: navigation, search

PyWPS Instance

PyWPS can be installed once in a server, but it may be configured to run several WPS services (instances). Each WPS service just needs a set of processes (stored in one directory) and a local/specific configuration file. Processes are stored together as python programs in one directory. To setup this, you have to:

1 - create processes directory – directory, where you store all processes for particular PyWPS instance:

$ mkdir -p /usr/local/wps/processes

2 - copy configuration file-template to some location, and configure you PyWPS installation (see below):

$ cp PyWPS/pywps/default.cfg /usr/local/wps/pywps.cfg
$ $EDITOR /usr/local/wps/pywps.cfg

3 - create any process(es) in the processes directory. You can start with the example processes, stored in PyWPS/pywps/processes directory. See how-to-write-custom-process for how to write custom processes:

$ cp PyWPS/pywps/processes/ultimatequestionprocess.py /usr/local/wps/processes/

4 - Every process, you have in the processes directory, needs to be registered in the init.py file. The file has to contain at least:

__all__=["ultimatequestionprocess"]

Where all represents list of processes (file names) within the processes directory. The following will generate the file and content.

$ cd /usr/local/wps/processes/
$ echo "__all__=['ultimatequestionprocess']" > __init__.py

That's all....

Accepted environment variables

Following environemnt variables are accepted by PyWPS instance

PYWPS_CFG
Configuration file location
PYWPS_PROCESSES
Directory, where the processes are stored
PYWPS_TEMPLATES
Templates directory (structure should be similar to file:pywps/Templates)

Note: 99% of the time it is only necessary to configure PYWPS_CFG and PYWPS_PROCESSES


CGI wrapper script

To configure PyWPS via CGI, copy the PyWPS cgi wrapper script to your cgi-bin directory and edit the variables, which are set there:

$ cp PyWPS/webservices/cgi/pywps.cgi /usr/lib/cgi-bin
$ $EDITOR /usr/lib/cgi-bin/pywps.cgi

Note: Windows users, you have to script some similar script as .bat file or in python. This example is written as SHELL script.

The wrapper script will look like this:

#!/bin/sh

# Author: Jachym Cepicky
# Purpose: CGI script for wrapping PyWPS script
# Licence: GNU/GPL
# Usage: Put this script to your web server cgi-bin directory, e.g.
# /usr/lib/cgi-bin/ and make it executable (chmod 755 pywps.cgi)

# NOTE: tested on linux/apache

export PYWPS_CFG=/usr/local/wps/pywps.cfg
export PYWPS_PROCESSES=/usr/local/wps/processes/

/usr/local/pywps-VERSION/wps.py $1

The wrapper script is just a simple bash script that sets then environment variables mainly PYWPS_CFG/PYWPS_PROCESSES specifically for this instance, (see local configuration/processes in section: Configuration File) and then it executes wps.py script using any variables passed to the script. The last line should point to what ever location the wps.py file has.

Note: $1 is a bash parameters, that contains the arguments passed to the pywps.cgi script, which in turn need to be forward to the wps.py. This variable will contain the request done

CGI Apache configuration

Following the example above (path locations and names), you just need to setup the apache configuration file (httpd.conf or apache2.conf) as follows:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
AddHandler cgi-script .cgi
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +FollowSymLinks
                Order allow,deny
                Allow from all
        </Directory>

The ScriptAlias translates and URL like http://foo/cgi-bin/ into /usr/lib/cgi-bin and will look inside this path. The AddHanlder informs apache that .cgi extension is a CGI script and should be executed instead of being served as a web page. Finally Options +ExecCGI options in the Directory section allows for the execution of scripts in this particular directive.

The Apache configuration here presented is very standard, never the less is may change according to user needs / system properties.

Testing PyWPS instance

In the bash, you can do the default test:

$ cd /usr/lib/cgi-bin
$ sh pywps.cgi "request=GetCapabilities&service=WPS"

If desired, the same test can be executed with file wps_getcapabilites_request.xml locate in the /tests/request/wps_getcapabilites_request.xml path of PyWPS source code.

$ cd /usr/lib/cgi-bin
$ export REQUEST_METHOD=POST; cat wps_getcapabilities_request.xml | pywps.cgi

When using a file it is necessary to set the request method flag to POST, so that PyWPS knows what sort of parse is necessary to deal with stdin.

If everything goes ok you should have the GetCapabilities response document.....or the Exception Response with any problems that may exist. (See: Installation)

To test the CGI script from Apache the request should be done from a browser by typing the Server's URN plus path to CGI script, followed by the WPS KVP request.

http://localhost/cgi-bin/pywps.cgi?request=GetCapabilities&service=WPS


Debugging paths and env variables

Unset env variables, problems with python paths, problems running under Apache, etc, can give a lot of headaches. The only solution is to have sufficient information to understand what is going on. A basic python script called as CGI (http://foo/debug.py) can dump Apache's env variables and Python paths

#!/usr/bin/python 
import sys 
sys.stderr = sys.stdout 
import os,sys 
from cgi import escape 
print "Content-type: text/html" 
print 
print "<html><head><title>Situation snapshot</title></head><body><p>" 
print "Running:" 
print "<b>Python %s</b><br><br>" %(sys.version) 
print "Environmental variables:<br>" 
print "<ul>" 
for k in sorted(os.environ): 
	print "<li><b>%s:</b>\t\t%s<br>" %(escape(k), escape(os.environ[k]))
	#print "<li><b>%s:</b>\t\t%s<br>" %(k, os.environ[k])  
print "</p>"	
print "<b> Python paths:</b>"
print "<p>"
for p in sorted(sys.path):
	print "<li><b>%s</b><br>" %(escape(p))

print "</ul></p></body></html>"

To test the same parameters but in bash it's better this script (without HTML content)

#!/usr/bin/python 
import sys 
sys.stderr = sys.stdout 
import os,sys  
print "Python %s" %(sys.version) 
print "Environmental variables:" 
for k in sorted(os.environ): 
	print "%s:%s" %(k, os.environ[k])  
print ""	
print "Python paths:"
print ""
for p in sorted(sys.path):
	print "%s" % p