Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 15th, 2005, 11:29 AM   #1
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
Python for web?

I know that a lot of people use Python for web scripting and server side programming, but I never knew how. Can I just take a .py file and stick it on the server? Should I put it in a CGI-BIN? Is there an apache module? Do I need a specific config or any unix system with python on there suffice? Any advice is appreciated.
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Dec 15th, 2005, 1:32 PM   #2
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
Well, from what I'm used to doing, you do need to place the script into a proper cgi-bin folder. You obvoiusly need to install python on whatever server your running. (Or at least wherever the python script is to be run.) As long as python is properly installed you should be able to use any Unix box.

Make sure that you have your context type set in the main module otherwise the script will blow up with some funkey "internal error 500". Lika so...

import sys

print "Content-type: text/html\n"

....
rest of program

Make sure you have some sort of return after the content-type. I made that boo boo just yesterday. :p

That's pretty much the bair bones of how to the the python file working on the web server side. The experts out there will have to forgive my very vauge discription. Feel free to add/correct.
MegaArcon is offline   Reply With Quote
Old Dec 15th, 2005, 4:38 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Lich
I know that a lot of people use Python for web scripting and server side programming, but I never knew how. Can I just take a .py file and stick it on the server? Should I put it in a CGI-BIN? Is there an apache module? Do I need a specific config or any unix system with python on there suffice? Any advice is appreciated.
Well, there are several ways you can go about this. I'll list some of them below:

1. An executable CGI script. If your server is set up correctly, then .py files will be run through the Python interpretor and their output will be displayed as a web page. This method is slow and seldom used.

2. Apache has mod_python, which precompiles Python code and keeps it in memory, significantly speeding up your Python programs. The interface to Apache is quite involved, however.

3. A specialised HTTP server framework. These sorts of servers are becoming more and more popular of late. Ruby on Rails, for instance, uses the Webrick server toolkit by default. Probably the most popular HTTP server framework for Python is CherryPy.

CherryPy's probably the easiest of the lot to develop in, though its also probably the least supported. You may be limited by what your web hosting company offers, but nowadays web hosts are two-a-penny, so you probably shouldn't base your programming decisions around them. There'll always be plenty that cater for your needs.

Also, if you're just experimenting on your own computer, then the situation reverses itself. CherryPy's a darn site easier to install than Apache, and you don't have to worry about getting to grips with Apache page handlers and .htaccess files.

But, if you want serverside scripting on your website, and you're low on cash, then you may have to take what's supported (e.g. PHP).
Arevos is offline   Reply With Quote
Old Dec 15th, 2005, 7:42 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,840
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Thanks Arevos! I'm setting up CherryPy this instant as a solution to learning PHP!

:p


Edit: I'm running in to problems already. It says it can not import "cpg" when I'm running the first example program for Hello World on my computer.

But I can import cherrypy, it's there.

I've tried both methods of installing CherryPy (the latest version), unzipping and running setup.py. Or running the windows installer.

Both had the same error.

I'm using Python 2.4. Any ideas?

Quote:
Originally Posted by dir(cherrypy)
Error, HTTPError, HTTPRedirect, InternalError, InternalRedirect, NotFound, NotReady, RequestHandled, WrongConfigValue, WrongResponseType, WrongUnreprValue, _AttributeDump, _ThreadLocalProxy, __builtins__, __doc__, __file__, __name__, __path__, __version__, _cpcgifs, _cperror, _cphttptools, _cpserver, _cputil, _cpwsgiserver, _sessionDataHolder, _sessionLastCleanUpTime, _sessionLockDict, codecoverage, config, datetime, expose, lib, local, log, request, response, server, serving, session, sessionfilter, sys, threadData, types, urllib

Last edited by Sane; Dec 15th, 2005 at 8:11 PM.
Sane is offline   Reply With Quote
Old Dec 16th, 2005, 1:54 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I assume you're following the tutorial? It's a little out of date. A new version of CherryPy has recently been released that makes some changes (there's a disclaimer at the top of the page).

The main difference is that the cpg module has been gotten rid of. In place of cpg just use cherrypy. Here's a hello world program that should work with the new version:
import cherrypy

class HelloWorld:
	@cherrypy.expose
	def index(self):
		return "Hello world!"

cherrypy.root = HelloWorld()
cherrypy.server.start()
Arevos is offline   Reply With Quote
Old Dec 16th, 2005, 10:05 AM   #6
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
Awsomeness, thanks guys.
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Dec 16th, 2005, 12:56 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,840
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Thanks Arevos. And I've read the whole tutorial, and I haven't found anywhere how to actually put the darn thing on my website. I have it fully functioning at http://127.0.0.1:8080/index/ but of course that's only accessable by me.

Do I replace 127.0.0.1 with my internet IP, and forward my website to that address? And then I would have to port forward because I have a router. x_x;
Sane is offline   Reply With Quote
Old Dec 16th, 2005, 1:59 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,840
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Okay, I guess I was right. ^_^

http://64.230.63.162:8080/viewscores?userID=3

I replaced 127.0.0.1 with my IP. And portforwarded 8080 from my router.

Now how do I make it so my website forwards everything to the domain mentioned above like so?

http://www.insert_website_domain.com...cores?userID=3

Last edited by Sane; Dec 16th, 2005 at 2:19 PM.
Sane is offline   Reply With Quote
Old Dec 16th, 2005, 3:02 PM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Sane
Okay, I guess I was right. ^_^

http://64.230.63.162:8080/viewscores?userID=3

I replaced 127.0.0.1 with my IP. And portforwarded 8080 from my router.

Now how do I make it so my website forwards everything to the domain mentioned above like so?

http://www.insert_website_domain.com...cores?userID=3
Well, generally speaking any good web address reseller will allow you to bind a DNS hostname to an IP address. There are also sites like Dyndns, which will give you a subdomain (something.dyndns.org) that points to a home IP for free.

However, it might be an idea to get a webserver than supports CherryPy (there's a few listed on this page, and a lot more listed on the Python wiki here), or a virtual private server so you can install CherryPy for yourself. If you look around, you can probably find something reasonably cheap, but not, I suspect, as cheap as you could get a server with just PHP.

The advantage of an external server is bandwidth and always being on. But if you can spare the bandwidth and have a server you can keep always on, then by all means, host it yourself
Arevos is offline   Reply With Quote
Old Dec 16th, 2005, 3:46 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,840
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
All the user's data is stored on the server though, and I might need to make changes to that data every once in a while. Also, it would be more secure on my computer because I wouldn't have to worry about losing it.

I'm going to look into that Dyndns, thanks.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC