[PYTHON] The easiest way to set up Last-Modified in Flask

Sample code that returns json with flask --uokada's diary

I opened it for a while from the above entry, but I found an easy way when I looked at Stack Overflow, so I will introduce it. http - RFC 1123 Date Representation in Python? - Stack Overflow

from wsgiref.handlers import format_date_time

This format_date_time function is the point. If you pass a time stamp to this, it will be formatted and output in RFC1123 format.

The sample code looks like this.

#!/usr/bin/env  python2.7
from flask import Flask, jsonify, after_this_request
from wsgiref.handlers import format_date_time
from datetime import datetime
from time import mktime


app = Flask(__name__)

@app.route('/name/<name>.json')
def hello_world(name):
    greet = "Hello %s from flask!" % name
    result = {
        "Result":{
        "Greeting": greet
        }
    }
    @after_this_request
    def d_header(response):
        """ add header

        Arguments:
        - `response`:
        """
        now = datetime.now()
        stamp = mktime(now.timetuple())
        response.headers['Last-Modified'] = \
            format_date_time(stamp)
        return response
    return jsonify(ResultSet=result)

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

Launch the script and check the headers returned by the application.

% curl -I localhost:5000/name/uokada.json
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 91
Last-Modified: Thu, 28 Feb 2013 17:47:20 GMT
Server: Werkzeug/0.8.3 Python/2.7.1
Date: Thu, 28 Feb 2013 17:47:20 GMT

Last-Modified is properly output in RFC 1123 format.

I didn't notice this function at all because it wasn't listed in the Japanese translation. 20.4. wsgiref — WSGI Utility and Reference Implementation — Python 2.7ja1 documentation

Recommended Posts

The easiest way to set up Last-Modified in Flask
The easiest way to make Flask
The easiest way to try PyQtGraph
The easiest way to get started in Slack socket mode (Go)
The easiest way to synthesize speech with python
The easiest way to use OpenCV with python
How to set up public key authentication in ssh
Set the form DateField to type = date in Django
The easiest line bot in the world to lose weight
Set up Nunjucks in Node.js
Define a task to set the fabric env in YAML
How to import Python library set up in EFS to Lambda
Probably the easiest way to create a pdf with Python3
How to set the html class attribute in Django's forms.py
An easy way to hit the Amazon Product API in Python
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
How to set the output resolution for each keyframe in Blender
Set opset to embed in ONNX
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
Cython to try in the shortest
The fastest way to try EfficientNet
How to set up the development environment of ev3dev [Windows version]
Programming to fight in the world ~ 5-2
The easiest way to get Chainer v1.5 + CUDA + cuDNN on Windows
How to display bytes in the same way in Java and Python
What is Jenkins? ] Installation-how to use-automatic test / automatic deployment will be explained in the world's easiest way
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I will teach you the Twitter tweet bot in the easiest way in Japan.
What is the fastest way to create a reverse dictionary in python?
Set the time zone to Japan Standard Time
How to set up SVM using Optuna
How to set the server time to Japanese time
To set default encoding to utf-8 in python
Associate the table set in python models.py
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
In Jupyter, add IPerl to the kernel.
Set up Pipenv in Pycharm in Windows environment
Various comments to write in the program
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
[Django memo] I want to set the login user information in the form in advance
[Django version up] The way to convert to string when saving TextFiled has changed
I want to set a life cycle in the task definition of ECS
I'm addicted to the difference in how Flask and Django receive JSON data
Create a filter to get an Access Token in the Graph API (Flask)
Explaining how to make LINE BOT in the world's easiest way (2) [Preparing a bot application in a local environment with Django in Python]
Set up a simple HTTPS server in Python 3
The fastest way for beginners to master Python
Mathematics memorandum to keep up with the field # 4
Try to separate Controllers using Blueprint in Flask
How to set up Random forest using Optuna
Set the range of active strips to the preview range
How to set browser location in Headless Chrome
Twitter streaming client to enjoy in the terminal
To dynamically replace the next method in python
Set up a test SMTP server in Python.
Draw graphs in Julia ... Leave the graphs to Python
A clever way to time processing in Python
Set up a UDP server in C language