# RESTful api

參考教學

<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>

取得ip連線者ip

```python
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
```

<https://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-flask-for-python>

print東西到cmd上

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

<https://stackoverflow.com/questions/44405708/flask-doesnt-print-to-console/44410730>

## Configuration

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

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

## 靜態檔案

比起django方便快速許多

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

參考連結：\
<https://blog.csdn.net/lanchunhui/article/details/51583377>

## 轉址 Redirect

```python
#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)
```

參考資料：\
<https://blog.csdn.net/dengfan666/article/details/78320188>\
<https://www.letiantian.me/jiaocheng/python-flask/入门/使用redirect重定向网址.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stb11816.gitbook.io/python_note/web-framework/flask/api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
