後端

Yu-Chi Lai

之後的課程安排

沒有特定主題,我也快沒心思安排教什麼特別有料的了 QQ

會是由網路知識(理論)+實作(Flask、JS、LineBot?)而組成

INTRO

入門

  • 提供給客戶端 GUI 式的資料
  • HTML/Javascript/CSS

前端

  • 儲存、運算、深層的互動
  • 需要伺服器、資料庫

後端

  • 一種程式語言開發的模式,目的是有助於減輕網頁開發時,共通性活動的工作負荷,並提升代碼的可複用性
  • 之前教的 Flask、Hexo 都算

什麼是框架

常見語言/框架

Text

  • 熱門 (市佔率 77.6%)

  • 易學易懂、學習曲線低

  • 中小企業常用

  • 語法嚴謹度低、複雜程序執行的頗爛

PHP

  • PHP 最熱門的框架
  • MVC 架構
  • 架構漂亮、社群活躍

Laravel

  • 優雅、像白話文
  • 強型別
  • 任何語法皆屬於物件
  • 執行速率偏差
  • 99% 搭配 Rails 這個框架進行開發

RUBY/Rails

  • 我想 Python 應該不用我介紹了
  • Django 採用 MTV 架構,通常使用於大型的專案
  • Flask 較為輕量化,適合製作結構、機制簡單的網站

Python-

Django & Flask

  • 能在後端執行 Javascript
  • 執行效能優良
  • 跨平台開發與執行性佳
  • Express 是 Node.js 唯一認證 (? 的 Web 開發框架,將許多後端的機制包裝為模組的形式,知識門檻降低許多 (?。

Node.js

Warm up

暖身

Warm up

名詞

網站為了辨別使用者身分而儲存在用戶端(Client Side)上的資料,使用Cookie能夠讓使用者在網路瀏覽上更加方便,但在網路隱私的方面來說Cookie危害了使用者的安全。

名詞介紹 – Cookie

Server 和 Client 不會一直保持連線狀態,無法取得雙方的狀態,所以使用了 Session,Session 就像是餐廳的號碼牌, Server 可以透過該號碼牌來認定 Client 的身分、是否點過餐、知道 Client 點了什麼東西,給予 Client 餐點。

名詞介紹 – Session

GET 和 POST 是兩種常見的 Http method,差別如下:

  • GET- 從指定的資源請求數據,將要傳送的資料以Query String(一種Key/Vaule的編碼方式)加在 url 後方
  • POST- 向指定的資源提交要被處的數據,可以把表單資料放在 message-body 中運送

名詞介紹 – GET & POST

Uniform Resource Locator (統一資源定位器)

以 github.com/login 為例:

名詞介紹 – URL

https github.com login
協定 伺服器 路徑

GET 和 POST 是兩種常見的 Http method,差別如下:

  • GET- 從指定的資源請求數據,將要傳送的資料以Query String(一種Key/Vaule的編碼方式)加在 url 後方
  • POST- 向指定的資源提交要被處的數據,可以把表單資料放在 message-body 中運送

名詞介紹 – GET & POST

Warm up

比較進階 (? 的 Python Python 補救班

引入模組

Python 擁有許多強大的模組,像是 numpy、pandas、os,而我們可以透過 import 的方式運用其中的函式庫。

運用

import [module] (as [name]) 引入模組 (取名作)
from [module] import [variable/function] 從模組引入特定的函式
pip install [module] 安裝套件

Python-Decorator

def printHello(func):
    def wrapper():
        print('Hello')
        return func()
    return wrapper

@printHello
def printWorld():
    print('World')

printWorld()
printHello(printWorld())

Decorator- with arguments

def printHello(func):
    def wrapper(arg):
        print('Hello')
        return func(arg)
    return wrapper

@printHello
def printArg(arg):
    print(arg)

printArg('INFOR 35th')
printArg('7122')

Decorator- more args

def printHello(func):
    def wrapper(*args, **kwargs):
        print('Hello')
        return func(*args, **kwargs)
    return wrapper

@printHello
def printSingle(arg):
    print(arg)

@printHello
def printDouble(arg1, arg2):
    print(arg1)
    print(arg2)

printSingle('World')
printDouble('Koyori', 'cute')

*args 為一 list,指的是不論個數的輸入參數,**kwargs 為一 dict,指的是任何名字的參數

Setting up virtual environment

  • Anaconda
  • Docker
  • Pipenv
  • 基本上都學是件不錯的事情,但今天用 pipenv
pip install pipenv

WARMup

html 補完

Form

  • action 屬性裡面要放的是"資料傳送的目的地"
  • method 屬性裡面放的是資料的 傳遞方式 ,分為 get 和 post
<form action="資料傳送到哪裡" method="get/post">
    表單內容
</form>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
  <form>
    <input name="password" value="請輸入密碼" type="input">
  </form>
</body>
</html>

web dev

一些與網路相關的知識

Flask

just something easy

CONFIGure settings

DEBUG: 能幫助開發時簡化調整及重啟的過程,使開發效率提升 (? 

from flask import Flask
app = Flask(__name__)
app.config.from_pyfile('configs.py')


@app.route("/")
def hello():
    return "Hello World!"


if __name__ == "__main__":
    app.run()
    # app.debug=True 也行
# configs.py
DEBUG = TRUE

ERROR handling

from flask import Flask, render_template

app = Flask(__name__)
app.config["DEBUG"] = True


@app.route("/")
def index():
    return render_template('index.html')  # index page


@app.errorhandler(404) # errorhandler 中放置 status code
def page_not_found(error):
    return 'This page does not exist QwQ', 404  # 錯誤回傳


if __name__ == "__main__":
    app.run()

GET & POST

from flask import Flask, request, render_template

app = Flask(__name__)
app.config["DEBUG"] = True


@app.route('/')
def login():
    return render_template('login.html')


@app.route("/hello", methods=['GET', 'POST'])
def hello():
    if request.method == 'GET':
        return 'Hello' + request.args['username']


if __name__ == '__main__':
    app.run()
# login.html
<form method="get" action={{ url_for('user') }}>
    <input type="text" name="username">
    <input type="submit" value="input">
</form>

request 取得特定元素

  • form: post only
  • args: get only
  • values: get / post

 

使用 get 和使用 post

網址列會出現什麼差異?

COOKIE

from flask import Flask, make_response
app = Flask(__name__)
app.config.from_pyfile('configs.py')

@app.route('/cookie')
def set_cookie():
    res = make_response('Yummy cookies')
    res.set_cookie(key='flavor',value='chocolate')
    return res

@app.route('/get')
def get():
    get_cookie = request.cookies.get('flavor')
    return get_cookie

@app.route('/del')
def de():
    res = make_response('Cookies vanished')
    res.set_cookie(key='flavor',value='',expires=0)
    return res

if __name__ == "__main__":
    app.run()

創建/取得/刪除 特定 cookies

SESSION

from flask import Flask, render_template, session
import os
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = os.urandom(16)

@app.route('/session') # 建立session
def setsession():
    session['mine'] = 'Richard'
    return "My sessions"

@app.route("/get")  #取得session
def getsession():
    get_session = session.get('mine')
    return get_session

@app.route('/del') #刪除session
def delsession():
    del session['mine']
    return 'Session deleted'

if __name__ == '__main__':
    app.run()

os.urandom: 隨機生成一長度為 n bytes 的字串

SQL

MySQL

資料庫又稱為資料管理系統,可以視為電子化的檔案櫃,使用者可以對檔案中的資料執行: 新增、擷取、更新、刪除等等的操作。可以分為關聯式和非關聯式:

 

  • 關聯式: 是建立在關聯模型基礎上的資料庫,資料庫是由多個資料表所組成,可以將資料表關聯起來連結之間的關係。
  • 非關聯式: 資料存放區都未使用關聯式模式,它們傾向於對其支援的資料類型及資料的查詢方式提供更具體的方法。

資料庫是什麼?

Backend Training

By Expect Lai

Backend Training

Here're notes about backend development.

  • 86