日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

cherrypy 入门笔记(1) hello world

發(fā)布時(shí)間:2025/7/25 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cherrypy 入门笔记(1) hello world 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。