How to handle Python files with Apache - python

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?

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

Related

How do i create a Python HTTP server to host a file inside it?

I'm trying to make a program. This program will have the functionality to host a file in an HTTP server but I couldn't find a way to make it.
This HTTP server will host a JSON file in it.
And I wanted to make a program that sends a request to that server and then reads everything inside that server directory.
I tried many attempts and messed around with Python but I can't find the correct way to make it
This is already included in the standard library. Just run this in your terminal from the directory you want to serve:
python -m http.server
This is not recommended for product use, however, if you need to run this in production it is better to use a real HTTP server like Apache or NGINX anway.

strange behavior with flask on windows

I'm maintaining a web application built with Flask with python 2.7 coupled with Jinja and angularjs .In a Linux environment, everything is working fine.
On windows when I run the application on cmd or git bash (python app.py), I only see that the server is running and in which port (and everything else is working fine in the browser), but the problem is that the logs in console aren't shown like in a Linux terminal.
For example, I can't see the requests like: POST /login..or an exceptional mission or even a simple print "test" Dosen"to show (still everything is working in a browser).
Even worst, when I terminate the server with "ctr+c" all the previous messages and logs are printed in the terminal, all together in one single dump!
--- Update ---
when i use the command python -u app.py
it's even worst , the application dose'nt run in the browser anymore , no log in console and when i termiante it shows this :
screenshot of terminal
I would suggest that you creat a proper uWSGI gateway.
An example setup could be:
Linux 18.04 ->
Nginx / Apache reverse web proxy mode ->
Gunicorn (which has a debugger you can attach, this will output to the regular log files like other applications. Systemd logs I think) ->
Flask web framework.
Hope this helps.

Python script not running with local web server, just displays code in browser

I have spent hours banging my head against the wall on this one!
I am running Python 2.7.10 on a Mac. I have a few Python scripts that I have written which run inside the Atom editor.
I have been trying to run these scripts on a local web server to speed up development. I have tried MAMP (which just throws 500 Internal Server Errors), and now "python -m SimpleHTTPServer" which just displays the python code in the browser and doesn't seem to execute it.
I have chmod +x my .py files. I start the web server in the folder that contains the .py files.
Here is an example...
hello.py
print("hello world")
When I browse to http://localhost:8000/hello.py I get the raw code displayed in the browser.
print("hello world")
If I use terminal and enter "python hello.py" it runs and displays the correct output...
MacBook-Pro-3:folder dj$ python hello.py
hello world
I have tried dozens of tutorials and suggested solutions, but none seem to help. Am I missing something fundamental here?
Thanks!
D
The server that python -m SimpleHTTPServer (or, for future readers, python -m http.server on Python 3) spins will not execute any file. It is merely a server that serves files as the documentation suggests:
The SimpleHTTPServer module can be used in the following manner in order to set up a very basic web server serving files relative to the current directory.
In order to get a server that will actually execute Python code, you'll need to use another tool. I'd start with bottle or flask.

How run a command (python file) on boot on AWS EC2 server

I'm having some problem making a python file run everytime the AWS server boots.
I am trying to run a python file to start a web server on Amazon Webservice EC2 server.
But I am limited to edit systemd folder and other folders such as init.d
Is there anything wrong?
Sorry I don't really understand EC2's OS, it seems a lot of methods are not working on it.
What I usually do via ssh to start my server is:
python hello.py
Can anyone tell me how to run this file automatically every time system reboots?
It depends on your linux OS but you are on the right track (init.d). This is exactly where you'd want to run arbitrary shell scripts on start up.
Here is a great HOWTO and explanation:
https://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html
and another stack overflow specific to running a python script:
Run Python script at startup in Ubuntu
if you want to share you linux OS I can be more specific.
EDIT: This may help, looks like they have some sort of launch wizard:
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
When you launch an instance in Amazon EC2, you have the option of
passing user data to the instance that can be used to perform common
automated configuration tasks and even run scripts after the instance
starts. You can pass two types of user data to Amazon EC2: shell
scripts and cloud-init directives. You can also pass this data into
the launch wizard as plain text, as a file (this is useful for
launching instances using the command line tools), or as
base64-encoded text (for API calls).

How can I create an local webserver for my python scripts?

I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with, for the front end.
I've been looking, therefore, for a lightweight web server that can execute python scripts, sitting in the background on a machine, ideally as a Windows service. Security and extensibility are not high priorities as it's all running internally on a small network.
Should I run a native python webserver as a Windows service (in which case, how)? Or is it just as easy to install Apache onto the user's machine and run as CGI? Since this is all local, performance is not an issue either.
Or am I missing something obvious?
Don't waste a lot of time creating Windows service.
Don't waste a lot of time on Windows Apache.
Just make a Python service that responds to HTTP requests.
Look at https://docs.python.org/2/library/basehttpserver.html
https://docs.python.org/3/library/http.server.html for version 3
Python offers an HTTP server that you can extend with your server-side methods.
Look at http://docs.python.org/library/wsgiref.html
Python offers a WSGI reference implementation that makes your server easy and standards-compliant.
Also http://fragments.turtlemeat.com/pythonwebserver.php
"I'm trying to avoid making the user run python stuff from the command prompt."
I don't see how clicking a web page is any different from clicking desktop icons.
Starting a web server based on Python is relatively easy, once you have the web server. First, build the server. Later, you can make sure the server starts. Let's look at some ways.
Your user can't use a random browser to open your local page. They need a bookmark to launch "localhost:8000/myspecialserverinsteadofthedestop/" That bookmark can be a .BAT file that (1) runs the server, (2) runs firefox with the proper initial URL.
You can put the server in the user's start-this menu.
You can make your Python program a windows "service".
Best way is to make your own local server by using command prompt.
Make a new folder say Project
Make a new folder inside project & name it as "cgi-bin"(without quotes)
Paste your .py file inside the cgi-bin folder
Open cmd and change to the directory from which you want to run the server and type "python -m CGIHTTPServer"(without quotes)
Minimize the cmd window & open your browser and type "localhost:8000/cgi-bin/yourpythonfilename.py"(without quotes).
The wasiest step would be navigate to folder where your files are located and running http.server module
cd /yourapp
python3 -m http.server
the you should see something like this in console
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Running a native python webserver as a windows service should be a no brainer. Check out the documentation for writing windows services (win32api, ActiveState python) in python and also the documentation for subclassing BaseHttpServer and SimpleHttpServer.
BTW: I had a similar question on stackoverflow: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?
Basically, you subclass BaseHTTPServer (you have to anyway...) and then... but just read the accepted answer - it set me on the right track!

Categories

Resources