![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
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.
|
|
|
|
|
|
#2 |
|
Programmer
Join Date: Aug 2005
Posts: 66
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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). |
|
|
|
|
|
|
#4 | |
|
Programming Guru
![]() |
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:
Last edited by Sane; Dec 15th, 2005 at 8:11 PM. |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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() |
|
|
|
|
|
#6 |
|
Professional Programmer
|
Awsomeness, thanks guys.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
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; |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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 ![]() |
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|