--Run Python's CGI on GMO DigiRock's rental server "Core Server"
--Price plan: Business plan "CORE-B" (during the 30-day free trial period) --Server: b1.coreserver.jp
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
$ ls -l ~/public_html/hello.cgi
-r-xr-xr-x 1 alice hpusers 414 December 12 20:08 /virtual/alice/public_html/hello.cgi
#!/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>')
$ curl http://alice.b1.coreserver.jp/hello.cgi
<html><body>
Hello, world.<br>
Hello World.<br>
</body></html>
$ ls -l ~/public_html/message.py
-r-xr-xr-x 1 alice hpusers 565 December 12 20:14 /virtual/alice/public_html/message.py
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>')
$ curl http://alice.b1.coreserver.jp/message.py?message=%E7%BD%AA%E3%81%A8%C3%97
<html><body>
message:Sin and ×
</body></html>
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
$ ls -l ~/public_html/draw.py
-r-xr-xr-x 1 alice hpusers 922 December 15 01:53 /virtual/alice/public_html/draw.py
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())
$ 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.
Recommended Posts