tags:

views:

281

answers:

8

Most likely it's a dumb question for those who knows the answer, but I'm a beginner, and here it goes:

I have a Python script which I run in a command-line with some parameter, and it prints me some results. Let's say results are some HTML code.

I never done any Python programming for web, and couldn't figure it out... I need to have a page (OK, I know how to upload files to a server, Apache is running, Python is installed on the server...) with an edit field, which will accept that parameter, and Submit button, and I need it to "print" the results on a web page after the user submitted a proper parameter, or show any output that in a command-line situation are printed.

I've read Dive Into Python's chapters about "HTML Processing" and "HTTP Web Services", but they do not describe what I'm looking for.

If the answer isn't short, I would very much appreciate links to the more relevant stuff to read or maybe the key words to google for it.

+1  A: 

Sorry to say it, but HTTP web services are what you're looking for. Think of it this way - you put a form on a webpage, and a submit button. That goes to a web service that accepts the request and it chews on that request, and returns a response - which is a page of html. You never have to actually save the html, it is always dynamically generated, but it's a valid page of html nonetheless.

Look at django http://www.djangoproject.com/ for a high quality python based web service toolkit.

The first step to understanding the request / response idiom is to think of the request as a specifically-formatted (defined by CGI - that is, it's a GET or POST HTML action) set of command line parameters, and the response as specifically-formatted output (HTML) that gets sent, not to stdout, but across "the wire" to some browser. Sending the request reaches out across the wire and executes the script with some parameters, and you recieve back the output - your formatted HTML page.

Matt
Django sure seems like overkill for this type of problem...
Hortitude
+1 to the commenter that says "overkill".
Warren P
+4  A: 

For such a simple task, you probably don't need more than CGI. Luckily Python has a built-in cgi module which should do what you want.

Or you could look into some of the minimal web frameworks, such as web.py.

Daniel Roseman
+2  A: 

Sounds like you just need to enable CGI on apache, which pretty much will redirect your output to the webserver output.

Python does have CGI library you may take a look at it.

Here's an essay by Guido ( a bit dated )

And an interactive instruction that looks promising.

p.s.

Perhaps you may also want to see Google's offering for this Google app engine ( it may not be what you want though )

OscarRyz
There are lots of examples out there too. And lots of frameworks. I don't know if anybody uses this still, but I used to use the "twisted" framework for web app development in Python. Lots of people use lots of things, including Django. Where to start? Probably where Oscar says to start.
Warren P
A: 

I agree completely with Matt -- Django is what you want. Django provides a complete solution, and really wonderful documentation that will help you get something going. If your application grows and becomes more complicated, Django can still work; Django works even for very large and very complicated web sites.

Take a look at the free book, The Django Book.

Another plus with Django: if you do your web app in Django, you can port it to Google App Engine.

steveha
So vote matt up? Why a second answer.
Warren P
@Warren P, I provided a second answer because I wanted to say some things that Matt did not say. I mean, just compare my answer to his; they are far from identical. I acknowledged that he answered it first because I wasn't trying to trick anyone into upvoting me instead of Matt--I was just trying to be helpful. Perhaps you feel my answer does not contribute anything useful; if so, I suggest you downvote me.
steveha
A serious question for the community here: would it have been better StackOverflow manners to have gone into Matt's answer and started editing it to add my other points to it? So far I haven't edited other answers unless they contained mistakes, or code that needed fixing. It seems wrong to mingle my thoughts with someone else's thoughts without asking and I thought the correct thing was to post my own answer. Was I wrong to do this?
steveha
I think comments should be used for supplying additional reasons why someone's fundamental answer is right. Forking the "django" answer into two or three things just makes gleaning the overall perspective on people's answers harder. In any "What X should I use?" question, I would love it if there was only one answer per product of type X. Just me, or everybody else the same way?
Warren P
In a perfect world, it would be as you say: there would be exactly one answer per unique concept. In the real world, it's messier: several people write a similar answer at the same time, and nobody viciously prunes all answers but one; so it's common to see multiple similar answers. The other problem is that comments are much more limited than answers, here: 0) whatever you write is munged into a single paragraph, which I didn't want for my answer above; 1) you can't use HTML to make a link that says "The Django Book" and takes you to it, you have to just paste in a bare URL.
steveha
Hmmmm. I'm now tempted to suggest a new feature, where multiple answers can be tagged as "docked together", and will display together. When multiple answers are written at the same time, people can tag them as docked together; and in the case where someone wants to basically write a comment but using the richer text field of an answer, he/she can write as an answer but tag it as docked with the original answer.
steveha
I guess I could solve the "munged together into one paragraph" problem by posting the above answer as three comments under Matt's answer, one comment per paragraph. If you feel strongly about this, I'll do it just to make you happier.
steveha
A: 

I've not read DIVE INTO PYTHON, so maybe what I'm saying is redundant. As Daniel said, CGI may work for you. These days this would only the case for simple stuff with a fairly low number of hits. The Python CGI module is documented here. I have always used that module for form processing and just done prints for the output. As best as I've been able to figure out, that's the usual way of doing things, but I have not done a lot of CGI with Python.

You don't say what you're doing, so I'll state what may be obvious: Make sure you're telling the server that you're outputting a web page with your very first output being the content type followed by a blank line. Typically this is done with:

print "Content-Type: text/html"
print

(For Python 2, for 3 you'll need to make your prints into function calls.) After this, you print your html, header, body, etc. tags and the actual content.

PTBNL
A: 

I think Django is overkill. If you are wanting to learn about this stuff, start from something very simple like this:

http://www.islascruz.org/html/index.php/blog/show/Python:-Simple-HTTP-Server-on-python..html

You can put your code where the do_GET is.

gnibbler
A: 

A current method of creating simple (one-of )Python web page server is the wsgiref module.

wsgiref is a reference implementation of the WSGI specification that can be used to add WSGI support to a web server or framework.

See some SO qusetions (http://stackoverflow.com/search?q=%5Bpython%5D+wsgiref) for some code examples and more suggestions.

The wsgiref example is a good place to start:

from wsgiref.simple_server import make_server

def hello_world_app(environ, start_response):
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/plain')] # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return ["Hello World"]

httpd = make_server('', 8000, hello_world_app)
print "Serving on port 8000..."

# Serve until process is killed
httpd.serve_forever()
gimel
wsgi is considered "better" than cgi, primarily due to scalability in production apps, right? CGI is okay for any trivial "less than 10 users" apps out there. And it's not a bad place to start with learning CGI and then moving to wsgi, right?
Warren P
I think the WSGI example is simple enough - an option of similar complexity to CGI.
gimel
+2  A: 

Whose web server? If it is a web server provided by a web hosting company or someone else and you don't have control over it, you need to ascertain in what way they support the use of Python for writing web applications. It isn't enough to know just that they have Python available.

As pointed out by others, is likely that at least CGI scripts which use Python will be supported. CGI however is not really practical for running large Python web frameworks such as Django.

It is possible though that the web server might also support FASTCGI. If that is the case it becomes possible to use such larger Python web frameworks as FASTCGI uses persistent processes where as CGI creates a process for each request, where the overhead of large Python web frameworks generally makes the use of CGI impractical as a result.

If the Apache server is controlled by others using a standalone Python web server such as wsgiref and proxying to it from Apache isn't going to be doable either as you can't setup Apache to do it.

So, find out how use of Python for web applications is supported and work out whether you are just a user of the Apache instance, or whether you have some measure of control of changing its global configuration files and restarting it. This will dictate what you can do and use.

Graham Dumpleton