django是一款基于python语言的WEB开源框架,本文给出了如何将基于django写的python网站部署到window的IIS上。python
笔者的运行环境:web
原理解释:django
IIS经过ISAPI能够扩展支持其余语言实现的WEB应用,isapi_wsgi-0.4.2-py2.5这个程序做为ISAPI实现了WSGI规范,api
WSGI规范是做为python web应用与web服务容器之间的接口规范,经过这个程序,对IIS的某个虚拟站点的请求就能够定向浏览器
到这个ISAPI去处理,而无需为了去部署到某个特定容器里而去改动python web的任何代码。app
步骤框架
import os, sys sys.path.append(‘C:\\Web') os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'网站
import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()this
import isapi_wsgi # The entry points for the ISAPI extension. def __ExtensionFactory__(): return isapi_wsgi.ISAPISimpleHandler(application)google
if __name__=='__main__': # If run from the command-line, install ourselves. from isapi.install import * params = ISAPIParameters() # Setup the virtual directories - this is a list of directories our # extension uses - in this case only 1. # Each extension has a "script map" - this is the mapping of ISAPI # extensions. sm = [ ScriptMapParams(Extension="*", Flags=0) ] vd = VirtualDirParameters(Name="mysite", Description = "ISAPI-WSGI ISAPISimpleHandler Django mysite", ScriptMaps = sm, ScriptMapUpdate = "replace" ) params.VirtualDirs = [vd] HandleCommandLine(params)
5. 在命令行输入: wsgi_deploy.py install ,运行以后会在IIS上建立上面脚本定义的虚拟路径"mysite", 同时你会发现一个'_wsgi_deploy.dll'文件会建立出来,这个就是ISAPI。
细心的读者不妨在IIS的"mysite“的设置里去查看下就明白了。
6.部署后,既能够经过浏览器访问你的Web App了
注:若是出现错误,如何处理?
能够在命令行输入: python -m win32traceutil 即可以输出isapi_wsgi模块输出的错误堆栈信息
一般错误都是出如今路径方面。如相似于
ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): N o module named mysite.settings 这样的问题。
这样的状况,须要去找到上述的部署脚本wsgi_deploy.py,去修改成正确的配置,而后记住须要先运行
wsgi_deploy.py remove后再运行wsgi_deploy.py install。