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. 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 #!/usr/bin/env python #usage: %(progname)s [--test] [domain...] #Version: %(version)s #Contacts the NetworkSolutions whois database for each domain and displays #the result. #public methods: # def whois(domainname, whoisserver=None, cache=0) # raises NoSuchDomain if the domain doesn't exist. # return the result of contacting NetworkSolutions #def ParseWhois(page) # returns a DomainRecord object that contains a parsed #version of the information contained in 'page'. #class DomainRecord: # self.domain -- name of the domain #self.domainid -- domainid for this domain # self.created -- date in which the domain was created # self.lastupdated -- date in which the domain was last updated. # self.expires -- date in which the domain expires # self.databaseupdated -- date in which the database was last updated. # self.servers -- list of (hostname, ip) pairs of the # nameservers. # self.registrant -- name of the person or organization that # registered the domain. # self.registrant_address -- address of the person or organization that # registered the domain. # self.contacts -- dictionary of contacts### ## # #""" #_version = "1.0" import os, sys, string, time, getopt, socket, select, re NoSuchDomain = "NoSuchDomain" def whois(domainname, whoisserver=None, cache=0): if whoisserver is None: whoisserver = "whois.networksolutions.com" if cache: fn = "%s.dom" % domainname if os.path.exists(fn): return open(fn).read() page = _whois(domainname, whoisserver) if cache: open(fn, "w").write(page) return page def _whois(domainname, whoisserver): s = None ## try until we are connected while s == None: try: 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): 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 ## ## ---------------------------------------------------------------------- ## class DomainRecord: def __init__(self, domain): self.domain = domain self.domainid = None self.created = None self.lastupdated = None self.expires = None self.databaseupdated = None self.servers = None self.registrant = None self.registrant_address = None self.contacts = {} def __str__(self): return "%s (%s): (created:%s) (lastupdated:%s) (databaseupdated:%s) (servers:%s) (registrant:%s) (address:%s) (contacts:%s)" % (self.domain, self.domainid, self.created, self.lastupdated, self.databaseupdated, self.servers, self.registrant, repr(self.registrant_address), self.contacts) ## ## ---------------------------------------------------------------------- ## def _ParseContacts_RegisterCOM(page): contactDict = {} parts = re.split("((?:(?:Administrative|Billing|Technical|Zone) Contact,?[ ]*)+:)\n", page) contacttypes = None for part in parts: if string.find(part, "Contact:") != -1: if part[-1] == ":": part = part[:-1] contacttypes = string.split(part, ",") continue part = string.strip(part) if not part: continue record = {} lines = string.split(part, "\n") m = re.search("(.+) ([EMAIL PROTECTED])", lines[0]) if m: record['name'] = string.strip(m.group(1)) record['handle'] = None record['email'] = string.lower(string.strip(m.group(2))) flag = 0 phonelines = string.strip(lines[1]) record['phone'] = phonelines record['address'] = [] for contacttype in contacttypes: contacttype = string.lower(string.strip(contacttype)) contacttype = string.replace(contacttype, " contact", "") contactDict[contacttype] = record return contactDict def ParseWhois_RegisterCOM(page): m = re.search("Domain Name: (.+)", page) domain = m.group(1) rec = DomainRecord(domain) m = re.search("Record last updated on.*: (.+)", page) if m: rec.lastupdated = m.group(1) m = re.search("Created on.*: (.+)", page) if m: rec.created = m.group(1) m = re.search("Expires on.*: (.+)", page) if m: rec.expires = m.group(1) m = re.search("Registrant:", page) if m: i = m.end() m = re.search("\n\n", page[i:]) j = m.start() registrant = string.strip(page[i:i+j]) lines = string.split(registrant, "\n") registrant = [] for line in lines: line = string.strip(line) if not line: continue registrant.append(line) rec.registrant = registrant[0] rec.registrant_address = string.join(registrant[1:], "\n") m = re.search("(.+) \((.+)\)$", rec.registrant) if m: rec.registrant = m.group(1) rec.domainid = m.group(2) m = re.search("Domain servers in listed order:\n\n", page) if m: i = m.end() m = re.search("\n\n", page[i:]) j = m.start() servers = string.strip(page[i:i+j]) lines = string.split(servers, "\n") servers = [] for line in lines: parts = string.split(string.strip(line)) if not parts: continue servers.append(parts[0], parts[1]) rec.servers = servers m = re.search("((?:(?:Administrative|Billing|Technical|Zone) Contact,?[ ]*)+:)\n", page) if m: i = m.start() m = re.search("Domain servers in listed order", page) j = m.start() contacts = string.strip(page[i:j]) rec.contacts = _ParseContacts_RegisterCOM(contacts) return rec ## ## ---------------------------------------------------------------------- ## def _ParseContacts_NetworkSolutions(page): contactDict = {} parts = re.split("((?:(?:Administrative|Billing|Technical|Zone) Contact,?[ ]*)+:)\n", page) contacttypes = None for part in parts: if string.find(part, "Contact:") != -1: if part[-1] == ":": part = part[:-1] contacttypes = string.split(part, ",") continue part = string.strip(part) if not part: continue record = {} lines = string.split(part, "\n") m = re.search("(.+) \((.+)\) ([EMAIL PROTECTED])", lines[0]) if m: record['name'] = string.strip(m.group(1)) record['handle'] = string.strip(m.group(2)) record['email'] = string.lower(string.strip(m.group(3))) flag = 0 addresslines = [] phonelines = [] for line in lines[1:]: line = string.strip(line) if not line: flag = 1 continue if flag == 0: addresslines.append(line) else: phonelines.append(line) record['phone'] = string.join(phonelines, "\n") record['address'] = string.join(addresslines, "\n") for contacttype in contacttypes: contacttype = string.lower(string.strip(contacttype)) contacttype = string.replace(contacttype, " contact", "") contactDict[contacttype] = record return contactDict def ParseWhois_NetworkSolutions(page): m = re.search("Domain Name: (.+)", page) domain = m.group(1) rec = DomainRecord(domain) m = re.search("Record last updated on (.+)\.", page) if m: rec.lastupdated = m.group(1) m = re.search("Record created on (.+)\.", page) if m: rec.created = m.group(1) m = re.search("Database last updated on (.+)\.", page) if m: rec.databaseupdated = m.group(1) m = re.search("Registrant:", page) if m: i = m.end() m = re.search("\n\n", page[i:]) j = m.start() registrant = string.strip(page[i:i+j]) lines = string.split(registrant, "\n") registrant = [] for line in lines: line = string.strip(line) if not line: continue registrant.append(line) rec.registrant = registrant[0] rec.registrant_address = string.join(registrant[1:], "\n") m = re.search("(.+) \((.+)\)$", rec.registrant) if m: rec.registrant = m.group(1) rec.domainid = m.group(2) m = re.search("Domain servers in listed order:\n\n", page) if m: i = m.end() m = re.search("\n\n", page[i:]) j = m.start() servers = string.strip(page[i:i+j]) lines = string.split(servers, "\n") servers = [] for line in lines: parts = string.split(string.strip(line)) if not parts: continue servers.append(parts[0], parts[1]) rec.servers = servers m = re.search("((?:(?:Administrative|Billing|Technical|Zone) Contact,?[ ]*)+:)\n", page) if m: i = m.start() m = re.search("Record last updated on", page) j = m.start() contacts = string.strip(page[i:j]) rec.contacts = _ParseContacts_NetworkSolutions(contacts) return rec ## ## ---------------------------------------------------------------------- ## def ParseWhois(page): if string.find(page, "Registrar..: Register.com (http://www.register.com)") != -1: return ParseWhois_RegisterCOM(page) else: return ParseWhois_NetworkSolutions(page) ## ## ---------------------------------------------------------------------- ## def usage(progname): version = _version print __doc__ % vars() def main(argv, stdout, environ): progname = argv[0] list, args = getopt.getopt(argv[1:], "", ["help", "version", "test"]) for (field, val) in list: if field == "--help": usage(progname) return elif field == "--version": print progname, _version return elif field == "--test": test() return for domain in args: try: page = whois(domain) print page except NoSuchDomain, reason: print "ERROR: no such domain %s" % domain if __name__ == "__main__": main(sys.argv, sys.stdout, os.environ) -- http://mail.python.org/mailman/listinfo/python-list