FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Python via CGI - A personal triumph






Have you successfully implimented python projects on FriHost?
Yes, Duh! My FriHost sites are witten in Python - By me.
10%
 10%  [ 1 ]
Yes, but only some small stuff. Still learning.
40%
 40%  [ 4 ]
No, still confused on some things but still going!
30%
 30%  [ 3 ]
NO! GAME OVER MAN, GAME OVER! Im going to play with CSS and HTML some more
20%
 20%  [ 2 ]
Total Votes : 10

eznet
Python via CGI on FriHost

I know that the topic has been addressed elsewhere. With this said there seems to be some confusion as most of the addressing of this issue consists of a blend of mod_python and CGI info; something that is deeply confusing for a noob(or at least was for my feeble noob mind). To hopefully help anyone (as dumb as I) who has had problems with this, I have created this brief (and ridiculously elementary) tutorial. I concluded my problems initially were with A)combining mod and cgi info in my attempts or b)sequence and completion of all these steps(or lack there of).

Needless to say, I finally got my Python CGI working. I, of course, cannot take credit for it as I have read many, many articles (onsite and off) tonight. It has been a night of mistakes to say the least. But alas, this has passed and now I have an executable Python CGI script.

I am apparently not yet savvy enough to use the mod_python method. To be quite honest I had a hard enough time with the CGI which is supposed to be really easy; so I will be learning to crawl before I walk.

If you are interested in the step-by-step then here you go…

(note: Everything can be done from within your DirectAdmin file manager from creating the cgi files to editing and permission settings)

Step1: enter a cgi-bin directory of choice (either create a new sub-domain or go into a public directory you already have available).

Step2: Open up your favorite syntax editor (I personally like Crimson) and enter the following:
Code:

#!/usr/bin/python

# Required header that tells the browser how to render the text.
print "Content-Type: text/plain\n\n"

# Print a simple message to the display window.
print "Hello, World!\n"

Save the file to pythonTest.cgi

Step3: Upload pythonTest.cgi to the cgi-bin director that you are working in (Step1).

Step4: Set your permissions for pythonTest.cgi to 755 (executable) – can be done via explorer, your ftp program or DirectAdmin file console.

Step5: Open up Firefox (or your browser of choice) and enter http://yourName.frih.net/yourDirectory/cgi-bin/pythonTest.cgi and press enter! - Of course you will need to replace yourName with your frihost name and also replace yourDirectory with the directory you are working in (Step1).

Step6: Start writing python web-apps (I guess, I ain’t there yet).

In my case, as I think is everyone’s case who is using Python via CGI instead of mod_python, there was no need to have anything within my .htaccess

I have yet to get the hang of mod_python but I hope to as I have read that it is much faster. However, this will be down the road, my python writing is currently nowhere near a level of complexity that will require increase performance.

Hope this helps someone.

Resources:

http://www.cs.virginia.edu/~lab2q/lesson_1/

http://www.frihost.com/forums/vt-43909.html&highlight=modpython

http://www.frihost.com/forums/vt-43006.html&highlight=modpython
eznet
I am still having some difficulty. I am not exactly sure what the problem is so I will give the code first:
Code:

#!/usr/bin/python

# Print the required header that tells the browser how to render the text.
print "Content-Type: text/plain\n\n"

# Define a global variable gender = "female".
gender = "female"

# Define a function.
def display_vars (first_name, last_name, age):
print (first_name, last_name, age, gender)

# Define a function that calls 'display_vars' with intended local 'gender'.
def go_display():
gender = "male"
display_vars("John", "Smith", 22)

# Call the function 'go_display'.
go_display()

I cannot find any syntax errors and am clueless why this will not work. My file is at http://eznet.frih.net/cgi-bin/pythonTest.cgi . Any help is appriciated as I am at a standstill. When I cannot even get simple functions up and running...well I can't really do much at all....

Thanks in advance!
deepak
I've implemented a huge project in python on frihost and I am very thankful to them that they are still hosting ti in free with a quality of service.

eznet wrote:
I am still having some difficulty. I am not exactly sure what the problem is so I will give the code first:
Code:

#!/usr/bin/python

# Print the required header that tells the browser how to render the text.
print "Content-Type: text/plain\n\n"

# Define a global variable gender = "female".
gender = "female"

# Define a function.
def display_vars (first_name, last_name, age):
print (first_name, last_name, age, gender)

# Define a function that calls 'display_vars' with intended local 'gender'.
def go_display():
gender = "male"
display_vars("John", "Smith", 22)

# Call the function 'go_display'.
go_display()



Your code has no syntatical error, just make sure your CGI file is having 755 permission. http://deepak.frih.net/cgi-bin/test_python.cgi Remeber to change permission .. because it need to be executed.

Best!
eznet
Thanks for the interest.

Permissions are set. Checked that first Very Happy

Odd thing is is that I can get simple things to work like
Code:

gender="female"
print gender


Just not the function I previously listed (as well as other functions)... Still confused.
deepak
eznet wrote:
Thanks for the interest.

Permissions are set. Checked that first Very Happy

Odd thing is is that I can get simple things to work like
Code:

gender="female"
print gender


Just not the function I previously listed (as well as other functions)... Still confused.


I execucted that function in my account its not problem .. running fine and all legal function would work with frihost. I created a whole web based IDE for Python in python .. around 1000 line of python code with Javascript & HTML. Every legal syntax would executed. So try to learn python hard.
eznet
Hmmm... odd... and you entered it just like this (copy paste)? I am on server 2 and it is not working for me.... confused... still tryin tho.
deepak
Yeah, i copy and paste, moreover I'm also on server II see this --> its running fine http://deepak.frih.net/cgi-bin/test_python.cgi
eznet
Ha! I do not know what it is that has changed but it is now working for me. I have not changed my script since it wasnt working. reguardless, I am happy to have it working on here.. thanks for your interest.
deepak
Nothing special just few steps.

1. Check Permissions
2. Turn on Debugging, it'll really help if you are getting some error.
SurBiff
thanks alot for the quite little tutorial you have there Smile
i tried it out and here are the result

i've imported time and took <time.ctime()> because i wanted to see if it really whas that simple Smile

i've done manny things in python, both GUI's and consolled programs. but im still at the beginning level (i think), so this is going to be a funny experience.

PS.
why the <print 'Content: bla bla'>
what does that do? and can i change that so that the text behaves different?
deepak
When you are executing any script via CGI, then HTTP server isnt handling your requests, so you've to tell the Browser that this output must be fetched via HTTP/GET.

No, you cant change its a STANDARD line that tells browser that its http output.
velcrobandits
Im very excited about running python programs on this server. Im currently having a lot of problems running cgi python on two paid hosting sites that i use, and im not exactly a python newb. I set up on two of my own computers, but I couldnt get it running on those two sites. Anyway, that IDE thing sounds like a really cool project, can I check it out? im currently trying to write a web game in python.
eznet
If you are new to FriHost, I do not think that you will be disappointed in this place on any level. Bondings is THE MAN! I have paid budget host in the past and felt it in the service but FriHost blows the away, hands down - and for free no less!
deepak
Yeah sure, Frihost provide excellent service all free. If wish if they could start RoR on their server.

velcrobandits, I PM you IDE link. As i dont have shell access on frishot I couldnt make a chrootjail and permission and excutions via pyIDE is a bit problem right now. But still you can have decent idea if you run basic syntax.
spam
Quote:
2. Turn on Debugging, it'll really help if you are getting some error


how-what-where is debugging? is this on the server? sounds useful, just got my first mini-script working but bound do have problems later on Smile
deepak
@spam: see this http://www.frihost.com/forums/vt-53859.html
Related topics
Language
Does frihost still support Python for CGI programming
python script???
Python beginners help!
A Simple Python Dynamic Page
Python via CGI - A personal triumph
How to debug Python CGI errors
mod_python outside cgi-bin
Python
Decent apache and mod_python anyone?
Python CGI script 500 error
upload file to server using html form and CGI C
Python Standard Lib HTTP Server or Apache?
Tutorial needed for python cgi
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.