Import win32com.client
"" "This module contains all of the tools such as libraries we need iis server operating system user management, file system operations, and so on.." "" Class IISLib: "" "for the operation method includes creating IIS sites. Delete the site, modify site information, etc. "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" " Self.iis = Win32com.client.getObject ('IIS: // localhost / w3svc')
Def getWebsitenum (Self, Sitename): "" "Return The Site's Num. EXP: The Site 'Defaultsite' site number 1 if the site is not in.then return the null value." "" "" "" for www in self.iis: IF www.Class == 'IIsWebServer': if siteName == www.ServerComment: return www.Name return None def getNewSiteID (self):. "" "return the smallest number of the iis web site It's used when to create a new web Site "" "" Sitenums = [] smallnum = 1 for web in self.iis: if Web.class == 'IisWebserver': Sitenums.Append (Web.Name) Sitenums.Sort () for Num in Sitenums: if shortnum == Int (NUM): SmallNum = 1 Return Smallnum
def createNewWebSite (self, siteName, ServerBindings, AnonymousUserName, AnonymousUserPass, Path, ServerAutoStart = 1, AccessFlags = 513, AppIsolated = 2): "" ".. create a new site relatively more required parameters" "" siteNum = self .GetWebsitenum (Sitename) if Sitenum: # has been opened. Pass else: sitenum = self.getnewsiteid () www = self.iis.create ("IisWebserver", sitenum) www.setinfo () www.servercomment = siteename www.serverbindings = ServerBindings www.serverautostart = serverautostart www.anonymoususeusername = anonymoususeusername www.anonymoususeuserpass = anonymoususeuser www.setInfo ()
DIR = www.create ("IisWebVirtualdir", "root") Dir.setInfo ()
dir.AppIsolated = AppIsolated dir.Path = Path dir.AccessFlags = AccessFlags approot = "/ LM / W3SVC /% s / ROOT"% (siteNum,) dir.AppRoot = approot dir.AppFriendlyName = siteName dir.SetInfo ()
Def deleteWebsite (Self, Sitename): "" "Delete Site SiteName Parameters" "" "" "" "" "" "" SITENUM = SELF.GetWebsitenum (Sitename) if Sitenum: Self.Iis.delete ("IisWebserver", Sitenum) self.iis.setInfo ) Def StopWebsite (Self, Sitename): "" "Stop website" "SiteNum = self.GetWebsitenum (Sitename) if Sitenum: str =" IIS: // localhost / w3svc /% s "% (Sitenum,) WWW = Win32com .Client.getObject (STR) www.stop () Def StartWebsite: "" "" "" "" "" "" "SITENUM = SELF.GetWebsitenum (Sitename) if Sitenum: str =" IIS: // localhost / W3SVC /% S "% (Sitenum,) WWW = win32com.client.getObject (str) www.start ()
Class test: "" "Only for test programs, there is no other purpose" "" "DEF __INIT __ (Self):" "" "" "" "" "
DEF HELLO (Self): pass if __name__ == "__main__": IIS = Iislib () Iis.createNewwebsite ('Test.com', '192.168.0.47:8881:www.test.com', 'Wuy', 'Hello ', r'e: / test') Print "created the site" Iis.stopWebsite ('Test.com') # Iis.DeleteWebsite ('Test.com') Print "Deleted the Site"