[SAP CP] Web API created with python in CF environment

Introduction

This article was written as an article for December 15th of SAP Advent Calendar 2020.

This time, I would like to create a WebAPI with Phthon and run it on a CF environment. I was able to create a WebAPI in about 3 days of python history.

STEP 1: Let's make WebAPI with Python

Prepare to install the module used in this program.

First, prepare "requirements.txt" for batch installation. Use this file to manage Python packages across different environments You can install it if you want.

requirements.txt


Flask==0.12.2
flask-cors

After saving requirements.txt, use python's package management software Install the required modules.

$ pip install -r requirements.txt

Specifies the Python version to use in the CF environment.

runtime.txt


python-3.6.12

Create a deployment definition file for the CF environment. For , specify the application name when deploying to the CF environment.

manifest.yml


applications:
- name: <app name>
  path: .
  memory: 256M
  command: python server.py

I would like to write a WebAPI in Python. The code is shown below.

server.py


import os
import json
import pickle
from flask import Flask, request, abort
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

port = int(os.environ.get('PORT', 3000))


@app.route('/')
def hello():
    return "Hello World"


@app.errorhandler(404)
def error_not_found(error):
    result = "{\"message\": \"pickle file not found\"}"
    return result, 404


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=port)

STEP 2: Deploy to CF environment

Deploy the created program to the CF environment. Here, I will upload it to the trial environment.

Specify the execution environment and log in.

$ cf api <API endpoint>

The specified here can be confirmed from SAP Cloud Platform Cockpit.

image.png

If the connection is successful, you will be authenticated with your email and password. Log in using the email and password you registered.

image.png

Specify the organization to connect to. image.png

The specified here can also be confirmed from the SAP Cloud Platform Cockpit.

Now that you have specified the CF environment to deploy to, deploy to the CF environment.

cf push <app name>

STEP 3: Execution

Successful deployment adds the application to SAP Cloud Platform Cockpit The Status is displayed as RUNNING.

image.png

Now let's execute the created API. Click the URL listed in Application Routes.

This time, I made it so that it works when I access'/'. If successful, the following screen will be displayed.

image.png

Finally

I'm using Python3.6.12 this time, but the version of PHP available in the CF environment seems to vary. When I made a prototype in the past, I was using Python3.5.10. For this post, I tried to deploy the same program to the CF environment, but Python 3.5.10 was no longer available. You may not want to use a very old version of Python.

Reference URL

https://docs.cloudfoundry.org/buildpacks/python/index.html

Recommended Posts

[SAP CP] Web API created with python in CF environment
Web API with Python + Falcon
Try running python in a Django environment created with pipenv
Hit the web API in Python
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Introduced sip-4.14 in python3.2.2 environment with MacOS 10.7.4
Create a virtual environment with conda in Python
Explosive speed with Python (Bottle)! Web API development
Work in a virtual environment with Python virtualenv.
Use Python in Anaconda environment with VS Code
Evernote API in Python
virtual environment in python
Python environment with docker-compose
C API in Python 3
Development environment in Python
Virtual environment with Python 3.6
Periodically execute python script with cron in venv environment
Japanese can be used with Python in Docker environment
Playing with a user-local artificial intelligence API in Python
Web application made with Python3.4 + Django (Part.1 Environment construction)
OpenVINO using Inference Engine Python API in PC environment
Hit Mastodon's API in Python
Sample data created with python
Web scraping with python + JupyterLab
Display "Hello World" created in the local environment on the web
Working with LibreOffice in Python
Web scraping notes in python3
Install Python environment with Anaconda
Scraping with chromedriver in python
Manage python environment with virtualenv
EXE Web API by Python
Use Twitter API with Python
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Handle environment variables in Python
Working with sounds in Python
Scraping with Selenium in Python
Reflect the virtual environment created with Miniconda in Jupyter notebook
Build python3 environment with ubuntu 16.04
Video cannot be loaded with Spyder in Python development environment
Prepare python3 environment with Docker
Scraping with Tor in Python
Build python environment with direnv
Combined with permutations in Python
Play RocketChat with API / Python
Blender Python API in Houdini (Python 3)
Call the API with python3.
Web application with Python + Flask ② ③
Use subsonic API with python3
Web scraping beginner with python
[CLPEX memo] Run DO Python API in COS installed environment
Install CaboCha in Ubuntu environment and call it with Python.
Issue reverse geocoding in Japanese with Python Google Maps API
Streamline web search with python
Web application with Python + Flask ④
[Python / Django] Create a web API that responds in JSON format
Cloud DevOps Cookbook Part 4-Explore DevOps DirectMail in Python with REST API
Get US stock price from Python with Web API with Raspberry Pi
Install pip in Serverless Framework and AWS Lambda with Python environment
Jupyter Notebook 6.0.2 cannot be installed in the Python 2.7 environment created in Anaconda
Firebase Authentication token issuance in Python and token verification with Fast API
Get started with Python! ~ ① Environment construction ~