Package: wfastcgi
WFastCGIwfastcgi.py provides a bridge betweenIISand Python
using WSGI and FastCGI, similar to whatmod_pythonprovides for Apache HTTP
Server.It can be used with any Python web application or framework that supports WSGI,
and provides an efficient way to handle requests and process pools through IIS.InstallationDownloading PackageTo install via the Python Package Index (PyPI), type:pipinstallwfastcgiInstalling IIS and FastCGISee theIIS Installationpage for
information about installing IIS on your version of Windows.The Application Development/CGI package is required for use withwfastcgi.Enabling wfastcgiOncewfastcgiand IIS are installed, runwfastcgi-enableas an
administrator to enablewfastcgiin the IIS configuration. This will
configure a CGI application that can then be specified as aroute handler.wfastcgi-enableTo disablewfastcgibefore uninstalling, runwfastcgi-disable.wfastcgi-disablepipuninstallwfastcgiNote: uninstallingwfastcgidoes not automatically unregister the CGI
application.If the first argument passed towfastcgi-enableorwfastcgi-disableis
a valid file, the entire command line is used to register or unregister the CGI
handler.For example, the following command will enable wfastcgi with IIS Express and a
specific host configuration:wfastcgi-enable"C:\Program Files (x86)\IIS Express\appcmd.exe"/apphostconfig:C:\Path\To\applicationhost.configYou can disable wfastcgi in the same configuration file usingwfastcgi-disablewith the same options:wfastcgi-disable"C:\Program Files (x86)\IIS Express\appcmd.exe"/apphostconfig:C:\Path\To\applicationhost.configRoute HandlersRouting requests to your Python application requires some site-local
configuration. In your site’sweb.configfile, you will need to add a
handler and some app settings:<configuration><system.webServer><handlers><addname="Python FastCGI"path="*"verb="*"modules="FastCgiModule"scriptProcessor="C:\Python36\python.exe|C:\Python36\Lib\site-packages\wfastcgi.py"resourceType="Unspecified"requireAccess="Script"/></handlers></system.webServer><appSettings><!-- Required settings --><addkey="WSGI_HANDLER"value="my_app.wsgi_app()"/><addkey="PYTHONPATH"value="C:\MyApp"/><!-- Optional settings --><addkey="WSGI_LOG"value="C:\Logs\my_app.log"/><addkey="WSGI_RESTART_FILE_REGEX"value=".*((\.py)|(\.config))$"/><addkey="APPINSIGHTS_INSTRUMENTATIONKEY"value="__instrumentation_key__"/><addkey="DJANGO_SETTINGS_MODULE"value="my_app.settings"/><addkey="WSGI_PTVSD_SECRET"value="__secret_code__"/><addkey="WSGI_PTVSD_ADDRESS"value="ipaddress:port"/></appSettings></configuration>The value forscriptProcessoris displayed in the output ofwfastcgi-enableand may vary from machine to machine. The values forpathandverbmay also be customized to further restrict the requests
for which this handler will be used.Thenamevalue may be used in nestedweb.configfiles to exclude this
handler. For example, adding aweb.configto yourstatic/subdirectory
containing<removename="PythonFastCGI" />will prevent IIS from serving
static files through your Python app.The provided app settings are translated into environment variables and can be
accessed from your Python application usingos.getenv. The following
variables are used bywfastcgi.WSGI_HANDLERThis is a Python name that evaluates to the WSGI application object. It is a
series of dotted names that are optionally called with no parameters. When
resolving the handler, the following steps are used:As many names as possible are loaded usingimport. The last name is
never imported.Once a module has been obtained, each remaining name is retrieved as an
attribute. If()follows the name, it is called before getting the
following name.Errors while resolving the name are returned as a simple 500 error page.
Depending on your IIS configuration, you may only receive this page when
accessing the site from the same machine.PYTHONPATHPython is already running when this setting is converted into an environment
variable, sowfastcgiperforms extra processing to expand environment
variables in its value (including those added from app settings) and to expandsys.path.If you are running an implementation of Python that uses a variable named
something other thanPYTHONPATH, you should still specify this value asPYTHONPATH.WSGI_LOGThis is a full path to a writable file where logging information is written.
This logging is not highly efficient, and it is recommended that this setting
only be specified for debugging purposes.WSGI_RESTART_FILE_REGEXThe regular expression used to identify when changed files belong to your
website. If a file belonging to your site changes, all active CGI processes
will be terminated so that the new files can be loaded.By default, all*.pyand*.configfiles are included. Specify an empty
string to disable auto-restart.APPINSIGHTS_INSTRUMENTATIONKEYProviding an instrumentation key with this value will enable request tracing
withApplication Insightsfor your entire site. If you have not installed theapplicationinsightspackage, a warning is written toWSGI_LOG(if enabled) but the site will
operate normally.Application Insights is a low-overhead monitoring system for tracking your
application’s health and performance. When enabled, all errors in your site
will be reported through Application Insights.DJANGO_SETTINGS_MODULEA commonly used registry key when deploying sites built using Django. Typically
Django sites will setWSGI_HANDLERtodjango.core.handlers.wsgi.WSGIHandler()and load app-specific settings
through the module specified by this value.Sites using frameworks other than Django do not need to specify this value.WSGI_PTVSD_SECRETProviding an arbitrary string here and including theptvsdmodule in your environment will
automatically enable remote debugging of your web site. The string in this
application setting should be treated as a password, and needs to be provided
when attaching to the running site.WSGI_PTVSD_ADDRESSWhenWSGI_PTVSD_SECRETis specified, this value may also be specified to
override the default listening address for remote debugging. By default,
your site will listen onlocalhost:5678, but in many cases you may need
to change this to0.0.0.0:some-portin order to attach remotely.Remember that you will also need to forward the port through any firewalls
you might have configured.