domainLookup.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
  2. import time, json, os, sys, re, socket
  3. # Connecting to RPC
  4. def initRpc(config):
  5. """Initialize Namecoin RPC"""
  6. rpc_data = {
  7. 'connect': '127.0.0.1',
  8. 'port': '8336',
  9. 'user': 'PLACEHOLDER',
  10. 'password': 'PLACEHOLDER',
  11. 'clienttimeout': '900'
  12. }
  13. try:
  14. fptr = open(config, 'r')
  15. lines = fptr.readlines()
  16. fptr.close()
  17. except:
  18. return None # Or take some other appropriate action
  19. for line in lines:
  20. if not line.startswith('rpc'):
  21. continue
  22. key_val = line.split(None, 1)[0]
  23. (key, val) = key_val.split('=', 1)
  24. if not key or not val:
  25. continue
  26. rpc_data[key[3:]] = val
  27. url = 'http://%(user)s:%(password)s@%(connect)s:%(port)s' % rpc_data
  28. return url, int(rpc_data['clienttimeout'])
  29. # Either returns domain's address or none if it doesn't exist
  30. # Supports subdomains and .bit on the end
  31. def lookupDomain(domain):
  32. domain = domain.lower()
  33. #remove .bit on end
  34. if domain[-4:] == ".bit":
  35. domain = domain[0:-4]
  36. #check for subdomain
  37. if domain.find(".") != -1:
  38. subdomain = domain[0:domain.find(".")]
  39. domain = domain[domain.find(".")+1:]
  40. else:
  41. subdomain = ""
  42. try:
  43. domain_object = rpc.name_show("d/"+domain)
  44. except:
  45. #domain doesn't exist
  46. return None
  47. domain_json = json.loads(domain_object["value"])
  48. try:
  49. domain_address = domain_json["zeronet"][subdomain]
  50. except:
  51. #domain exists but doesn't have any zeronet value
  52. return None
  53. return domain_address
  54. # Loading config...
  55. # Check whether platform is on windows or linux
  56. # On linux namecoin is installed under ~/.namecoin, while on on windows it is in %appdata%/Namecoin
  57. if sys.platform == "win32":
  58. namecoin_location = os.getenv('APPDATA') + "/Namecoin/"
  59. else:
  60. namecoin_location = os.path.expanduser("~/.namecoin/")
  61. # Initialize rpc connection
  62. rpc_auth, rpc_timeout = initRpc(namecoin_location + "namecoin.conf")
  63. rpc = AuthServiceProxy(rpc_auth, timeout=rpc_timeout)