Dako
Dako

Reputation: 55

How to handle Python files with Apache

So I want to get into React Native development and have decided Python to be my backend, but for some reason I cannot configure the Apache correctly. The only way to successfully get the result from the request is to include path to python.exe at the start of the document like so:

!C:\Users\Name\PycharmProjects\AppName\venv\Scripts\python.exe

But the problem is that the file is than executed by the Python console, and if I want to access it via mobile phone I get this error:

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

So my question is:

Is there any way in which I can configure Apache to execute a file, without the requirement of the py console, so the request might be handled by devices, which doesn't have a Python console installed?

Upvotes: 0

Views: 1577

Answers (1)

furas
furas

Reputation: 142641

if you connect from mobile device to http://192.168.1.3/HelloWorld.py on server with CGI then code should be executed on server, not on mobile device. If CGI doesn't work then server may try to send code as normal file and then mobile mdevice ay try to run it locally but it is wrong - CGI server should run code on server.

At start I would put code in subfolder cgi-bin to run it as http://192.168.1.3/cgi-bin/HelloWorld.py because most CGI servers as default run code only in this subfolder.


On Linux script would need shebang

#!/usr/bin/env python

in first line and it should be executable

chmod a+x script.py

CGI has also some rules how to generate data which it will send to client. It may need at start extra information for HTTP protocol - and using only print("Hello World") may generate wrong data and it may have problem to send it. You should have it in any tutorial for CGI scripts. See module cgi


To run Python's code Apache needs module mod_cgi, mod_fcgi or mod_python

mod_cgi and mod_fcgi can run scripts in different languages: Python, Perl, Ruby, etc. and even Bash, PHP or C/C++/Java


Python3 has standard module http which can be used also as simple server

python3 -m http.server --cgi 

and it will serve all files in folder in which you run it. And it runs files from subfolder cgi-bin/ - see doc: http

Upvotes: 1

Related Questions