0

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?

11
  • I don't understand you problem - Apache runs on server and if it runs python code then it runs it on server, not on user device. Maybe you doesn't run it with Apache. Do you have installed module mod_cgi or mod_fcgi or mod_python in Apache? Or maybe use more popular method and use any web framework like Flask/Django instead of using Apache. Commented Jan 18, 2020 at 20:57
  • Well my problem is whenever I send request to get the file, which is written in python, this error is shown. "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. " I cannot make apache to execute the file properly, I have enabled mod_cgi.so. Also created specific <Directory/> in httpd.conf which is responsible only for python files... Will probably try Django or Flask, but I just was curious if apache can execute python files. Commented Jan 18, 2020 at 23:06
  • 1
    BTW: as I know as standard it runs files only in subfolder cgi-bin/ so first you can try t put code in this folder. On Linux script would need shebang #!/usr/bin/env python in first line and it should be executable - chmod a+x script.py Commented Jan 18, 2020 at 23:36
  • 1
    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 may 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. Commented Jan 19, 2020 at 0:03
  • 1
    BTW: 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 Commented Jan 19, 2020 at 0:10

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.