Run Python CGI on CORESERVER

Overview

--Run Python's CGI on GMO DigiRock's rental server "Core Server"

This environment

--Price plan: Business plan "CORE-B" (during the 30-day free trial period) --Server: b1.coreserver.jp

Settings: CGI executable file extension to cgi or py

The .cgi file can be executed as CGI from the beginning, so you can put it with execute permission.

$ ls -l ~/public_html/hello.cgi
-r-xr-xr-x 1 alice hpusers 414 December 12 20:08 /virtual/alice/public_html/hello.cgi

If you want the extension to be py, the .py file is not designed to run as CGI, so put an .htaccess file that describes the settings to run as CGI.

$ ls -l ~/public_html/.htaccess 
-rw-r--r--1 alice hpusers 27 December 12 18:09 /virtual/alice/public_html/.htaccess

The contents of .htaccess are described as follows.

.htaccess


AddHandler cgi-script .py

Hello World display sample (hello.cgi)

Source code location

$ ls -l ~/public_html/hello.cgi
-r-xr-xr-x 1 alice hpusers 414 December 12 20:08 /virtual/alice/public_html/hello.cgi

Source code

#!/usr/local/bin/python3

import io
import sys

#Specify the character encoding of the output stream
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

#HTTP header
print('Content-Type: text/html; charset=utf-8')
print() # blank line, end of headers

#HTML body
print('<html><body>')
print('Hello, world.<br>')
print('Hello World.<br>')
print('</body></html>')

Access result

$ curl http://alice.b1.coreserver.jp/hello.cgi
<html><body>
Hello, world.<br>
Hello World.<br>
</body></html>

Input value display sample (message.py)

Source code location

$ ls -l ~/public_html/message.py 
-r-xr-xr-x 1 alice hpusers 565 December 12 20:14 /virtual/alice/public_html/message.py

Source code

message.py


#!/usr/local/bin/python3

import cgi
import html
import io
import sys

#Specify the character encoding of the output stream
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

#Get the value of the message parameter
form = cgi.FieldStorage()
message = form.getfirst('message', 'Hello, world.')

#HTTP header
print('Content-Type: text/html; charset=utf-8')
print() # blank line, end of headers

#HTML body
print('<html><body>')
print(f'message: {html.escape(message)}') #HTML escaped and output
print('</body></html>')

Access result

$ curl http://alice.b1.coreserver.jp/message.py?message=%E7%BD%AA%E3%81%A8%C3%97
<html><body>
message:Sin and ×
</body></html>

External library usage sample (draw.py)

Install package

This time install the NumPy and Matplotlib packages in your ~ / my-space directory.

$ python3 -m pip install numpy matplotlib --target ~/my-space

The package is placed in the ~ / my-space directory.

$ ls ~/my-space
__pycache__                                 numpy
bin                                         numpy-1.17.4.dist-info
cycler-0.10.0.dist-info                     pkg_resources
cycler.py                                   pylab.py
dateutil                                    pyparsing-2.4.5.dist-info
easy_install.py                             pyparsing.py
kiwisolver-1.1.0.dist-info                  python_dateutil-2.8.1.dist-info
kiwisolver.cpython-36m-x86_64-linux-gnu.so  setuptools
matplotlib                                  setuptools-42.0.2.dist-info
matplotlib-3.1.2-py3.6-nspkg.pth            six-1.13.0.dist-info
matplotlib-3.1.2.dist-info                  six.py
mpl_toolkits

Source code location

$ ls -l ~/public_html/draw.py 
-r-xr-xr-x 1 alice hpusers 922 December 15 01:53 /virtual/alice/public_html/draw.py

Source code

draw.py


#!/usr/local/bin/python3

import io
import sys

#Add library path to module search path
sys.path.append('../my-space')

#Load NumPy and Matplotlib
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

#Data creation
x = np.arange(0, 6, 0.1) #0 to 6 0.Generate in 1 increments
y1 = np.sin(x)
y2 = np.cos(x)

#Drawing a graph
plt.figure(figsize=(4, 3), dpi=160) #Image size
plt.plot(x, y1, label='sin')
plt.plot(x, y2, linestyle = '--', label='cos') #Draw with dashed line
plt.xlabel('x') #x-axis label
plt.ylabel('y') #y-axis label
plt.title('sin & cos') #title
plt.legend() #Usage Guide

#Generate PNG image byte sequence
image = io.BytesIO()
plt.savefig(image, format='png')
image.seek(0)

#HTTP header
sys.stdout.buffer.write(b'Content-Type: image/png\n\n')

#Output image binary data
sys.stdout.buffer.write(image.read())

Access result

$ curl --dump-header - -s http://alice.b1.coreserver.jp/draw.py --output a.png
HTTP/1.1 200 OK
Date: Sat, 14 Dec 2019 17:05:05 GMT
Server: Apache
Vary: User-Agent
Transfer-Encoding: chunked
Content-Type: image/png

Saved a.png file.

a.png

Reference material

Recommended Posts

Run Python CGI on CORESERVER
Run Openpose on Python (Windows)
Run Tensorflow 2.x on Python 3.7
Run unix command on python
Run CGI written in python on Sakura's rental server
Run Python on Schedule on AWS Lambda
Run TensorFlow Docker Image on Python3
Python CGI file created on Windows
Periodically run Python on Heroku Scheduler
Run servo with Python on ESP32 (Windows)
Python on Windows
twitter on python3
[Python] Run Flask on Google App Engine
Python CGI basics
Build CGI Server running on Python 3 on Docker
Run AzureKinect in Python on Christmas Eve.
python on mac
Run servomotor on Raspberry Pi 3 using python
[Python] Run Headless Chrome on AWS Lambda
Run Python code on A2019 Community Edition
Python on Windbg
Run Python in C ++ on Visual Studio 2017
Run python wsgi server on NGINX Unit
Periodically run a python program on AWS Lambda
Install and run Python3.5 + NumPy + SciPy on Windows 10
Put MicroPython on Windows to run ESP32 on Python
Run Python YOLOv3 in C ++ on Visual Studio 2017
How to run MeCab on Ubuntu 18.04 LTS Python
Run Zookeeper x python (kazoo) on Mac OS X
Run pip install on MacOS Python 3.7 or later
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
TensorFlow: Run data learned in Python on Android
Run Python on Apache to view InfluxDB data
Python conda on cygwin
Install python on WSL
Run Python with VBA
PyOpenGL setup on Python 3
Install Python on Pidora.
Run prepDE.py with python3
Install Scrapy on python3
Run Django on PythonAnywhere
Install Python on Mac
Run python from excel
Run mysqlclient on Lambda
Run Blender with python
Installing pandas on python2.6
python basic on windows ②
Install python on windows
Install Python 2.7.3 on CentOS 5.4
Run OpenMVG on Mac
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Cloud Run tutorial (python)
Install Python 3.6 on Docker
Run iperf with python
Install Apache 2.4 on Ubuntu 19.10 Eoan Ermine and run CGI
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
Set-enable Python virtualenv on Windows
Set up Python 3.4 on Ubuntu
Run Jupyter on Ubuntu on Windows