Python for Finance: Analyze Big Financial Data

(Elle) #1
return response(environ,    start_response)

Here, environ is a dictionary containing all incoming information. The Request function


wraps all information in a manner that makes accessing the environ information a bit


more convenient. start_response is usually used to indicate the start of a response.


However, with Werkzeug you have the Response function, which takes care of the


response.


All parameters provided to the web service are found in the request.args attribute, and


this is what we provide to the get_option_value function. This function returns either an


error message in text form or the calculated option value in text form.


To be better able to serve this function (e.g., via a local web server), we put the function


into a separate WSGI script and add the serving functionality to it. Example 14-10 shows


the code of this script, called vol_pricing.py.


Example 14-10. Python script for volatility option valuation and web service helper


function



Valuation of European volatility options


in Gruenbichler-Longstaff (1996) model


square-root diffusion framework


— WSGI application for web service



from vol_pricing_service import get_option_value
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple


def application(environ, start_response):
request = Request(environ)


wrap environ in new object


text = get_option_value(request.args)


provide all parameters of call to function


get back either error message or option value


response = Response(text, mimetype=‘text/html’)


generate response object based on the returned text


return response(environ, start_response)


if name==‘main’:
run_simple(‘localhost’, 4000 , application)


Being in the right subdirectory (volservice), you can now start the application by


executing the following command via the shell or command-line interface:


$   python  vol_pricing.py
* Running on http://localhost:4000/

This fires up a separate Python process that serves the WSGI application. Using urllib, we


can now access the “full power” of the web service. Copying the URL in your web browser


and pressing the Return key yields something like the result shown in Figure 14-12.

Free download pdf