Integration with WSGI Applications

Overview

The Pythonic SnapSearch Client comes with a WSGI-compliant Interceptor Middleware for easy integration with existing web applications. The solution is framework agnostic, and supports any WSGI-compliant web applications, either hand-made, or built upon WSGI framewrorks (such as Django, Falcon, Flask, web2py, etc.).

Flask App

Flask is a WSGI compliant microframework for web applications. The following Python script serves a simple "Hello World" page through any of the public IP address(es) of the runner machine.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello World!\r\n"

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

Integration

  1. initialize an Interceptor.
from SnapSearch import Client, Detector, Interceptor
interceptor = Interceptor(Client(api_email, api_key), Detector())
  1. deploy the Interceptor.
from SnapSearch.wsgi import InterceptorMiddleware
app.wsgi_app = InterceptorMiddleware(app.wsgi_app, interceptor)
  1. putting it all together.
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello World!\r\n"

if __name__ == '__main__':
    # API credentials
    api_email = "<email>"  # change this to the registered email
    api_key = "<key>"  # change this to the real api credential

    # initialize the interceptor
    from SnapSearch import Client, Detector, Interceptor
    interceptor = Interceptor(Client(api_email, api_key), Detector())

    # deploy the interceptor
    from SnapSearch.wsgi import InterceptorMiddleware
    app.wsgi_app = InterceptorMiddleware(app.wsgi_app, interceptor)

    # start servicing
    app.run(host="0.0.0.0", port=5000)

Verification

  1. server starts the web application.
$ pip install Flask
$ pip install snapsearch-client-python
$ python main.py
 * Running on http://0.0.0.0:5000/
  1. search engine robot visits (emulated with curl),
$ curl -i -A "Googlebot" http://<server_ip>:5000/

and receives an intercepted HTTP response.

HTTP/1.0 200 OK
server: Werkzeug/0.9.4 Python/2.6.6
Connection: close
Date: Wed, 12 Mar 2014 16:48:25 GMT

<html><head><style type="text/css">body { background: #fff }</style></head><body>Hello World!
</body></html>
  1. server log shows both the robot and SnapSearch backend service.
 * Running on http://0.0.0.0:5000/
<robot_ip> - - [13/Mar/2014 00:42:59] "GET / HTTP/1.1" 200 -
<snapsearch_ip> - - [13/Mar/2014 00:46:21] "GET / HTTP/1.1" 200 -

Table Of Contents

Previous topic

Getting Started

Next topic

Integration with WSGI Applications