Can we run python progam in cgi? - cgi

The HTML form that I makes use of uses browse option and the file format uploaded is like this(/home/anu/anu.py). Now since in the form action the processing takes place in the server side. can we use CGI for taking the filename as input and process the text using the python program in the hard disk and display the output on the web page.
Is there any code available?
can we run python program in the hard disk using CGI?

Yes, you can run Python programs with CGI. The cgi standard module will help.

In Apache you can configure additional extensions to be treated as cgi.
The directory that contains the CGI will have to have these option included
Options Indexes FollowSymLinks ExecCGI
AddHandler cgi-script .cgi .py

Related

Solution to execute CGI scripts

I have some CGI scripts which are used internal to the organization. Due to some standard changes, the server/system team has commented the below line from httpd.conf (apache config file) that support the CGI scripts. Because of this change, the existing CGI scripts are affected and unable to execute them on browser.
### LoadModule cgid_module modules/mod_cgid.so
Is there a way to overcome this situation and make the scripts work as is.
NOTE : The above commented line cannot be uncommented/enabled.
Ways of circumventing your company's policy, from best to worse:
Load mod_cgi anyway.
Load mod_fcgi, and convert your CGI script into a Fast CGI daemon. It's a lot of work, but you'll can get faster code out of it!
Load your own module that does exactly the same thing as mod_cgi. mod_cgi is open source, so it should be easy to just rename it.
Load mod_fcgi, and write a Fast CGI daemon that executes your script.
Install a second apache web server with mod_cgi enabled. Link to it directly or use mod_proxy on the original server.
Write your own web server. Link to it directly or use mod_proxy on the original server.
Last time you asked this question, you talked about using mod_perl instead. The standard way to run CGI program unchanged (for some value of "unchanged") under mod_perl is by using ModPerl::Registry. Did you try that? How did it go?
Another alternative would be to convert your programs to use PSGI. You could try using Plack::App::Wrap::CGI or CGI::Emulate::PSGI. Using Plack would free you from any deployment restrictions. You could run the code under mod-perl or even as a separate service behind a proxy server.
But I can't help marvelling at how ridiculous this whole situation is. Your company has CGI programs that it (presumably) relies on to run part of its business. And they've just decided to turn off support for them. You need to find out why this decision has been made and try to buy some time in order to convert to an alternative technology.

How to do URL rewriting in Apache through Chef?

I've made a cookbook in Chef to install a web application, Apache and PHP.
This web application has a PHP document that clients access in this URL:
http://localhost/www-app/ClientFE
With this I have a problem. By default PHP will only interpret documents that have a PHP extension, and as my URL doesn't have it, it gets interpreted as text. I can't change the file's extension because the client (which is also software) should not be modified.
What I want to accomplish is to make PHP interpret that document, and for what I've investigated it can be accomplished in three ways at least: configure appropriately PHP, create an alias, create a rewrite rule.
First: I could configure PHP to interpret the desired document:
<FilesMatch "/www-app/ClientFE">
SetHandler application/x-httpd-php
</FilesMatch>
Second: I could do it through mod_write (I would have to rename the fill in the web app, which is also acceptable for me):
RewriteCond ^/www-app/ClientFE /www-app/ClientFE.php
Third: I could do it with mod_alias in a similar fashion than mod_rewrite.
The 3 are good for me, but my problem is: how to do them with Chef ? I haven't been able to find useful docs about it, and I have already tried to put this in my recipe (I found them around the Internet but they haven't worked):
apache_rewrite do
source "/www-app/ClientFE"
target "/www-app/ClientFE.php"
end
and this
apache_module "alias" do
source "/www-app/ClientFE"
target "/www-app/ClientFE.php"
end
Some people in my company suggested 2 valid solutions:
Put the configuration in a .htaccess inside my web application. This works for me and allows to use the alternatives I mentioned above.
Do nothing! It happens that the Multiviews option in the Apache2 cookbook is enabled by default, and it allows Apache to resolve URL http://localhost/A as http://localhost/A.php .... I mean, if Apache is asked about file A, and it doesn't find it, then it will search for A.php instead.
From the two, I chose the later, and yes, it works. It wasn't working before because in my many attempts I created both A and A.php and in that case the Multiviews has no effect.

Why do I see the directory structure on a Web server and not the default index page?

I am developing a Java application and I have to integrate a Perl application's code in it. The service has been provisioned on a CGI server.
When I am copying Perl files to the server location, index files are supposed to be automatically invoked. But when I am testing that URL, it is showing me the directory structure and not the default index page.
You chaneg the defaut directory listing function of Apache using the DirectoryIndex directive as described in the Apache Documentation
So if you want myprog.pl to run, instead of seeing a directory index, then you need a directive similar to:
DirectoryIndex myprog.pl
Your exact directive will be similar, but may differ depending on file paths.

How apache calls/invokes the appropriate handler/interpreter?

First of all, sorry if the question is unclear due to my poor knowledge.
I'm interested to know how apache calls the appropriate engine/invoker to serve a request. Suppose, user requests a http://somesite.com/someurl.php - now how apache determines that it needs to launch the PHP interpreter? Does apache determines so based on the file extention/MIME type or anything else?
What I know is: I can configure apache to invoke certain interpreters based on file's extension, by something like:
AddHandler cgi-script .cgi .py
# Tells apache to treat .cgi & .py files to treat as cgi scripts
Why I'm concerned about it? Recently, I came to know from my question ( PHP file upload: mime or extension based verification? ) that if some user uploads a file with wrong MIME type (i.e image/jpeg) but with an extention .php the file can get executed (assuming it has got execution permission) and malicious php code included with the EXIF meta-data can do harmful things.
It depends entirely on how the server is configured. By default, Apache always uses the default handler which simply serves the content of the file. However, you can change that using the SetHandler directive. This directive can be placed literally anywhere in the configuration, including <Files> or <Directory> or <Location> blocks, making it possible to set the handler based on the filesystem path, URL prefix, or pretty much any other variable Apache can access.
It is particularly common to set the handler based on the filename extension, though, so Apache provides the AddHandler directive as a shortcut for doing so.
For more details, have a look at the handler documentation.
P.S. For what it's worth, filesystems normally don't store MIME type data, so Apache normally has to guess at the MIME type of a file by examining the extension.

How to keep a text file readable just from php and python, and not by users?

There is a way to keep a text file on my server, but that only my php and python scripts would be able to read it, and if the user just type the url of the file will obtain a forbitten state?
I'll like to keep it in the website directory, httpdocs (or httpsdocs, maybe).
Tha file will contain my database user and passord, and other stuff like it: must be in text-format becose i need to have just 1 file but i'll need to read it by many languages (php and python for example) in an easy way (probably in json structure)
So doesnt matter if the file extension would be .txt, can be whatever you like more, but i just need that if the users cant go to http://www.example.com/myfile.txt or download it by ftp.
Is this possible, with some apache settings (chmod? maybe not..) or not?
put a .htaccess file in your directory and deny access to your file like this:
<Files file.txt>
Order allow,deny
Deny from all
</Files>
You could try encoding you text file in Base 64 (or something similar) that's easy enough to implement/has implementations in various languages.
See the Wiki article on Base 64 encoding
Place the file outer the document directory (e.g instead of /var/www/public to /var/www/private) and give rw permissions to the apach to this dir, but do not enable it in apache.conf