cherrypy 入门笔记(1) hello world
生活随笔
收集整理的這篇文章主要介紹了
cherrypy 入门笔记(1) hello world
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
cherrypy 是一個小型web框架,可以用來做一點小型玩具程序。最近閑的無聊,拿來學習一下.
hello world 應用
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; }為了好玩再加個js彈窗效果 hello.js :
alert("hello world!");效果如下:
其中 __file__的用法 可以參考這里 http://andylin02.iteye.com/blog/933237
官方文檔的說法是__file__是模塊加載的路徑。不過使用絕對路徑,也就是文件目錄下的static文件夾了。
但是使用IDLE下__file__沒有定義,因為沒有在任何文件中執行。
可以使用以下代碼測試是否得到文件的絕對路徑, 當然是要在命令行中執行的,或者在Windows下雙擊運行:
import os.path current_dir = os.path.dirname(os.path.abspath(__file__)) print current_dir raw_input()就能顯示出來當前執行python文件的文件夾路徑了.
?
?
轉載于:https://www.cnblogs.com/jaw-crusher/p/3455819.html
總結
以上是生活随笔為你收集整理的cherrypy 入门笔记(1) hello world的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Security3.1登陆
- 下一篇: ubuntu在系统启动logo过后无法进