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 is a high-level Python Web framework best known for rapid development of data-intensive web applications. To create a simple "Hello World" application,
$ django-admin.py startproject hello_world
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello World!")
from django.conf.urls import patterns, include, url
urlpatterns = patterns(
'',
url(r'^$', 'hello_world.views.home', name='home'),
)
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from SnapSearch import Client, Detector, Interceptor
interceptor = Interceptor(Client(api_email, api_key), Detector())
from SnapSearch.wsgi import InterceptorMiddleware
application = InterceptorMiddleware(application, interceptor)
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)
# 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>
$ 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>
$ 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