python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page
  • Configuration
  • 靜態檔案
  • 轉址 Redirect

Was this helpful?

  1. Web Development
  2. Flask

RESTful api

PreviousFlaskNextTemplate

Last updated 5 years ago

Was this helpful?

參考教學

動態網址

取得ip連線者ip

from flask import request
from flask import jsonify

@app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
    return jsonify({'ip': request.remote_addr}), 200

print東西到cmd上

print('This is error output', file=sys.stderr)

Configuration

可將重要參數加入至app.config

app.config['SECRET_KEY'] = 'some secret words'

靜態檔案

比起django方便快速許多

# 靜態檔案的url 直接用於html中
url_for('static', filename='css/styles.css', _external=True)

轉址 Redirect

#encoding: utf-8

from flask import Flask, redirect, url_for

app = Flask(__name__)


@app.route('/')
def index():
    login_url = url_for('login')  # login欄位可輸入其他function名稱,會回傳對應的url(/login/)
    return redirect(login_url)
    return u'这是首页'

@app.route('/login/')
def login():
    return  u'这是登陆页面'

@app.route('/question/<is_login>/')
def question(is_login):
    if is_login == '1':
        return  u'这是发布问答的页面'
    else:
        return  redirect(url_for('login'))


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

參考連結:

參考資料:

http://www.pythondoc.com/flask-restful/first.html
http://www.pythondoc.com/flask-mega-tutorial/index.html
https://theblackcat102.wordpress.com/2016/06/30/用flask建立restful-api/
http://blog.csdn.net/m0_38124502/article/details/78680368
https://segmentfault.com/a/1190000002480266
https://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-flask-for-python
https://stackoverflow.com/questions/44405708/flask-doesnt-print-to-console/44410730
https://blog.csdn.net/lanchunhui/article/details/51583377
https://blog.csdn.net/dengfan666/article/details/78320188
https://www.letiantian.me/jiaocheng/python-flask/入门/使用redirect重定向网址.html