2017-03-28 29 views
1

在下面的代碼中,我想將URL存儲在一個變量中,以檢查發生哪個URL錯誤的錯誤。如何獲得燒瓶中的當前基礎URI?

@app.route('/flights', methods=['GET']) 
def get_flight(): 
    flight_data= mongo.db.flight_details 
    info = [] 
    for index in flight_data.find(): 
     info.append({'flight_name': index['flight_name'], 'flight_no': index['flight_no'], 'total_seat': index['total_seat'] }) 
    if request.headers['Accept'] == 'application/xml': 
     template = render_template('data.xml', info=info) 
     xml_response = make_response(template) 
     xml_response.headers['Accept'] = 'application/xml' 
     logger.info('sucessful got data') 
     return xml_response 
    elif request.headers['Accept'] == 'application/json': 
     logger.info('sucessful got data') 
     return jsonify(info) 

輸出: **

* Restarting with stat 
* Debugger is active! 
* Debugger PIN: 165-678-508 
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 
127.0.0.1 - - [28/Mar/2017 10:44:53] "GET /flights HTTP/1.1" 200 - 

**

我想這個消息"127.0.0.1 - - [28/Mar/2017 10:44:53] "GET /flights HTTP/1.1" 200 -"應存放在一個變量或我怎樣才能得到正在執行當前的URL。 謝謝

回答

4

您可以使用base_url方法的瓶子的request函數。

from flask import Flask, request 

app = Flask(__name__) 


@app.route('/foo') 
def index(): 
    return request.base_url 


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

這將返回以下,如果應用程序的路線是/foo

http://localhost:5000/foo 
+0

127.0.0.1 - - [28/MAR/2017十時44分53秒] 「GET /航班HTTP/1.1」 200 -...如果想要返回整個事物,那我該怎麼辦? –

+0

@RaviBhushan我不知道任何方法來捕獲瓶服務器的標準輸出。 –

+0

好的.... thanx回答 –