Simple CGI dice roller.

I get an error:
Traceback (most recent call last):
File "", line 1, in ?
File "C:Documents and SettingsEKIXHOODesktopDadosMotor.py", line 10, in ?
nombre = formulario['nombre'].value
File "C:Python22libcgi.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('[email protected]', correo, mensaje)
servidor.quit()

print '

Tirada enviada a ' + correo + '.

'
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.

Comments

  • : I get an error:
    : Traceback (most recent call last):
    : File "", line 1, in ?
    : File "C:Documents and SettingsEKIXHOODesktopDadosMotor.py", line 10, in ?
    : nombre = formulario['nombre'].value
    : File "C:Python22libcgi.py", line 550, in __getitem__
    : raise KeyError, key
    : KeyError: nombre
    :
    : And here is the little script:
    : [code]
    : #!/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('[email protected]', correo, mensaje)
    : servidor.quit()
    :
    : print '

    Tirada enviada a ' + correo + '.

    '
    : 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.'
    : [/code]
    : 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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • I do have the field nombre in the form. I tried with what you say:

    [code]
    #!/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
    ...
    [/code]

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

    [code]

    ...

    ...

    ...


    ...

    [/code]
  • : I do have the field nombre in the form. I tried with what you say:
    :
    : [code]
    : #!/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
    : ...
    : [/code]
    :
    : And now i get the same error but with the next variable, correo. Perhaps my form is wrong?. Check its code:
    :
    : [code]
    :
    : ...
    :
    : ...
    :
    : ...
    :
    :
    : ...
    :
    : [/code]

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

    [code]
    #!/usr/bin/python

    import cgitb; cgitb.enable()
    import cgi

    print 'Content-type: text/plain
    '
    print

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

    print nombre, correo, dado
    [/code]

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


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories