I want to pass an argument to a python function and execute it from PHP on a web server

Introduction

As you can see from the title, it is a request that is too niche, but I got caught in various errors on the way, so I have summarized it for myself in the middle.

Execute python function in PHP

Command execution function

You can use the exec command to execute commands in PHP as you would on the command line, not just python functions. (See https://www.php.net/manual/ja/function.exec.php)

exec($command, $output, $result_code);

Pass the command you want to execute to \ $ command. \ $ output and \ $ result_code are optional, but \ $ output stores the command execution result as an array, and \ $ result_code stores the code indicating whether the command execution was successful.

For example

exec('ls', $output, $result_code);

When you execute, the command "ls" that displays the files and directories in the current directory is executed, and the result is stored in \ $ output. When successful, \ $ result_code seems to be 0. (* Confirmation required)

python execution command (passing arguments)

Consider the following python file hello.py.

hello.py


def hello(name):
    print(name)

Just take the arguments and print.

To do this from the command line:

# python -c 'import hello; hello.hello("Alice")'

If you give this command as \ $ command and the above-mentioned exec command argument, you can execute the python function from PHP. However, to execute the function this way, you need to run the command in the same directory as hello.py.

Allows execution from another directory

I'm not familiar with python, so here's a random and successful method.

-Using $ PYTHONPATH

Recognition of PATH to refer to when executing python.

# export PYTHONPATH=<The path you want to specify>:${PATH}

By doing so, you will be able to refer to the path you want to specify. This time it's a good idea to specify the directory that contains hello.py. That way, you can run the hello function with the python -c command from another directory that doesn't contain hello.py.

Run on a web server

I think that you often operate a web server when you are using PHP. There is a difference between simply executing the same command from the command line on the server as described above and executing the same command from the web server. (See https://sierra-kilo.hatenablog.jp/entry/2017/08/11/233708)

It seems that the path specified in $ PYTHONPATH is not inherited when executing from the Web server. In the reference article, I am dealing with it by rewriting the apache configuration file, but this time I will change the setting in/etc/sysconfig/httpd. (See http://yukke.blog3.fc2.com/blog-entry-68.html)

In this reference article

export ORACLE_HOME=/oracle/home/

By doing so, the environment variable ORACLE_HOME is added, but the environment variable I want to add this time is PYTHONPATH. Also, for some reason export didn't work, so I did the following.

PYTHONPATH=<The path you want to specify>:${PATH}

After rewriting up to this point, restart apache and check. Php.info () is useful for checking environment variables. (Details of how to use are omitted)

If PYTHONPATH is added to the environment variable, you will be able to refer to the specified path from the web server, so you will be able to execute python functions.

Summary

This time, there were some points that got stuck for one purpose, so I summarized them all at once, but I think that they could be used independently again.

To summarize briefly

--How to use the exec function --How to use the python -c command --Specify PYTHONPATH and execute from another directory --Add environment variables when running from a web server

was.

I study every day.

Recommended Posts

I want to pass an argument to a python function and execute it from PHP on a web server
I want to pass an argument to a python function and execute it from PHP on a web server
The real cause of the Jubatus client spitting out a timeout and dropping an error
I made a server with Python socket and ssl and tried to access it from a browser
I want to make a web application using React and Python flask
I want to write an element to a file with numpy and check it.
Execute Python function from Powershell (how to pass arguments)
[Python3] Take a screenshot of a web page on the server and crop it further
Pass an array from PHP to PYTHON and do numpy processing to get the result
Get an image from a web page and resize it
I want to send a message from Python to LINE Bot
I made you to execute a command from a web browser
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
I want to copy an English paper from pdf and put it in Google Translate
I want to create a pipfile and reflect it in docker
Everything from building a Python environment to running it on Windows
I want to get information from fstab at the ssh connection destination and execute a command
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
I want to use jar from python
I want to tweet on Twitter with Python, but I'm addicted to it
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
[Python] Create a linebot to write a name and age on an image
I want to format and check Python code to my liking on VS Code
Python: I want to measure the processing time of a function neatly
[python] Send the image captured from the webcam to the server and save it
I made a POST script to create an issue on Github and register it in the Project
Set up a yum repository server on CentOS7 system and refer to it locally and from other servers.
I tried to push the Sphinx document to BitBucket and it will be automatically reflected on the web server
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
Send a message from Slack to a Python server
I want to email from Gmail using Python.
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to perform SageMaker inference from PHP
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I have a private Python package but I want to install pipenv on GitHub Actions and build a Docker Image
[Python] Concatenate a List containing numbers and write it to an output file.
I want to exe and distribute a program that resizes images Python3 + pyinstaller
I made an image classification model and tried to move it on mobile
Launch a web server with Python and Flask
PHP and Python integration from scratch on Laravel
I want to use ceres solver from python
I want to get / execute variables / functions / classes of external files from Python
I want to convert horizontal text to vertical text and post it on Twitter etc.
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
[Python] I tried to get the type name as a string from the type function
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to get the file name, line number, and function name in Python 3.4
I want to make C ++ code from Python code!
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server ―― 1. Apache introduction
I want to write to a file with Python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux and build a Web server --2 PHP introduction
Steps to attach and debug from VS Code to Jupyter Lab on a remote server
[Python] I made a system to introduce "recipes I really want" from the recipe site!
[Mac] I want to make a simple HTTP server that runs CGI with Python
Source compile Apache2.4 (httpd 2.4.43) + PHP7.4 on Linux to build a Web server --3 MySQL 8.0 introduction
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
I want to replace the variables in the python template file and mass-produce it in another file.
I tried studying on the WEB server side at an in-house Python study session
I want to write a triple loop and conditional branch in one line in python
I want to embed a variable in a Python string