## page was renamed from WukooPy/QuickInto ##language:zh ''' 如何利用 WukooPy 进行Web 发布 ''' ::-- ZoomQuiet [<>] == 建立一个wukoo_helloworld包 == 所有应用项目均以python包方式发布,在这里建立python包的方法很多,不再详述。用于测试,比较偷赖的方法是: 1)在系统site-packages子目录下建立wukoo_helloworld子目录 2)在wukoo_helloworld下建立一个__init__.py 这样我们就建立了一个wukoo_helloworld包,下面如不特指,一切操作均在wukoo_helloworld子目录下进行 == 建立项目 == === 建立发布内容 === 增加一个helloword.py文件,内容: {{{ #!python def helloworld(wkp): return 'Hello World' }}} 这样我们就做了一个函数发布的页面,页面内容为Hello World,wkp参数为api调用入口 === 制作页面发布接口 === 建立发布内容后,就要建立url调用逻辑,这种系统指定使用一个root函数,修改文件为 {{{ #!python def helloworld(wkp): return 'Hello World' def root(): result=helloworld return result }}} === 制作项目发布接口 === 每个项目均应制作一个内容发布接口函数create_publisher,该函数调用不用的语法规则(使用不用的publisher),上页内容增加为: {{{ #!python def helloworld(wkp): return 'Hello World' def root(): result=helloworld return result from wukoopy.publish_wukoopy import Publisher ##使用publish_wukoopy发布 def create_publisher(): return Publisher( root ) }}} wukoopy下内置quixote1,quixote2,wukoopy,karrigell四种发布方式,Publisher 参数为: 第一个参数:项目入口,每种publisher的入口形式均不一样,wukoopy为root函数 session_manager:为使用sessions,不加则不使用 config:配置文件 logger:log记录 **kwargs :其它配置 到此我们的项目基本做完了 === 选择不同方式发布 === 在wukoo.server下有不同的server发布方式 1)选择simple方式 (1)在命令行下执行 {{{ python \*\*\simple_server.py --factory wukoo_helloworld.helloworld.create_publisher }}} (2)使用python程序 建立一个run_simple.py文件,内容 {{{ #!python from wukoo_helloworld.helloworld import create_publisher from wukoopy.server.simple_server import run run(create_publisher) }}} 运行后,访问http://localhost:8080/ 2)选择cgi访问 建立一个run_cgi.cgi文件,并放在apache可以访问的地方 {{{ #!python from wukoo_helloworld.helloworld import create_publisher from wukoopy.server.cgi_server import run run(create_publisher) }}} 3)选择fast cgi访问 建立一个run_cgi.fcgi文件,并放在apache可以访问的地方(apache配置不详述) {{{ #!python from wukoo_helloworld.helloworld import create_publisher from wukoopy.server.fastcgi_server import run run(create_publisher) }}} 4)选择xitami访问 建立一个run_xitami.py文件 {{{ #!python from wukoo_helloworld.helloworld import create_publisher from wukoopy.server.xitami_server import run run(create_publisher,'helloworld') }}} 5)选择mod_python访问 配置apache.conf {{{ SetHandler python-program PythonHandler wukoopy.server.mod_python_handler PythonOption quixote-publisher-factory wukoo_helloworld.helloworld.create_publisher PythonInterpreter wukoo_helloworld.helloworld PythonDebug On }}} 6)选择scgi方式 (略) 7)server下还用几种发布方式,参照上述进行。(有一些还是半成品,文件名中有todo) == 更多特性 == === 加入子页面 === 增加一个index,初步展示调用api的方法: {{{ #!python def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='' return result def helloworld(wkp): return 'Hello World' def root(): result=index result.hello=helloworld return result }}} === 发布一个静态目录 === 建立一个images子目录,并发布这个目录为/images {{{ #!python def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='' return result def helloworld(wkp): return 'Hello World' import os.path def images(dgn,*args): return dgn.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def root(): result=index result.hello=helloworld result.images=images return result }}} === 发布一个静态文件 === 在目录下放一个favicon.ico文件,并发布这个favicon,发布为/favicon.ico {{{ #!python def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='' return result def helloworld(wkp): return 'Hello World' import os.path def images(wkp,*args): return wkp.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def favicon(wkp): return wkp.StaticFile(os.path.join(os.path.dirname(__file__),'favicon.ico'),mime_type='image/x-icon') def root(): result=index result.hello=helloworld result.images=images result.favicon_ico=favicon #使用扩展名为.ico return result }}} === 动态改态页面属性 === 增加一个函数包装函数_charsetGB2312,将index改为GB2312发布 {{{ #!python def _charsetGB2312(fn): '''函数包装函数,将页面设为gb2312''' def _init(dgn,*args): dgn.set_charset('gb2312') return fn(dgn,*args) return _init #--------------------- def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='' return result index=_charsetGB2312(index) def helloworld(wkp): return 'Hello World' import os.path def images(wkp,*args): return wkp.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def favicon(wkp): return wkp.StaticFile(os.path.join(os.path.dirname(__file__),'favicon.ico'),mime_type='image/x-icon') def root(): result=index result.hello=helloworld result.images=images result.favicon_ico=favicon #使用扩展名为.ico return result }}} === 使用基本认证,设置页面访问权限 === 增加一个权限函数_baseAuth,将protect页面设置访问权限 {{{ #!python def _charsetGB2312(fn): '''函数包装函数,将页面设为gb2312''' def _init(dgn,*args): dgn.set_charset('gb2312') return fn(dgn,*args) return _init def _baseAuth(dgn,func,realm='Protected'): t=dgn.get_baseauth() if t is not None and t==('1','1'): result=None else: dgn.set_status(401) dgn.set_charset('gb2312') dgn.set_header('WWW-Authenticate', 'Basic realm="%s"' % realm) result='密码虽然弱智,你还是访问不了' return result #--------------------- def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='你能进来算你牛

' result+='都告诉密码了还能进不来

' result+='' return result index=_charsetGB2312(index) def helloworld(wkp): return 'Hello World' def protect(wkp): return '哈哈,你进来了' protect._q_access=_baseAuth import os.path def images(wkp,*args): return wkp.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def favicon(wkp): return wkp.StaticFile(os.path.join(os.path.dirname(__file__),'favicon.ico'),mime_type='image/x-icon') def root(): result=index result.hello=helloworld result.images=images result.protect=protect result.favicon_ico=favicon #使用扩展名为.ico return result }}} === 动态构建和发布一个图形 === 增加msn函数,从远程获得图形文件并以/msn.jpg发布出去 {{{ #!python def _charsetGB2312(fn): '''函数包装函数,将页面设为gb2312''' def _init(dgn,*args): dgn.set_charset('gb2312') return fn(dgn,*args) return _init def _baseAuth(dgn,func,realm='Protected'): t=dgn.get_baseauth() if t is not None and t==('1','1'): result=None else: dgn.set_status(401) dgn.set_charset('gb2312') dgn.set_header('WWW-Authenticate', 'Basic realm="%s"' % realm) result='密码虽然弱智,你还是访问不了' return result #--------------------- def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='你能进来算你牛

' result+='都告诉密码了还能进不来

' result+='这是俺的msn,zq老大做的,频繁访问他的网站会受不了

' result+='' return result index=_charsetGB2312(index) def helloworld(wkp): return 'Hello World' def protect(wkp): return '哈哈,你进来了' protect._q_access=_baseAuth import os.path def images(wkp,*args): return wkp.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def favicon(wkp): return wkp.StaticFile(os.path.join(os.path.dirname(__file__),'favicon.ico'),mime_type='image/x-icon') def msn(wkp): wkp.set_content_type('image/jpeg') result=wkp.web.urlget( 'http://wiki.woodpecker.org.cn/moin/WukooPy/TouchAuthor?action=AttachFile&do=get&target=hui5774hot.png' ) return result def root(): result=index result.hello=helloworld result.images=images result.protect=protect result.favicon_ico=favicon #使用扩展名为.ico result.msn_jpg=msn return result }}} === URL MAP处理 === 增加urlmap函数,用来作为缺省urlmap的处理函数,很简单,有*args参数即可 {{{ #!python def _charsetGB2312(fn): '''函数包装函数,将页面设为gb2312''' def _init(dgn,*args): dgn.set_charset('gb2312') return fn(dgn,*args) return _init def _baseAuth(dgn,func,realm='Protected'): t=dgn.get_baseauth() if t is not None and t==('1','1'): result=None else: dgn.set_status(401) dgn.set_charset('gb2312') dgn.set_header('WWW-Authenticate', 'Basic realm="%s"' % realm) result='密码虽然弱智,你还是访问不了' return result #--------------------- def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='你能进来算你牛

' result+='都告诉密码了还能进不来

' result+='这是俺的msn,zq老大做的,频繁访问他的网站会受不了

' result+='什么样的垃级,俺都照吃不误

' result+='' return result index=_charsetGB2312(index) def helloworld(wkp): return 'Hello World' def protect(wkp): return '哈哈,你进来了' protect._q_access=_baseAuth def urlmap(wkp,*args): return '哈'+str(args)+',原来俺吃的是这个' import os.path def images(wkp,*args): return wkp.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def favicon(wkp): return wkp.StaticFile(os.path.join(os.path.dirname(__file__),'favicon.ico'),mime_type='image/x-icon') def msn(wkp): wkp.set_content_type('image/jpeg') result=wkp.web.urlget( 'http://wiki.woodpecker.org.cn/moin/WukooPy/TouchAuthor?action=AttachFile&do=get&target=hui5774hot.png' ) return result def root(): result=index result.hello=helloworld result.images=images result.protect=protect result.urlmap=urlmap result.favicon_ico=favicon #使用扩展名为.ico result.msn_jpg=msn return result }}} === 使用页面变量,设置页面访问权限 === 增加一个权限函数_formAuth,将formprotect页面设置访问权限 {{{ #!python def _charsetGB2312(fn): '''函数包装函数,将页面设为gb2312''' def _init(dgn,*args): dgn.set_charset('gb2312') return fn(dgn,*args) return _init def _baseAuth(dgn,func,realm='Protected'): t=dgn.get_baseauth() if t is not None and t==('1','1'): result=None else: dgn.set_status(401) dgn.set_charset('gb2312') dgn.set_header('WWW-Authenticate', 'Basic realm="%s"' % realm) result='密码虽然弱智,你还是访问不了' return result def _formAuth(dgn,func): if dgn.get_form('user')=='王二' and dgn.get_form('password')=='麻子': result=None else: dgn.set_charset('gb2312') result='只有王二有权进,去求他吧' return result #--------------------- def index(wkp): wkp.set_content_type("text/html") result="" result+='Hello World

' result+='你能进来算你牛

' result+='都告诉密码了还能进不来

' result+='这是俺的msn,zq老大做的,频繁访问他的网站会受不了

' result+='什么样的垃级,俺都照吃不误

' result+='试试能不能进

' result+='只有王二才能进

' result+='' return result index=_charsetGB2312(index) def helloworld(wkp): return 'Hello World' def protect(wkp): return '哈哈,你进来了' protect._q_access=_baseAuth protect=_charsetGB2312(protect) def form_protect(wkp): return '哈哈,我王二麻子进来了' form_protect._q_access=_formAuth form_protect=_charsetGB2312(form_protect) def urlmap(wkp,*args): return '哈'+str(args)+',原来俺吃的是这个' import os.path def images(wkp,*args): return wkp.StaticDirectory(os.path.join(os.path.dirname(__file__),'images'),*args) def favicon(wkp): return wkp.StaticFile(os.path.join(os.path.dirname(__file__),'favicon.ico'),mime_type='image/x-icon') def msn(wkp): wkp.set_content_type('image/jpeg') result=wkp.web.urlget( 'http://wiki.woodpecker.org.cn/moin/WukooPy/TouchAuthor?action=AttachFile&do=get&target=hui5774hot.png' ) return result def root(): result=index result.hello=helloworld result.images=images result.protect=protect result.form_protect=form_protect result.urlmap=urlmap result.favicon_ico=favicon #使用扩展名为.ico result.msn_jpg=msn return result }}} 暂时到这:)