WSGI for web apps

Silk will execute Python code written using the WSGI model. How it does so depends on which silk platform version your account is hosted on.

Silk platform version 1

Silk will execute scripts ending in .py within a Python FCGI handler that runs within the WSGI model. Scripts will be dynamically loaded as modules on the first request, and their 'request_handler' attribute should be set to the instance of a function implementing the start_response() callable.

Example WSGI script

1
2
3
4
5
6
7
def my_handler(environ, start_response):
       start_response('200 OK', [('Content-Type', 'text/plain')])
       yield "Hello World\n"
       for k, v in sorted(environ.items()):
            yield "%s: %s\n" % (k, v)

request_handler = my_handler

Reloading your app

The environment variable wsgi_max_requests can be set to cause the FCGI wrapper to reload your Python script (module) every N requests. During development, you can set this to something very low to see incremental changes as you work. Otherwise, the FCGI wrapper will cache your code for FCGI_MAX_REQUESTS.

For example, in your .htaccess file:

1
SetEnv wsgi_max_requests 10

Silk platform version 2

Python applications use Phusion Passenger as an app server. You can run an application by placing the following in an .htaccess file in your ~/www-root/ directory:

1
2
3
4
PassengerAppType wsgi
PassengerStartupFile passenger_wsgi.py
PassengerAppRoot /users/m/y/mynetid/www-root
PassengerPython /usr/bin/python

PassengerAppRoot should be the full path to the directory where your application's startup file is located.

PassengerPython should be the full path to your preferred version of Python.

Python version PassengerPython value
2.7 (default) /usr/bin/python
3.5 /usr/local/bin/python35

Modules

Python modules are managed using the pip command.

You can see which modules are currently installed by typing pip list, or see which you have installed yourself with pip list --user.

Additional modules can be installed by typing pip install --user <modulename>, which will place them in your home directory, under ~/.local/. Scripts or commands included with a module are placed in ~/.local/bin/ and can either be run directly from there or added to your PATH by adding a line such as the following to ~/.bash_profile:

1
export PATH=~/.local/bin:$PATH