用Python操作iis

这篇博客介绍了如何使用Python的win32com.client模块来操作IIS,包括创建新网站、获取网站编号、停止和启动网站、删除网站等方法。示例代码演示了创建一个名为'test.com'的网站并进行启停操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import win32com.client

"""
这个模块包含了所有的我们服务器需要的一些工具库.
比如iis操作,系统用户管理,文件系统操作等等.
"""
class IISLib:
    """
    用于操作IIS.
    方法包括创建站点,删除站点,修改站点信息等等
    """
    def __init__(self):
        """
        初始化类.
            创建iis管理对象
        有一点是需要注意的,那就是所有的siteName参数表示的都是ServerComment的值
        """
        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 smallNum == int(num):
                smallNum += 1
        return smallNum

    def createNewWebSite(self, siteName, ServerBindings, AnonymousUserName, AnonymousUserPass, 
            Path, ServerAutoStart = 1, AccessFlags=513, AppIsolated = 2):
        """
        创建一个新的站点.需要的参数比较的多.
        """
        siteNum = self.getWebSiteNum(siteName)
        if siteNum:
            # 已经开通了.
            pass
        else:
            siteNum = self.getNewSiteID()
            www = self.iis.Create("IIsWebServer", siteNum)
            www.SetInfo()

            www.ServerComment = siteName
            www.ServerBindings = ServerBindings
            www.ServerAutoStart = ServerAutoStart
            www.AnonymousUserName = AnonymousUserName
            www.AnonymousUserPass = AnonymousUserPass
            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):
        """
        删除网站
        siteName参数为网站名称
        """
        siteNum = self.getWebSiteNum(siteName)
        if siteNum:
            self.iis.Delete("IIsWebServer", siteNum)
            self.iis.SetInfo()
           
    def stopWebSite(self, siteName):
        """
        停止网站
        """
        siteNum = self.getWebSiteNum(siteName)
        if siteNum:
            str = "IIS://localhost/w3svc/%s" % (siteNum,)
            www = win32com.client.GetObject(str)
            www.Stop()

    def startWebSite(self, siteName):
        """
        Start the siteName website.
        """
        siteNum = self.getWebSiteNum(siteName)
        if siteNum:
            str = "IIS://localhost/w3svc/%s" % (siteNum,)
            www = win32com.client.GetObject(str)
            www.Start()

class Test:
    """
    仅仅用于测试程序,没有其他目的
    """
    def __init__(self):
        """
        初始化程序
        """
        pass

    def hello(self):
        pass
       
if __name__ == "__main__":
    iis = IISLib()
    iis.createNewWebSite('test.com', '192.168.0.47:8881:www.test.com', 'wuhy', 'hello', r'e:/test')
    print "Created the site"
    iis.stopWebSite('test.com')
    #iis.deleteWebSite('test.com')
    print "Deleted the site"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值