tags:

views:

395

answers:

5

Hi all, here is my problem... I have a python script that, once executed from command line, performs the needed operations and exit. If, during the execution, the program is not able to perform a choice, he prompts the user and asks them to take a decision ! Now I have to implement a web interface, and here comes the problems ... I created an htm file with a simple form that, once the user "submits" he passes the parameters to a cgi script that contains just one line and runs my python program ! And seems to work ... My question is : if it happens that the program needs to ask the user for a choice, how can I return this value to my python script ! To prompt the user for a choice I need to create a webpage with the possible choices ... Does anybody know how can I open a webpage with python ? The second and most important question is: how can I return a value from a web page to my "original" python module ?? In python I would simply make a

return choice

but with a web page I have no idea how to do it !!

Recap: 1. Starting from a web page, I run a cgi script ! Done

  1. This CGI script runs my python program... Done

  2. If the program is not able to take a decision,

    3a create a web page with the possible choices I can do it

    3b display the created web page ????????

    3c return the responce to the original pyhon module ????????

thanks in advance

A: 

Web pages don't return values, and they aren't programs - a web page is just a static collection of HTML or something similar which a browser can display. Your CGI script can't wait for the user to send a response - it must send the web page to the user and terminate.

However, if the browser performs a second query to your CGI program (or a different CGI program) based on the data in that page, then you can collect the information that way and continue from that point.

Kylotan
+1  A: 

I think you could modify the Python script to return an error if it needs a choice and accept choices as arguments. If you do that, you can check the return value from your cgi script and use that to call the python script appropriately and return the information to the user.

Is there a reason why you can't call the python script directly? I suspect you'd end up with a neater implementation if you were to avoid the intermediate CGI.

What webserver are you using? What cgi language? Perl maybe?

Jon Cage
A: 

Probably easier if you write your cgi in python then call your python script from the cgi script.

Update your script to separate the UI from the logic. Then it should be relatively easy to interface your script with the (python) cgi script.

For python cgi reference: Five minutes to a Python CGI

monkut
+6  A: 

"Does anybody know how can I open a webpage with python ? The second and most important question is: how can I return a value from a web page to my "original" python module ??"

This is all very simple.

However, you need to read about what the web really is. You need to read up on web servers, browsers and the HTTP protocol.

Here's the golden rule: A web server responds to HTTP requests with a web page.

The second part of that rules is: A Request is a URL and a method (GET or POST). There's more to a request, but that's the important part.

That's all that ever happens. So, you have to recast your use case into the above form.

Person clicks a bookmark; browser makes an empty request (to a URL of "/") and gets a form.

Person fills in the form, clicks the button; browser POST's the request (to the URL in the form) and gets one of two things.

  • If your script worked, they get their page that says it all worked.

  • If your script needed information, they get another form.

Person fills in the form, clicks the button; browser POST's the request (to the URL in the form) and gets the final page that says it all worked.

You can do all of this from a "CGI" script. Use mod_wsgi and plug your stuff into the Apache web server.

Or, you can get a web framework. Django, TurboGears, web.py, etc. You'll be happier with a framework even though you think your operation is simple.

S.Lott
A: 

http://docs.python.org/library/cgihttpserver.html

I think first off you need to separate your code from your interface. When you run a script, it spits out a page. You can pass arguments to it using url parameters. Ideally you want to do your logic, and then pass the results into a template that python prints to the cgi.

meunierd