Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1165

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Simple CGI dice roller. Posted by GAZ082 on 29 Sept 2003 at 9:11 PM
I get an error:
Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\Documents and Settings\EKIXHOO\Desktop\Dados\Motor.py", line 10, in ?
nombre = formulario['nombre'].value
File "C:\Python22\lib\cgi.py", line 550, in __getitem__
raise KeyError, key
KeyError: nombre

And here is the little script:

#!/usr/bin/python2

import cgitb; cgitb.enable()
import string, random, smtplib, cgi

print 'Content-type: text/html'
print

formulario = cgi.FieldStorage()
nombre = formulario['nombre'].value
correo = formulario['correo'].value
dado = formulario['dado'].value

valores = dado.split('d')
numero = int(valores[0])
caras = int(valores[1])

if numero <= 20 and numero >= 1 and caras <= 100 and caras > 1:

random.seed()
tiradas = []

for i in range(numero):
tiradas.append(random.randint(1,caras))

secuencia = str(tiradas)
secuencia = secuencia.strip('[')
secuencia = secuencia.strip(']')

mensaje = nombre + '(' + dado + '): ' + secuencia

servidor = smtplib.SMTP('localhost')
servidor.set_debuglevel(1)
servidor.sendmail('gaz082@yahoo.com.ar', correo, mensaje)
servidor.quit()

print '<h3>Tirada enviada a ' + correo + '.</h3>'
print 'No digo que sacaste para agregar misterio...'
print 'Gracias por usarme!.'
else:
print 'Error, error, ahh, ahh!, error! Mantengamos la calma...'
print '0 < Dados <= 20 y 1 < Caras <= 100 ?'
print 'Pulsa retroceder, verifica esas condiciones e intenta de nuevo.'

Ive got the form in another html file with the action="file.py or .cgi" stuff, post mode.

Thanks for any help that you could give me.

Report
Re: Simple CGI dice roller. Posted by infidel on 30 Sept 2003 at 7:30 AM
: I get an error:
: Traceback (most recent call last):
: File "<string>", line 1, in ?
: File "C:\Documents and Settings\EKIXHOO\Desktop\Dados\Motor.py", line 10, in ?
: nombre = formulario['nombre'].value
: File "C:\Python22\lib\cgi.py", line 550, in __getitem__
: raise KeyError, key
: KeyError: nombre
:
: And here is the little script:
:
: #!/usr/bin/python2
: 
: import cgitb; cgitb.enable()
: import string, random, smtplib, cgi
: 
: print 'Content-type: text/html'
: print
: 
: formulario = cgi.FieldStorage()
: nombre = formulario['nombre'].value
: correo = formulario['correo'].value
: dado = formulario['dado'].value
: 
: valores = dado.split('d')
: numero = int(valores[0])
: caras = int(valores[1])
: 
: if numero <= 20 and numero >= 1 and caras <= 100 and caras > 1:
: 
:     random.seed()
:     tiradas = []
: 
:     for i in range(numero):
:         tiradas.append(random.randint(1,caras))
: 
:     secuencia = str(tiradas)
:     secuencia = secuencia.strip('[')
:     secuencia = secuencia.strip(']')
: 
:     mensaje = nombre + '(' + dado + '): ' + secuencia
: 
:     servidor = smtplib.SMTP('localhost')
:     servidor.set_debuglevel(1)
:     servidor.sendmail('gaz082@yahoo.com.ar', correo, mensaje)
:     servidor.quit()
: 
:     print '<h3>Tirada enviada a ' + correo + '.</h3>'
:     print 'No digo que sacaste para agregar misterio...'
:     print 'Gracias por usarme!.'
: else:
:     print 'Error, error, ahh, ahh!, error! Mantengamos la calma...'
:     print '0 < Dados <= 20 y 1 < Caras <= 100 ?'
:     print 'Pulsa retroceder, verifica esas condiciones e intenta de nuevo.'
: 

: Ive got the form in another html file with the action="file.py or .cgi" stuff, post mode.
:
: Thanks for any help that you could give me.

It looks like your form data doesn't include a nombre key.

Try nombre = formulario.getvalue('nombre','') to return a default empty string if there's no nombre field.


infidel

Report
Re: Simple CGI dice roller. Posted by GAZ082 on 30 Sept 2003 at 10:06 AM
I do have the field nombre in the form. I tried with what you say:

#!/usr/bin/python2

import cgitb; cgitb.enable()
import string, random, smtplib, cgi

print 'Content-type: text/html'
print

formulario = cgi.FieldStorage()
nombre = formulario.getvalue('nombre','') #Here is the thing you said.
correo = formulario['correo'].value
dado = formulario['dado'].value
...


And now i get the same error but with the next variable, correo. Perhaps my form is wrong?. Check its code:

<form action="Motor.py" method="post">
...
<input type="text" name="nombre" size="20" maxlength="30" value="dsdsdsd">
...
<input type="text" name="correo" size="20" maxlength="40" value="dsdsd">
...
<input type="text" name="dado" size="6" maxlength="6" value="#d#">
<input type="image" border="0" name="Tirar" src="boton.gif" width="18" height="17" align="middle">
...
</form>

Report
Re: Simple CGI dice roller. Posted by infidel on 30 Sept 2003 at 10:31 AM
: I do have the field nombre in the form. I tried with what you say:
:
:
: #!/usr/bin/python2
: 
: import cgitb; cgitb.enable()
: import string, random, smtplib, cgi
: 
: print 'Content-type: text/html'
: print
: 
: formulario = cgi.FieldStorage()
: nombre = formulario.getvalue('nombre','') #Here is the thing you said.
: correo = formulario['correo'].value
: dado = formulario['dado'].value
: ...
: 

:
: And now i get the same error but with the next variable, correo. Perhaps my form is wrong?. Check its code:
:
:
: <form action="Motor.py" method="post">
: ...
: <input type="text" name="nombre" size="20" maxlength="30" value="dsdsdsd">
: ...
: <input type="text" name="correo" size="20" maxlength="40" value="dsdsd">
: ...
: <input type="text" name="dado" size="6" maxlength="6" value="#d#">
: <input type="image" border="0" name="Tirar" src="boton.gif" width="18" height="17" align="middle">
: ...
: </form>
: 


I don't know what to tell you, I put this script:

#!/usr/bin/python

import cgitb; cgitb.enable()
import cgi

print 'Content-type: text/plain\n'
print

formulario = cgi.FieldStorage()
nombre = formulario['nombre'].value
correo = formulario['correo'].value
dado = formulario['dado'].value

print nombre, correo, dado


... in my cgi-bin and was able to execute it just fine using your form. What version of Python are you using?


infidel

Report
Re: Simple CGI dice roller. Posted by GAZ082 on 30 Sept 2003 at 11:06 AM
2.2



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.