Create REST API that returns the current time with Python3 + Falcon

Motivation

To provide services using machine learning with REST API First, let's implement the REST API in Python.

Falcon Python web framework. It's a minor, but it's lightweight and fast.

Building a speed of light Web API server with Falcon Web API with Python + Falcon

environment

Python 3.5.2 Falcon 1.1.0 Cython 0.24.1 (Python acceleration)

You can install it with pip.

pip install falcon
pip install cython

Source code

timeapi.py


import json
import falcon
from datetime import datetime

#Create a class and describe the process
class TimeResource(object):
	#GET method
	def on_get(self, req, resp):

		#Write a message
		dt=datetime.now() #Times of Day
		msg = {
			"hour": dt.hour,
			"minute": dt.minute,
			"second": dt.second
		}

		#Return message in json format
		resp.body = json.dumps(msg)

#app instance creation
app = falcon.API()
#Connect endpoints and classes
app.add_route("/", TimeResource())

if __name__ == "__main__":
	#Start the server
	from wsgiref import simple_server
	httpd = simple_server.make_server("", 8000, app)
	httpd.serve_forever()

Run

http://localhost:8000

When you access

{'second': 53, 'hour': 12, 'minute': 1}

The result is displayed as follows.

Recommended Posts

Create REST API that returns the current time with Python3 + Falcon
Create an API server quickly with Python + Falcon
Web API with Python + Falcon
Call the API with python3.
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
The first API to make with python Djnago REST framework
Tornado-Let's create a Web API that easily returns JSON with JSON
Create Awaitable with Python / C API
Hit the Etherpad-lite API with Python
[LINE Messaging API] Create a BOT that connects with someone with Python
Create a REST API to operate dynamodb with the Django REST Framework
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
[Python] Quickly create an API with Flask
Challenge to create time axis list report with Toggl API and Python
Get and convert the current time in the system local timezone with python
Create test data like that with Python (Part 1)
Create an app that guesses students with python
Use logger with Python for the time being
Create a page that loads infinitely with python
Create an app that works well with people's reports using the COTOHA API
Extract "current date only" and "current date and time" with python datetime.
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
Create a Todo app with the Django REST framework
PHP and Python samples that hit the ChatWork API
Create a tweet heatmap with the Google Maps API
A memo that I touched the Datastore with python
Try to create a waveform (audio spectrum) that moves according to the sound with python
Note calling the CUDA Driver API with Python ctypes
python get current time
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client
[LINE Messaging API] Create parrot return BOT with Python
Create a bot that only returns the result of morphological analysis with MeCab on Discord
Creating an API that returns negative-positive inference results using BERT in the Django REST framework
A script that makes it easy to create rich menus with the LINE Messaging API
I made an API with Docker that returns the predicted value of the machine learning model
Try hitting the Twitter API quickly and easily with Python
Let's create a script that registers with Ideone.com in Python.
Probably the easiest way to create a pdf with Python3
Creating a Python script that supports the e-Stat API (ver.2)
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Get the current date and time in Python, considering the time difference
The story that Python stopped working with VS Code (Windows 10)
Create a web API that can deliver images with Django
Create API with Python, lambda, API Gateway quickly using AWS SAM
Treat the Interface class like that with Python type annotations
Use Trello API with python
Create an API with Django
Use Twitter API with Python
Create 3d gif with python3
Execution time measurement with Python With
Play RocketChat with API / Python
Use subsonic API with python3
Create a directory with python
Time synchronization (Windows) with Python
How to deal with the problem that the current directory moves when Python is executed from Atom
Operate Jupyter with REST API to extract and save Python code
Create an API that returns data from a model using turicreate
Create an alias for Route53 to CloudFront with the AWS API
A story about adding a REST API to a daemon made with Python