Python web.py模塊基本應用


Python web.py模塊基本應用

 

系統版本:CentOS release 6.5

安裝pip:yum install python-pip

安裝web:pip install web

主要以配置文件為主進行

 

1、入門配置

[root@IDC-105 erweima]# cat weixin.py

# -*- coding:utf-8 -*-

import web

urls = (    #定義路由,用戶訪問界面

    '/','Index'  #首頁,首頁指向

)

class Index(object):

    def GET(self):

        print "已檢測到,頁面有用戶訪問"   #瀏覽器返回打印信息

        return "Welcome to My WebSite"    #瀏覽器獲取的界面信息

if __name__ == '__main__':

    web.application(urls,globals()).run()

執行腳本,驗證結果

 

2、增加templates部署應用

a、增加templates

[root@IDC-105 erweima]# cat weixin.py

# -*- coding:utf-8 -*-

import web

urls = (    #定義路由,用戶訪問界面

    '/','Index'  #首頁,首頁指向

)

 

render = web.template.render('templates')

class Index(object):

    def GET(self):

        return render.index()

 

if __name__ == '__main__':

web.application(urls,globals()).run()

 

b、新增目錄和文件

[root@IDC-105 erweima]# tree

.

├── static

│   └── images

│       └── zuomian.jpg

├── templates

│   └── index.html

├── weixin.py

└── weixin.pyc

頁面查看

 

3、附件

自行上傳圖片

了解基本html知識

[root@IDC-105 erweima]# cat templates/index.html

<!DOCTYPE HTML PUBLIC>

<html>

 <head>

  <title> web test </title>

  <style type="text/css">

      .shou {width:60%;

             height:60px;

             background:#ccccff;

            }

 

      *{margin:0;padding:0;}

      .box{width:100%;

           height:200px;

           background:#ffff33;

          }

      .box ul {width:100%;

               height:40px;

               background:black;}

      .box ul li{

                 width:100px;

                 float:left;

                 list-style:none;

                 line-height:40px; 

                 color:white;

                 font-family:"微軟雅黑";}

  </style>

 </head>

 <body>

    <div class="shou">

       <h1>歡迎來到深圳地鐵1號線</h1>

       <a href="http://www.szmc.net/page/index.html" class="sh1">深圳地鐵官網</a>

    </div>

    <hr>

    <h2>請排隊候車</h2>

     <div class="box">

        <ul>

           <li>首頁</li>

           <li>公司新聞</li>

           <li>運營服務</li>

           <li>規划建設</li>

           <li>物業開發</li>

           <li>招標招商</li>

        </ul>

     </div>

     <img src="/static/images/zuomian.jpg" width=200px height=100px />

 </body>

</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM