webpy使用筆記

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

webpy入門

  工作環境中需要經常生產和測試伺服器,機房一直很混亂,因此萌生了開發一個簡單方便的伺服器管理系統(說的好高大上,其實就是個可以擷取伺服器資訊的小web應用)。之所以選擇webpy,正式因為它夠簡單,尤其是對於我這種python新人來說。它是一款輕量級的python web開發架構,對於個人開發小應用來說很適合。

webpy install
下載:wget http://webpy.org/static/web.py-0.37.tar.gz
安裝:python setup.py install
webpy ‘hello world‘

  可以參考webpy的官方文檔:http://webpy.org/docs/0.3/tutorial

      hello, world如下:

import weburls = (    ‘/‘, ‘index‘)class index:    def GET(self):        return "Hello, world!"if __name__ == "__main__":    app = web.application(urls, globals())    app.run()

  在webpy中,url請求的映射在urls元組中,如中GET ip:port/,會直接調用index類的GET方法,返回字串‘hello, world!‘;

  class index中包含了一個GET方法,用來處理與index相應的url的GET請求的;

  在主函數中,只需要建立一個application對象,運行就可以開啟一個簡單的web應用,預設的地址為:127.0.0.1:8080

GET && POST

  web包含兩種方法:GET和POST

      對於GET,可以採用:

class index:    def GET(self):        return "Hello, world!"

  而,對於POST,採用:

class index:    def POST(self):        data = web.input(name=None)        return "Hello, " + data.name + "!"
html模板

  在webpy中,一般採用templates來存放html分頁檔。大概的訪問方式如下:

urls = (    ‘/img‘, ‘image‘)render = web.template.render(‘templates‘)class image:    def GET(self):        return render.image()

  urls中定義了url映射,訪問ip:port/img會直接條用class image來處理;

  web.template.render(path)是用來指定存放html的目錄,上面指定了html的指定存放位置位於當前檔案夾下的templates檔案下;

  返回的render.image()表示在render所指定的目錄下尋找image.html檔案並作為返回結果。

class show:    def GET(self):        return render.show(‘hello world!‘)
$def with(str)<html>    <body>         $for i in range(5):            <h1>$str</h1>    <body></html> 

  show類是用來展示字串‘hello world!‘,下面的html為show.html,webpy支援模板,支援參數以$def with()開始作為函數的開始;

      在html中可以使用python語句,但語句前需要添加$,在上面的html中str會在頁面上列印5次。

靜態檔案

  在webpy中,提供了預設的靜態檔案的訪問方式

  •   webpy作為伺服器時,在目前的目錄下建立static目錄,webpy會自動在該目錄下尋找靜態檔案
  •       在 Apache 中可以使用 Alias 指令,在處理 web.py 之前將請求映射到指定的目錄。
webpy db

  在webpy中提供了資料庫訪問的API,其實從源碼中可以看出來是對MySQLdb的封裝,但為了方便起見用起來還是可以的。

db = web.database(dbn=‘mysql‘, db=‘test‘, user=‘root‘, pw=‘123123‘)def new_post(title, content):    db.insert(‘news‘, title=title, content=content, posted_on=datetime.datetime.utcnow())def get_post(id):    try:        return db.select(‘news‘, where=‘id=$id‘, vars=locals())[0]    except IndexError:        return Nonedef get_posts():    return db.select(‘news‘, order = ‘id DESC‘)def del_post(id):    db.delete(‘news‘, where = ‘id = $id‘, vars = locals())def update_post(id, title, content):    db.update(‘news‘, where=‘id = $id‘, vars=locals(), title=title, content=content)

  webpy也支援事務:

import webdb = web.database(dbn="postgres", db="webpy", user="foo", pw="")t = db.transaction()try:    db.insert(‘person‘, name=‘foo‘)    db.insert(‘person‘, name=‘bar‘)except:    t.rollback()    raiseelse:    t.commit()

本作品採用知識共用署名-非商業性使用-相同方式共用 3.0 未語言版本許可協議進行許可。歡迎轉載,請註明出處:
轉載自:cococo點點 http://www.cnblogs.com/coder2012

webpy使用筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.