Python+Bottle+Sina SAE快速構建網站


Bottle是新生一代Python框架的代表,利用Bottle構建網站將十分簡單。

Sina SAE是國內較出名的雲平台之一,十分適用於個人網站的開發或創業公司網站開發。

下面將介紹如果通過Python+Bottle+Sina SAE快速搭建一個網站。

1.注冊Sina SAE賬號后,創建應用,選擇Python應用。進入代碼管理,創建代碼版本,點擊編輯代碼,就能看見這樣的界面:

 

# encoding=utf8
import sae
#導入Bottle模塊
from bottle import Bottle,route, run, template, request, response,  post, get, static_file,debug
app=Bottle()
debug(True)  #打開debug功能

@app.get('/')
def web_index():
    return "Hello World"

application = sae.create_wsgi_app(app) 

將index.wsgi文件的代碼換成以上的代碼,就能實現一個最簡單的網站。現在訪問XXXX.sinaapp.com(XXXX是我們創建應用的時候輸入的二級域名),就能返回"Hello world”。
@app.get('/')
def web_index():
   
return "Hello World"
這段代碼就是一個路由,其中get是HTTP訪問方式,一般常用get和post;'/'是uri地址。

@app修飾器下面需要一個函數的定義,當用戶訪問"/"uri時,服務器就會調用這個函數,並把函數return的結果返回給用戶,函數可以返回字符串,返回網頁(template),返回文件(static_file)等。

2.return網頁

現在要制作一個返回登錄頁面的功能。

在服務器的根目錄創建一個views文件夾(文件名沒有硬性規定的),並放置一個login.html文件

login.html文件代碼為

<html>
    <body>
        <form action="/login">
            用戶名:
            <input type="text" name="username">
            密碼:
            <input type="password" name="pwd">
            <input type="submit" value="登陸">
        </form>
    </body>
</html>

在index.wsgi文件加入代碼:

 

@app.get("/login")
def web_login():
    return template("login")

 

這是訪問XXXX.sinaapp.com/login就會返回login.html這個網頁

3.return動態網頁

template功能十分強大,可以動態地在html文件中插入參數,同時,html里面可以輸入python代碼來使參數的插入更靈活。

例如我們"/logn"路由代碼改成:

@app.get("/login")
def web_login():
  myList=["print me","no print me","print me"]
  return template("login",myList=myList)

將login.html的代碼改成:

<html>
    <body>
        <form >
            用戶名:
            <input type="text" name="username">
            密碼:
            <input type="password" name="pwd">
            <input type="submit" value="登陸">
        </form>
        % for item in myList:
        %if item=="print me":
        <p>{{item}}</p>
        %end
     %end
  </body> </html>

由於html文件不能識別縮進,所以需要%end來標記一個for或if的結束。輸出結果為

網頁繼承:

% rebase('nav_base.html')

父網頁要加入

{{!base}}

 

網頁包含子網頁

% include('show_cols_include.html')

設置參數的默認值

% setdefault('cashier_id', '0')

 

4.return文件

在服務器的根目錄創建一個images文件夾

index.wsgi代碼:

@app.get("/images/:filename")
def file_images(filename):
  return static_file(filename,root='images')

這樣,客戶端就可以直接下載服務器的文件,例如網頁中的圖片,用到的CSS,JS文件等。

5.return JSON

在bottle,直接return字典類型的數據,就等於return JSON數據,如

myDict={“name”:"kevin";"age":21}
return myDict

 

6.獲取請求的參數

很多的get請求會附帶一些參數,post請求會有客戶端發來的JSON,可以用以下代碼來獲取:

name=request.params.get("name")

"name"為參數名稱

7.設置或獲取cookie

設置cookie:

response.set_cookie('username',"this is my username")

 

獲取cookie

username=request.get_cookie("username")

 

8.上傳文件

 

file_path = 'static/xls/{0}.xls'.format(str(int(time.time())))
filename=request.files.get('input_xls').filename()  #獲取文件名
request.files.get(
'input_xls').save(file_path) #保存文件

上傳文件一定要在html form 標簽加入屬性
enctype ="multipart/form-data"

構建一個自己的服務器和自己的網站就這么簡單。

暫時想到這么多,以后繼續補充~~~~

 

最后附上index.wsgi的全部代碼:

# encoding=utf8
import sae
from bottle import Bottle,route, run, template, request, response,  post, get, static_file,debug
app=Bottle()
debug(True)


@app.get('/')
def web_index():
    return "Hello World"
    
@app.get("/login")
def web_login():
  myList=["print me","no print me","print me"]
  return template("login",myList=myList)
  
@app.get("/params")
def params():
  name=request.params.get("name")
  return name  
  
@app.get("/images/:filename")
def file_images(filename):
  return static_file(filename,root='images')

application = sae.create_wsgi_app(app) 

 

 

 


免責聲明!

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



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