Archive

Posts Tagged ‘Common Gateway Interface’

Setup Apache Server on MAC

April 13, 2012 1 comment

To Set Up a Server on my mac.

  • Goto Preference: Sharing, Check the Sharing View
  • Now the apache is running, to do a test: put:  localhost in your browser, you will see “it works!”
  • When you check the box for Sharing View, you will be notified that there are two different websites are now available on your computer
    • 2. Computer Website: http://your-computer-name/
    •       The Site Folder in  <Library/WebServer/>
    •       There are 3 subfolders under this folder: a) CGI-Executable; b) Documents; c) Share
    •        You should put your executables in this folder, such as the below python code:

#!/usr/bin/python
print “Content-type: text/html”
print
print “<html><head>”
print “”
print “</head><body>”
print “Test Page”
print “</body></html>”

Name it as ‘test.py’, and in the terminal change it to

                   $ sudo chmod 755 test.py

Now you can run it by : http://localhost/cgi-bin/test.py

Here’s a useful link: http://www.editrocket.com/articles/python_apache_mac.html

Also here’s how to share a folder: http://www.macworld.com/article/1140370/pythonserver.html

In Terminal, first cd to the folder you’d like to make available via the Web server. For example, cd ~/Pictures to switch to your user’s Pictures folder. Next, you activate Python’s built-in Web server with this command: python -m SimpleHTTPServer (and yes, capitalization counts). Press Return, and you’ll see a message stating Serving HTTP on 0.0.0.0 port 8000 .... The important bit there is the port number (8000). (You can use a different port number by simply including it at the end of the command, like so: python -m SimpleHTTPServer 8080.) To access the server from the machine you’re running the Python server on, switch to a Web browser, and enter http://localhost:8000 (replace 8000 with the port you used, if you didn’t use the defaut) in the URL bar. When you press Return, you’ll see a listing of all files in the directory. Depending on what types of files are there, and which browser you’re using, you can then click on files to view them in the browser. In my testing, Firefox works better than Safari, as Safari wants to download file types it doesn’t know how to handle while Firefox will often let you open them with a helper application. To access the server from another machine on your network, you need to know the server computer’s name. You can find this in the Sharing System Preferences panel, in the aptly-named Computer Name section of the panel. Just below the computer’s actual name, you’ll see a line that begins Computers on your local network can access…, followed by the name they’ll need to use to do so. Typically, this is simply the Computer Name followed by .local—so if the machine’s name is OctoMac, for instance, you can then access the server by entering http://OctoMac.local:8000 in your browser’s URL bar.

Advertisements

Configure Apache web server for Python on Windows

March 26, 2012 Leave a comment

Configure the Apache web server to run Python programs on Windows machines.

For detailed information, please check the below link:
http://www.editrocket.com/articles/python_apache_windows.html
http://www.editrocket.com/articles/apache_windows.html

1. Install Python and Apache
Python: http://python.org/download/.
Apache: http://httpd.apache.org/download.cgi

2. Configure Apache to run Python CGI

The next step is to edit ‘httpd.conf’ apache configuration file located in the apache install directory in the conf directory. Search the httpd.conf file for the line

Options Indexes FollowSymLinks

Add ExecCGI to the end of the line. The line should now look similar to the following (NOTE: there may be more options listed):

Options Indexes FollowSymLinks ExecCGI

Next, search for the following:

#AddHandler cgi-script .cgi

Uncomment this line by removing the # in front of the line, and add a .py to the end of the line. The new line should look like this:

AddHandler cgi-script .cgi .py

3. Restart Apache

Now, the apache web server needs to be restarted. You can do this either via the Apache service located in the services control panel or via the Start -> All Programs -> Apache . . . -> Control Apache Server menu option.

4. Run a test Python page

You can use the EditRocket hello world Python sample web page template for a test Python page. This is located in File -> New From Template -> python -> Sample_Web_Page. The following is an example of the template:

#!/usr/bin/python
print “Content-type: text/html”
print
print “<html><head>”
print “”
print “</head><body>”
print “Hello.”
print “</body></html>”

Note the line #!/usr/bin/python. This line needs changed to match the location of your Python installation. For example,

#!/Python27/python

Here is an example assuming Python is installed in the C:\Python27 location

#!/Python26/python
print “Content-type: text/html”
print
print “<html><head>”
print “”
print “</head><body>”
print “Hello.”
print “</body></html>”

Save this file as test.py to your htdocs folder under your apache installation directory. Open your web browser and type in your apache host (and :port if the port is something other than 80) followed by test.py, for example

http://localhost/test.py

Some Useful Links for python-cgi

March 3, 2012 Leave a comment