Using CGI scripts, I can run single Python files on my server and then use their output on my website.
However, I have a more complicated program on my computer that I would like to run on the server. It involves several modules I have written myself, and the SQLITE3 module built in Python. The program involves reading from a .db file and then using that data.
Once I run my main Python executable from a browser, I get a "500: Internal server error" error.
I just wanted to know whether I need to change something in the permission settings or something for Python files to be allowed to import other Python files, or to read from a .db file.
I appreciate any guidance, and sorry if I'm unclear about anything I'm new to this site and coding in general.
FOLLOW UP: So, as I understand, there isn't anything inherently wrong with importing Python files on a server?
I suggest you look in the log of your server to find out what caused the 500 error.
You can get an extensive error message by adding the following lines at the beginning of your CGI script:
import cgitb
cgitb.enable()
Related
I would like to run a couple of very simple Python 3 scripts on the web. As an example, say, the script just reads certain parameters from the URL, sends an email and prints out a simple HTML page saying "success".
I have a virtual private server with Nginx, so I am free to setup the best framework.
My question is: what is the state-of-the-art setup to do this?
More particularly: what do I need to install on my server for Nginx and what do I use in my Python scripts for e.g. reading the URL content? My idea is, that once the server setup is done, I can just put any new script_xy.py file into some directory and it can be accessed using the URL of the script, without a full blown deployment for each script.
Flask If I were to use Flask (or Django), each new script would need its own, continuously running process and its own Nginx setup. That seems like a total overkill to me. Another alternative is web2py, is it the same here or would that be an idea?
CGI 20 years ago I used to program such simple things as Perl scripts using CGI. I read this can be done in principle with Python, but CGI is slow. Then there is Fast CGI. However, my impression was that this is still a bit outdated?
WSGI WSGI seems to be the state-of-the-art alternative to CGI for Python. What python modules would I need to import in my script and what would be the setup for Nginx?
Something else? As you see, I might just need a few hints on what to search for. I am not even sure if I need to search for "Python web framework" or "Python server" etc.
Any help is greatly appreciated.
Thanks a lot for your ideas!
juxeku
I have a server side Python script that imports a big package called nltk. It ran at a command prompt, but would not run in Apache server.
I tried the logging and put it as the first import and created a log file immediately after. But nothing is written to the file before the script crashes.
Is there a way to see the "ImportError: no module ..." when the script runs on Apache?
As far as I'm aware, there's no built-in way to tell Apache to simply dump error log messages directly to the browser. I'm betting the error_log output happens in an independent part of the Apache pipeline -- just a guess.
Most of the time when someone wants output errors directly to the browser, that person is a developer working in a web script such as PHP, Python, Perl, or other lang. In order to output error messages that occur within your lang interpretter, you have to format the output and pass it through apache as if it was normal output.
PHP
Some languages provide an easy way to do this, such as the well known error_reporting switch in PHP.
Python
Unfortunately some languages, make this painful or almost impossible and Python is one of them.
At one time this was apparently possible using https://docs.python.org/3/library/cgitb.html
However that library has been deprecated. Seems like the Python community doesn't have much love for developers who want to get immediate feedback about errors in the browser. :(
EDIT 1
It looks like I can import the sqlite3 package. Now I'm working on figuring out how to specify the IP address of the hosted server so I can connect to it. It's not looking likely though.
EDIT 2
It looks like sqlite3 is not designed to access hosted databases. The search continues.
This is a pretty basic question, however I haven't found any resources on Namecheap's website, here, or through Google searches that shed any light on my problem. I'm trying to write a python script that will query my hosted database to retrieve records. So far I haven't gotten past the first step of importing a module and this is my code:
#!/usr/bin/python
print "Content-type: text/html\n\n"
import cgitb
cgitb.enable()
import PyMySQL
cgitb works fine and prints out the following error message:
I also tried importing MySQLdb and received the same error. Does anyone have experience with databases, python, and namecheap?
I have spent ages trying to figure this out. I'm basically trying to develop a website where I have to execute a python script when the users clicks a specific button. After researching on Stack Overflow and Google, I need to configure Apache to be able to run CGI scripts. I have seen numerous examples of software that can accomplish this, such as mod_wsgi. The problem is that I get extremely confused with the instructions for these softwares, especially for mod_wsgi. I don't understand the instructions at all, and I can't install the software and get anything to work.
If anyone has a very easy method for executing python scripts in Apache, I would greatly appreciate. If anyone would like to explain how to use mod_wsgi, I would greatly appreciate that as well, because as of right now I have no idea how to use it and the installation instructions are confusing the hell out of me.
One way, not so easy but simple in some way, is to use CGI, as you said.
You can find more information on Apache documentation and Python CGI module documentation.
But, basically, you have to set your server to run cgi sripts. This is done by editing httpd.conf or a .htaccess file:
For the first option, add or uncomment the follow:
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/ # update the second location according to your configuration. It has to be a place where apache is allow to use, otherwise see apache documentation for setting another directory.
Then, you just need to add your python script in the directory that you set above.
Note that the output from your script must be preceded by a mime-type header, as Apache documentation says.
So, a hello world script could be named hello.py and its content could be:
#!/usr/bin/python
print('Content-type: text/html') # the mime-type header.
print() # header must be separated from body by 1 empty line.
print('Hello world')
Than, you can call your scrit from a browser:
http://localhost/cgi-bin/hello.py
Note that Python has some goodies inside its cgi realted builtin modules. The cgi module will give you a way to handle forms, and cgitb will give you a helpful (but not perfect) way to debug your script. For more on that, read the documentation again.
Finally, using cgi directly to run python scripts gives you a raw way to work with http requests. There is a lot of already done frameworks, like flask and django, that gives you more power easily. You could check that.
Can I use os.system() or subprocess.call() to execute a Python program on a webserver?
I mean can I write these functions in a .py script and run it from a web browser and expect the program to be executed?
Thanks a lot.
EDIT:
Sorry for all the confusion, I am giving you more background to my problem.
The reason I am trying to do is this.
I have a Python program that accepts an XML file and returns me TTF file.
I run that program in terminal like this:
ttx somefile.xml
After which it does all the work and generates a ttf file.
Now when I deploy this script as a module on web server. I use a to allow user to browse and select the XML file.
Then I read the file data to temporary file and then pass the file to the module script to be executed like this:
ttx.main([temp_filename])
Is this right way to do it? Because at this point, I don't get any error in the log or in browser. I get blank screen.
When this didn't work, I was going to try os.system or subprocess.call
You do not use os.system or subprocess.call to execute something as a cgi process.
Maybe you should read the Python cgi tutorial here:
http://www.cs.virginia.edu/~lab2q/
If you want your cgi process to communicate with another process on your local machine, you might want to look at "REST frameworks" for Python.
So long as your server is configured to run CGI scripts (Apache's documentation for that is here, for example), yes, you can execute a python script from a webserver. Simply make sure the script is in the appropriate cgi-bin/ directory and that the file has executable permission on the server.
With regards to your comments:
You can, if you really want, explicitly allow other folders on the server to run executable code. I don't know what server you're using, but on Apache this is done by setting Option +ExecCGI for the folder you want. Again, see the docs I linked to.
You need to give an absolute path with respect to the server. As an example, a site I develop has the layout: /public_html/cgi-bin/ When I want to access .cgi or .py files, the url for the site is something like http://chess.narnia.homeunix.com/cgi-bin/index.cgi. You can also set up re-directs to certain files if you want.
One way to pass parameters through your browser is to append them to the URL like an HTTP POST method. Here's a good example of doing that.
Is that what you were looking for with your question, or did you want to actually invoke the python script with os.system()?
Yes, I do it all the time. Import as you would do normally, stick your .py in your cgi-bin folder and make sure the server is capable of handling python.
Another option would be to simply create an application on Google's App Engine. That gives you oodles of resources and APIs for Python execution.
http://code.google.com/appengine
I've done it quite a bit in classic ASP on IIS 5 and above. I would have the ASP engine execute python code (instead of, e.g., vbscript (hearkening back to the old days, here)). Behind those asp pages would be python modules written in straight python that could be imported and could execute pretty much arbitrary code. As others have mentioned, the effective user needs to have execute permission on the thing you're trying to execute.