Simple code to call a python program from Javascript on EC2

Press the button to launch the python program

Create the following html. Prepare a python program that returns the entered value when you enter it.

image.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <h1>test</h1>
  <p>Please enter the characters</p>
  <input id="plane" type="text" value="">
  <input id="btn" type="button" value="input">
  <p id="string"></p>
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script>
    $(function(){
      $("#btn").click(
        function() {
          var plane = document.getElementById("plane").value;
          $.ajax({
            url: "./test.py",
            type: "post",
            data: {data: plane},
            success: function(data){
              document.getElementById("string").innerText = data;
            },
            error: function(data){
              console.log("failed");
            }
          });
        }
      );
    });
  </script>
</body>
</html>
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cgi

storage = cgi.FieldStorage()
data = storage.getvalue('data')
print('Status: 200 OK')
print('Content-type: text/html\n')

print('The entered characters are' + data + 'is.')

httpd settings

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ sudo -i
[root@ip-xxx-xxx-xxx-xxx ~]# yum install python3
[root@ip-xxx-xxx-xxx-xxx ~]# yum install httpd
[root@ip-xxx-xxx-xxx-xxx ~]# systemctl start httpd
[root@ip-xxx-xxx-xxx-xxx ~]# cd /var/www/html/
[root@ip-xxx-xxx-xxx-xxx html]# vi index.html
[root@ip-xxx-xxx-xxx-xxx html]# vi test.py
[root@ip-xxx-xxx-xxx-xxx html]# chmod 775 test.py
[root@ip-xxx-xxx-xxx-xxx html]# vi /etc/httpd/conf/httpd.conf
Add ExecCGI at the end of line around line 144 Options Indexes FollowSymLinks ExecCGI
Add a line around line 157 AddHandler cgi-script .cgi .py
At the end of line around line 306.html .Add htm AddType text/html .shtml .html .htm
At the end of line around line 307.html .Add htm AddOutputFilter INCLUDES.shtml .html .htm
[root@ip-xxx-xxx-xxx-xxx html]# systemctl restart httpd

Execution result

image.png

Recommended Posts

Simple code to call a python program from Javascript on EC2
From buying a computer to running a program with python
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
How to run a Python program from within a shell script
Call Matlab from Python to optimize
Call a Python function from p5.js.
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
Things to note when running Python on EC2 from AWS Lambda
Call dlm from python to run a time-varying coefficient regression model
Everything from building a Python environment to running it on Windows
Send a message from Python to Slack
Connecting from python to MySQL on CentOS 6.4
[Python] I tried to make a simple program that works on the command line using argparse.
Run Python code on A2019 Community Edition
Call a command from Python (Windows version)
A program to elect a president based on prestigious democracy from ATND participants
From re-environment construction of Python to graph drawing (on visual studio code)
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
Periodically run a python program on AWS Lambda
How to open a web browser from python
[2020 version] How to install Python3 on AWS EC2
To execute a Python enumerate function in JavaScript
How to generate a Python object from JSON
A simple Pub / Sub program note in Python
Call a Python script from Embedded Python in C ++ / C ++
I want to make C ++ code from Python code!
An easy way to call Java from Python
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Think about how to program Python on the iPad
Write a super simple molecular dynamics program in python
Changes from Python 2 to Python 3.0
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Python Ver. To introduce WebPay with a little code.
Steps to attach and debug from VS Code to Jupyter Lab on a remote server
Receive dictionary data from a Python program in AppleScript
Machine learning python code summary (updated from time to time)
Rewrite Python2 code to Python3 (2to3)
Preferences to generate animated GIFs from Python on Mac
From python to running instance on google cloud platform
How to build a Django (python) environment on docker
Steps from installing Python 3 to creating a Django app
How to make a Python package using VS Code
A program to write Lattice Hinge with Rhinoceros with Python
Consider a conversion from a Python recursive function to a non-recursive function
A note on speeding up Python code with Numba
Write code to Unit Test a Python web app
Python script to create a JSON file from a CSV file
How to build a Python environment on amazon linux 2
A mechanism to call a Ruby method from Python that can be done in 200 lines
I wrote Python code to create a table (view) dependency diagram (PlantUML) from SQL
An introduction to machine learning from a simple perceptron
Good and bad code to compare on a minimap
Summary of points to keep in mind when writing a program that runs on Python 2.5
I made a program to collect images in tweets that I liked on twitter with Python
Let's create a program that automatically registers ID/PW from CSV to Bitwarden with Python + Selenium
Call C language functions from Python to exchange multidimensional arrays