[Tutor] Re: Re: Translating to Python [perl --> python]

Derrick 'dman' Hudson dman@dman.ddts.net
Tue, 2 Jul 2002 09:30:22 -0500


--WIyZ46R2i8wDzkSu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jul 02, 2002 at 01:59:40PM +0000, Kyle Babich wrote:
| Ok, well then something is still wrong because I'm still getting syntax
| errors that I can't figure out.  Is there a python tutorial that is
| more like cgi101.com (only python)?  (Ie. including source codes, live
| working examples, and different sections for different commands)

http://python.org/doc/current/tut/tut.html

It covers the python language.  Working with CGI is trivial once you
know the language -- just use the same environment variables as always
(found in the 'os.environ' dictionary) and use the cgi.FieldStorage
class to parse form submissions.
=20
| Btw, here is the code again:
|=20
| #!/usr/bin/env python
|=20
| import cgi
|=20
| print "Content-type: text/html\n\n"
|=20
| form =3D cgi.FieldStorage()
  try:
  ^^^^
      c =3D form['c'].value
  ^^^^
| except KeyError :
      c =3D ""
  ^^^^
| content =3D c
| if c =3D=3D 'abc':
      content =3D "abc123"
  ^^^^
| elif c =3D=3D 'def':
      content =3D "def456"
  ^^^^
| else:
      print "error: content failed\n"
  ^^^^

| It says there is an error near except KeyError:

The 'except' keyword can only be used after a 'try:'.  Just like in
perl you can't put "else" all over the place, only after an "if".
Also, you need to indent the blocks of compound statements.  If, in
perl, it would have had curly braces around it, indent it.

| but I've never used python before so I don't know what to change.

Read Guido's tutorial.  It is very easy to read if you already know
the basics of programming (which you do if you've used perl before).

HTH,
-D

--=20

A kindhearted woman gains respect,
but ruthless men gain only wealth.
        Proverbs 11:16
=20
http://dman.ddts.net/~dman/


--WIyZ46R2i8wDzkSu
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0huP4ACgkQO8l8XBKTpRS+vgCgj8XX6uSXGdgtsTty31LSDY2I
S2kAoLEq1uZyHLjv2PQWaWSGvmikTd6k
=GBN1
-----END PGP SIGNATURE-----

--WIyZ46R2i8wDzkSu--