I recently needed to write a Python script that fetches data periodically from an external API and maintains it in a local txt file. I plan to host this script on my virtual hosting at lolipop.jp and run it every ten minutes through cron. However, after deploying it, the script has not been producing any output. Below is the method for deploying a Python script on the virtual hosting at lolipop.jp without using SSH.
Upload your script
Firstly, you need to obtain the directory where the Python interpreter is located on your machine. You can find specific information at this link. In my environment, it is /usr/local/bin/python3.7
.
In the first line of your Python script, add the declaration for this interpreter, for example:
#!/usr/local/bin/python3.7
Subsequently, upload your script and set permissions to 700 using an FTP tool.
In the Lolipop.jp control panel, add your script. Note that the execution address here only needs to include the file name after the home directory, for example: scripts/my.py
. There is no need to declare the interpreter again.
A crucial step is to fill in an email address to receive error messages for troubleshooting during installation; otherwise, you won’t receive error information, and this step is essential.
Install pip packages
Next, proceed to install the required packages for the script.
If your configuration supports SSH connection, you can enter these commands directly in an SSH session. If not, like in my case, you need to write these commands into a .sh file to be executed by another cron.
You can find the personal home directory at the control panel, and use YOUR-HOSTNAME\
as a placeholder.
Firstly, add local/bin
in the home directory to the environment variable for easy pip installation:
export PATH=$PATH:/home/users/1/YOUR-HOSTNAME/.local/bin
For cases where installation is not done through SSH, it’s advisable to switch the current directory to the home directory:
cd /home/users/1/YOUR-HOSTNAME/web
Download Python-pip and install it:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py /usr/local/bin/python3.7 get-pip.py --user
Then, use python-pip to complete the installation of missing dependencies, for example:
/usr/local/bin/python3.7 -m pip install requests --user
Additional information for requests library
Especially, if you need to install the requests library, after installation, you might encounter runtime errors. This is due to the machine’s OpenSSL version being too low (OpenSSL 1.0.2k-fips 26 Jan 2017), while the new version of urllib3 in requests requires OpenSSL 1.1.1 or higher. If this issue occurs, install an older version of requests to resolve it.
Uninstall the old version and install the specified version of requests:
/usr/local/bin/python3.7 -m pip uninstall requests --user /usr/local/bin/python3.7 -m pip install requests==2.28.1 --user
Once the dependencies are installed, the script should run normally. In case of installation errors, error messages will be sent via email.
References
- 【python】lolipopでバッチを定期実行する【cron】 | MaedaDevelopment
- Drop support for OpenSSL<1.1.1 · Issue #2168 · urllib3/urllib3