Min första python klass 🙂

[python]
”’
Created on 20 okt 2010
@author: Peter Lindblom
”’
import subprocess
from time import localtime, strftime
import re

class LoopiaDnsSync:
"""A simple loopia dns python module"""
__user = ""
__domains = []
def __init__(self, username, password, domains=[]):
"""Username, Password, optional list of domains"""
self.__user = username + ":" + password
self.__domains = domains

def addDomain(self, domain):
"""Adds a domain to the list of domains and returns current LoopiaDnsSync instance"""
self.__domains.append(domain);
return self

def __getIpAddress(self):
"""Gets your current ip-address"""
outputStr = subprocess.Popen(["curl", "-s", "http://dns.loopia.se/checkip/checkip.php"], stdout=subprocess.PIPE).communicate()[0]
return re.search(’^.*: ([^<]*).*$’, outputStr).group(1)

def __syncADomain(self, user, currentIP, domain):
"""Syncs a domain at a time"""
inputStr = "http://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname=" + domain + "&myip=" + currentIP
outputStr = subprocess.Popen(["curl", "-s", "–user", user, inputStr], stdout=subprocess.PIPE).communicate()[0]
return domain+"="+currentIP+": "+outputStr

def syncDomains(self):
"""Syncs all domains using __syncADomain"""
print __name__+": " + strftime("%a, %d %b %Y %H:%M:%S", localtime())
ipAdress = self.__getIpAddress()
for domain in self.__domains:
print self.__syncADomain(self.__user, ipAdress, domain)

[/python]