SimpleHTTPServer which will allow an ajax request to execute a PY file? - python

i was going to make a simple web interface for a Python script / module I am working on. I figured to submit the form which would do 1 of 2 options. Modify a config json file and run the python script, or submit a form to the python script and execute it.
Simply put, i know when working with some other applications, Apache for example, you need to configure it such that it accepts .py files as executable. Is that the same case with SimpleHTTPServer? If so, how?
I was looking at: https://docs.python.org/2/library/simplehttpserver.html
I was also looking at BaseHTTPServer at https://docs.python.org/2/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler though nothing was jumping out at me during configuration

you can enable the CGI (Common Gateway Interface https://en.wikipedia.org/wiki/Common_Gateway_Interface ) of the SimpleHTTPServer, see the following thread How to host python cgi script with `python -m SimpleHTTPServer 8000` or `python -m CGIHTTPServer 8000`?
this is similar to get Django running ( https://en.wikipedia.org/wiki/Django_(web_framework) ) . Django is enabled using the mod_wsgi module or FastCGI on a WSGI-compatible webserver (Apache,...).

Related

Error with Python subprocess when running Flask app using nginx + WSGI

I have developed a Python web server using Flask, and some of the endpoints make use of the subprocess module to call different executables. On development, using the Flask debug server, everything works fine. However, when running the server along with nginx+WSGI (on the exact same machine), some subprocess calls fail.
For example, one of the tools I'm using is Microsoft's dotnet, which I installed from my user as sudo apt-get install -y aspnetcore-runtime-5.0 and is then called from Python with the subprocess module. When I run the server with python3 server.py, it works like a charm. However, when using nginx and WSGI, the subprocess call fails with an exception that says: /bin/sh: 1: dotnet: not found.
I suspect this is due to the command not being accessible to the user and group running the server. I have used this guide as a reference to deploy the app, and on the wsgi .ini file, I have set uid = javierd and gid = www-data, while on the systemd .service file I have User=javierd, Group=www-data.
I have tried to add the executables' paths to /etc/profile, but it didn't work, and I don't know any other way to fix it. I find also very surprising that this happens to some executables, but not to all, and that it happes to dotnet, for example, which is located at /usr/bin/dotnet and therefore should be accessible to every user. Any idea on how to solve this problem? Furthermore, if somebody could explain me why this is happening, I would really appreciate the effort.
Thanks a lot!
Ok, finally after having a big headache, I noticed the error, and it was really simple.
On the tutorial I linked, when creating the system service file, the following line was included: Environment="PATH=/home/myuser/myfolder/enviroment/bin".
Of course, as this was overriding the path, there was no way of executing the commands. Once I notices it I just removed that line, restarted the service, and it was fixed.

Execute other python script when django server is started

I have a Django project and other python script which is a ZMQ socket which continuously listens for message to perform some operation, ZMQ server script is an independent script which for now runs as python zmq_server.py in a terminal or cmd.
What I am trying to do is start the ZMQ server script when python manage.py runserver is called to start the django server.
I did some digging but found nothing related to this, is it possible to do something like this?
You can run any script when your Django server starts by importing it in your settings.py at the top like this:
import zmq_server
Normally that's what you'd do for a script you're going to use in your project, but if you just want it to get executed you can do it like that as well.

running CGI with py2exe

this may sound strange but i can't seem to find an example anywhere..
i'm trying to run a script via CGI locally but i'm trying to run it as an .exe via py2exe.
so the idea is that you click the executable and CGI starts locally and the script gets run without having to install any python on the machine and you can pull up the local website in browser and the script is producing the webpage.
So i can get all these things to work on my machine with python installed, and i created a script that imports the cgi script i created to start CGI and imports the python script that i want to display and i can see compiled versions of both in the dist that it creates. but from here i have no idea where to go, as normally i would just ping the cgi-bin/script.py folder via the local host and it would show up in my browser.
my question:
can a CGI and script be run locally and displayed as an executable without having python on the machine.
and the extra build file with bdist.win32 created does that need to be called somehow?
i'm assuming there is somewhere in the main program i'm supposed to 'display' the script via html but not quite sure where to do that?
i get the 'cgi script is not a plain file ('//') error.. not sure on that one.
any help appreciated, thx
I partially solved this by importing webbrowser and then openning the local url with
webbrowser.open_new('http://localhost:8086') #proper url here
But it's precisely CGI what is driving me crazy. For some reason, when browsing to a python script, it won't execute - I just get the code, even when the original python script defined cgi_directories properly (they did work with the python script). I hope that helps a bit, at least. Here's the whole script:
#!/usr/bin/env python
import webbrowser
import BaseHTTPServer
import CGIHTTPServer
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8008)
handler.cgi_directories = ["/cgi"]
httpd = server(server_address, handler)
webbrowser.open_new("http://localhost:8008/cgi/script.py");
httpd.serve_forever()

Python cgi on apache server

I am new to python cgi programming. I have installed apache 2.2 server on linux mint and I have my html form in var/www folder which is being displayed properly. Action to the form is a cgi script that I've put in the folder /usr/lib/cgi-bin. But on submit, it says "The requested URL /usr/lib/cgi-bin/file.cgi as not found on this server." Does anyone know the fix for this?
What is the name of your Python program? Is it /usr/lib/cgi-bin/file.cgi?
What are the rights of this file? Can it be read by apache? Can it be executed?
Is first line with #!/usr/bin/env python or similar good? Make sure that this file can be run from command line (ie. shebang is good)
Does apache receive request with that file? Look at apache logs, especially error.log and access.log (probably in /var/log/apache)
Make sure you have enabled ExecCGI for /usr/lib/cgi-bin/ directory in Apache configuration. For examples see at: http://opensuse.swerdna.org/suseapache.html. You can also use ScriptAlias directive.

how to run .py file in browser using python webserver

i have running a python webserver by using this simple script -
from http.server import SimpleHTTPRequestHandler as RH
from socketserver import TCPServer
ServerName='localhost'
OnPort=8000
print ("Server is running at Port 8000")
TCPServer((ServerName, OnPort), RH).serve_forever()
it is running good and run my index.html file but it is not run .py file in browser when i type --
http://localhost:8000/myfile.py
it just show my python codes as it is i write in file ,it is not execute the code please help me to run my python file (.py) in browser by using this webserver only ,i don't want to use any framework or another webserver.
Is there any way to make a virtual host in this python server like apache.
if possible please suggest me that how to do this and any configuration file need to be configured or not.
thanx...
The problem is that SimpleHTTPRequestHandler only serves files out of a directory, it does not execute them. You must override the do_GET method if you want it to execute code.
You might want to check out CGIHTTPRequestHandler instead. I very briefly played with it on a linux based system and the CGI criteria was for a file to be executable and have the correct shabang. I'm not sure if this would work on Windows though ( if that is even a relevant concern )
With your example code you would only need to change RH to
import CGIHTTPServer.CGIHTTPRequestHandler as RH
Alternatively the 3rd party library Twisted has a concept of .rpy files that are mostly plain Python logic. http://twistedmatrix.com/trac/wiki/TwistedWeb
Note:
Just verified that the CGIHTTPRequestHandler works. Caveats is that all python files must be in a cgi-bin subdir, they must have a valid shabang, must be executable, and must provide valid CGI output.
Personally having written C++ CGI scripts in the 90's, the CGI route seems like the path to maddness... so check out Twisted, CherryPy, or Django ( those three mostly cover the Python web spectrum of possibilities )

Categories