cherrypy 入门笔记(1) hello world
cherrypy 是一個(gè)小型web框架,可以用來(lái)做一點(diǎn)小型玩具程序。最近閑的無(wú)聊,拿來(lái)學(xué)習(xí)一下.
hello world 應(yīng)用
hello.py:
import cherrypyimport os.pathcurrent_dir = os.path.dirname(os.path.abspath(__file__))class Hello(object):content = """<html><head><title>hello</title><link href="/static/hello-style.css" rel="stylesheet" type="text/css"/></head><body><h1 id="first-step">hello world</h1><script src="/static/hello.js"></script></body></html>"""@cherrypy.exposedef index(self): # define the default pagereturn Hello.content@cherrypy.exposedef hello(self): # define the hello pagereturn "Hello"if __name__ == '__main__':cherrypy.quickstart(Hello(), config={'/static':{'tools.staticdir.on': True,'tools.staticdir.dir': os.path.join(current_dir, "static")}})css和js文件放在和python文件相同目錄下的static文件夾中
css文件? hello-style.css:
h1#first-step {color: green;border: 1px dotted #d5d5d5;font-size: 30px;text-align:center; }h1#first-step:hover {color: orange;font-size: 30px;border: 1px solid #e5e5e5;-webkit-transition: all 0.6s;-moz-transition: all 0.6s;-ms-transition: all 0.6s;-o-transition: all 0.6s;transition: all 0.6s; }為了好玩再加個(gè)js彈窗效果 hello.js :
alert("hello world!");效果如下:
其中 __file__的用法 可以參考這里 http://andylin02.iteye.com/blog/933237
官方文檔的說(shuō)法是__file__是模塊加載的路徑。不過(guò)使用絕對(duì)路徑,也就是文件目錄下的static文件夾了。
但是使用IDLE下__file__沒(méi)有定義,因?yàn)闆](méi)有在任何文件中執(zhí)行。
可以使用以下代碼測(cè)試是否得到文件的絕對(duì)路徑, 當(dāng)然是要在命令行中執(zhí)行的,或者在Windows下雙擊運(yùn)行:
import os.path current_dir = os.path.dirname(os.path.abspath(__file__)) print current_dir raw_input()就能顯示出來(lái)當(dāng)前執(zhí)行python文件的文件夾路徑了.
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/jaw-crusher/p/3455819.html
總結(jié)
以上是生活随笔為你收集整理的cherrypy 入门笔记(1) hello world的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring Security3.1登陆
- 下一篇: 结构体指针和数组理解