brand new to python

Bengt Richter bokr at oz.net
Sun Mar 13 21:24:41 CET 2005


On 13 Mar 2005 11:22:54 -0800, richard.hubbell at gmail.com wrote:

>I am sure this is old news, the syntax of python is crazy to me.
>There I said it, I'm sure I'll get over it or around it.
>
>I was trying to make a whois script work and was unable.
>
>May be someone with lint-like eyes can tell what's wrong.
I haven't tried
>
>Using xemacs I had hoped that python mode would do more
>for syntax problems, maybe I'm just not using python mode
>correctly in xemacs??
>
>./whois.py yahoo.com
>  File "./whois.py", line 117
>    class DomainRecord:
>    ^
>SyntaxError: invalid syntax

Yeah, that's not terribly informative. But notice that all your try/except pairs aren't lined up.
The second except is at the same indentation as the first below the second try. Multiple excepts
are legal under a try, but in your case it leaves the outer (first) try without any matching-indent except.

After fixing that, you may have to join some broken lines, if your post is accurately reflecting your source.

[...]
>  ## try until we are connected
>
>  while s == None:
>    try:
The above try has no matching except

>      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>      s.setblocking(0)
>      try:
>        s.connect(whoisserver, 43)
>      except socket.error, (ecode, reason):
>        if ecode in (115, 150):
>          pass
>        else:
>          raise socket.error, (ecode, reason)
>        ret = select.select([s], [s], [], 30)
>
>        if len(ret[1]) == 0 and len(ret[0]) == 0:
>          s.close()
>          raise TimedOut, "on connect "
>        s.setblocking(1)
>
>      except socket.error, (ecode, reason):
The above except is at the same indentation as the previous one,
which is legal syntax, but you probably meant it to match the first try?

>        print ecode, reason
>        time.sleep(10)
>        s = None
>
>        s.send("%s \n\n" % domainname)
>        page = ""
>        while 1:
>          data = s.recv()
>          if not data: break
>
>          page = page + data
>
>          s.close()
>
>          if string.find(page, "No match for") != -1:
>            raise NoSuchDomain, domainname
>
>          if string.find(page, "No entries found") != -1:
>            raise NoSuchDomain, domainname
>
>          if string.find(page, "no domain specified") != -1:
>            raise NoSuchDomain, domainname
>
>          if string.find(page, "NO MATCH") != -1:
>            raise NoSuchDomain, domainname
>      return page
Careful with the above return's indentation after you fix the try/excepts.
>
>##
>##
>----------------------------------------------------------------------
I've heard pychecker (http://pychecker.sourceforge.net/) catches a lot of stuff.
Maybe it would help you. Having lint-eyes, I haven't yet tried it, though it
would probably save me some time ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list