使用Python開發你的第一個服務器程序

聲明:本文是用Py3.6版本,而且從此以後我的系列文章優先使用Py3.6版本,爲什麼說優先使用Py3.6版本呢?因爲有的時候Py3.6版本確實有些問題,那我只能用Py2.7版本!

是這樣的,昨天在公衆號看到有朋友用Django 構建了一個簡單的Python服務器程序 用 Python 開發服務器「傻瓜式教程」,於是我當時就留言也要搞一個Flask於是就有了這篇文章. 其實Flask服務器程序我早就寫過了,只是當時可能並沒有多少人注意到.用Python打造屬於自己的搜索引擎

閒話不說,進入正題.

打開你的Pytharm,然後新建一個項目,這裏你直接選中Flask模板,一切都幫你搞定了. 我們是快速入門,所以不要折騰一些命令行以及其它虛擬環境.

然後這個項目就可以直接運行,然後你就可以看到經典的Hello world這裏使用app.route來管理接口服務的路由,下面我都對一些方法進行了說明,可以看圖

這裏我提煉出幾點

每次修改程序後,需要先停止服務,然後再重新運行服務
Flask默認的請求方式是GET
怎麼接受請求的參數

這裏可以直接通過後綴變量來傳入數據

@app.route('/search/<keyword>')
def search(keyword):
    '''尖括號裏面可以輸入變量字符串'''
    return 'hello world %s' % keyword

當然也可以通過這種方式來請求,這裏我是演示get請求方式,這裏需要先專稿Flask中的Request模塊,然後才能使用

      print('打印全部參數:%s' % request.args)
      # 獲取請求字段one,two
      one = int(request.args.get('one'))
      two = int(request.args.get('two'))
怎麼進入到指定的網頁

需要先導入Flask中的render_templete模塊,然後再使用 return render_template('index.html') 這個index.html文件在你的Templates模板文件夾中

怎麼跳轉到其它網頁,以及如何帶參數,帶幾個參數

return render_template('result.html', arg1=one, arg2=two, mode_word=mode_word, show=result) 這裏是跳轉到result.html網頁中,後面是我們傳入的參數,有幾個參數我們就可以傳入幾個參數

結果

完整代碼

from flask import Flask
from flask import request
from flask import render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    '''打開瀏覽器默認是進入這個方法'''
    # return 'Hello World!'
    return render_template('index.html')


@app.route('/index')
def index():
    '''後面追加index是進入這個方法'''
    return 'hello world index'


@app.route('/search/<keyword>')
def search(keyword):
    '''尖括號裏面可以輸入變量字符串'''
    return 'hello world %s' % keyword


@app.route('/plus')
def doPlus():
    return '666'


@app.route('/plus')
def doPlus2(name):
    return '777'


@app.route('/add')
def doAdd():
    print('打印全部參數:%s' % request.args)
    # request.args 就是全部參數的字典
    if 'one' not in request.args:
        return '沒有請求參數one'

    if 'two' not in request.args:
        return '沒有請求參數two'

    try:
        one = int(request.args.get('one'))
        two = int(request.args.get('two'))
        result = '結果是:%d' % (one + two)
        return render_template('result.html', show=result)
    except Exception as e:
        result = '錯誤信息:%s' % e.args
        return render_template('result.html', show=result)


@app.route('/mul')
def doMul():
    print('打印全部參數:%s' % request.args)
    # request.args 就是全部參數的字典
    if 'one' not in request.args:
        return '沒有請求參數one'

    if 'two' not in request.args:
        return '沒有請求參數two'

    try:
        one = int(request.args.get('one'))
        two = int(request.args.get('two'))

        result = '結果是:%d' % (one * two)
        return render_template('result.html', arg1=one, arg2=two, show=result)
    except Exception as e:
        result = '錯誤信息:%s' % e.args
        return render_template('result.html', show=result)


@app.route('/comp')
def doComp():
    print('打印全部參數:%s' % request.args)
    # request.args 就是全部參數的字典
    if 'one' not in request.args:
        return '沒有請求參數one'

    if 'two' not in request.args:
        return '沒有請求參數two'

    try:
        one = int(request.args.get('one'))
        two = int(request.args.get('two'))
        mode = request.args.get('mode')
        result = 0
        mode_word = ''
        if '+' == mode:
            result = one + two
            mode_word = '加法'
        elif '-' == mode:
            result = one + two
            mode_word = '減法'
        elif '*' == mode:
            result = one * two
            mode_word = '乘法'
        elif '/' == mode:
            result = one / two
            mode_word = '除法'
        else:
            result = '計算方式無效'
            mode_word = '計算方式無效'

        result = '結果是:%s' % str(result)
        return render_template('result.html', arg1=one, arg2=two, mode_word=mode_word, show=result)
    except Exception as e:
        result = '錯誤信息:%s' % e.args
        return render_template('result.html', show=result)


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

完整代碼見Github: https://github.com/pythonchannel/theFlask

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章