Ayuda con formularios CGI
Juan Carlos Rodrigo
jrodrigog en gmail.com
Dom Sep 10 21:11:21 CEST 2006
Este es un CGI de ejemplo... Tiene dos paginas, un
formulario y otra en la que te saluda con el nombre
que pusiste. El formulario funciona igual para metodos
GET y POST.
------8<------8<------8<------8<------8<------
#!/bin/env python
import cgi
# Activar debugging cgi
import cgitb; cgitb.enable()
def http_header():
print 'Content-type: text/html'
print
def greet( form ):
print """\
<html><body>
Hola %s
</body></html>
""" % cgi.escape( form[ 'name' ].value )
# Escapar los datos que vienen del form
# antes de mostrarlos es conveniente.
def show_form( form ):
print """\
<html><body>
<form method="POST">
Dime tu nombre:<br>
<input type="text" name="name">
<input type="submit" name="go" value="..">
</form>
</body></html>"""
def main():
# Crear una cabecera http
http_header()
# El formulario es un diccionario
form = cgi.FieldStorage()
# Si existe la variable del form 'go' (Boton)
if form.has_key( 'go' ):
# El usuario pulso el boton, le saludamos
greet( form )
else:
# No se ha pulsado el boton, mostrar form
show_form( form )
if __name__ == "__main__":
main()
------8<------8<------8<------8<------8<------
Con este programa puedes probar ese cgi, llamas
a este script server.py y bajo su directorio creas
un cgi-bin y pones alli el cgi anterior con el
nombre test.py, asegurate de que se puede
ejecutar y arranca el server.py
------8<------8<------8<------8<------8<------
import BaseHTTPServer, CGIHTTPServer
httpd = BaseHTTPServer.HTTPServer(
('localhost', 8080),
CGIHTTPServer.CGIHTTPRequestHandler
)
httpd.serve_forever()
------8<------8<------8<------8<------8<------
lo veras en http://localhost:8080/cgi-bin/test.py
Saludos.
------------ próxima parte ------------
_______________________________________________
Python-es mailing list
Python-es en aditel.org
http://listas.aditel.org/listinfo/python-es
Más información sobre la lista de distribución Python-es