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.).

Django App

Django is a high-level Python Web framework best known for rapid development of data-intensive web applications. To create a simple "Hello World" application,

  1. start a hello_world project with django-admin.py
$ django-admin.py startproject hello_world
  1. create a home view in hello_world/view.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello World!")
  1. add the home view to hello_world/urls.py
from django.conf.urls import patterns, include, url

urlpatterns = patterns(
    '',
    url(r'^$', 'hello_world.views.home', name='home'),
)

Integration

  1. integration all happens in hello_world/wsgi.py, the original content should look like,
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  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
application = InterceptorMiddleware(application, interceptor)
  1. putting it all together.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# API credentials
api_email = "<email>"
api_key = "<key>"

# 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
application = InterceptorMiddleware(application, interceptor)

Verification

  1. server servers the Django application at http://<server_ip>:5000/.
# Apache server's virtual host configuration (partial)
<VirtualHost *:5000>
    ServerName <server_name>
    CustomLog /<server_root>/log/access
    ...
    WSGIProcessGroup <server_name>
    WSGIDaemonProcess <server_name> python-path=/<server_root>/hello_world:/usr/lib/python2.6/site-packages:/usr/lib64/python2.6/site-packages display-name=%{GROUP}
    WSGIScriptAlias / /<server_root>/hello_world/wsgi.py
    DocumentRoot /<server_root>/hello_world/
    <Directory /<server_root>/hello_world>
        ...
    </Directory>
</VirtualHost>
  1. search engine robot visits (emulated with curl),
$ curl -i A "Googlebot" http://<server_ip>:5000/main.py

and receives an intercepted HTTP response

HTTP/1.1 200 OK
Date: Thu, 13 Mar 2014 14:20:18 GMT
Server: Apache/2.2.15 (CentOS)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/plain; charset=UTF-8

<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.
$ cat /<server_root>/log/access
...
<robot_ip> - - [13/Mar/2014:22:20:19 +0800] "GET / HTTP/1.1" 200 14
<snapsearch_ip> - - [13/Mar/2014:22:20:18 +0800] "GET / HTTP/1.1" 200 107

Table Of Contents

Previous topic

Integration with WSGI Applications

Next topic

Integration with Python CGI Scripts