当前位置: 首页>>代码示例>>Python>>正文


Python ServerFactory.service方法代码示例

本文整理汇总了Python中twisted.internet.protocol.ServerFactory.service方法的典型用法代码示例。如果您正苦于以下问题:Python ServerFactory.service方法的具体用法?Python ServerFactory.service怎么用?Python ServerFactory.service使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.internet.protocol.ServerFactory的用法示例。


在下文中一共展示了ServerFactory.service方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from twisted.internet.protocol import ServerFactory [as 别名]
# 或者: from twisted.internet.protocol.ServerFactory import service [as 别名]
    def __init__(self, args):
        # Change working directory, if it exists
        if not os.path.isdir(args.dir):
            print "Path %s does not exist or is not a directory." % args.dir
            sys.exit(-1)
        else:
            os.chdir(args.dir)
        if verbosity > 0:
            print "Working directory: ", args.dir

        # Set path to primary.txt, if not at directory
        if os.path.isdir(args.primary):
            print "Primary.txt must point to a file, not a directory."
            sys.exit(-1)
        else:
            self.primary_txt = args.primary

        self.host = args.ip
        self.port = args.port

        # Create log directory
        try:
            os.makedirs(self.logdir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                print "Could not initialize server.  Does the directory have execute permission?"
                sys.exit(-1)

        # Delete lock files leftover from a crash
        pattern = "^"+self.lock_prefix+".*$"
        for f in os.listdir(self.logdir):
            if re.search(pattern, f):
                os.remove(os.path.join(self.logdir, f))

        # If logs exist, read from disk
        if os.path.isfile(self.logfile):
            log = shelve.open(self.logfile)
        else:
            log = shelve.open(self.logfile)
            log['next_id'] = 1
        self.txn_list = log
        if verbosity > 1:
            print "Raw log:", self.txn_list

        # Read primary.txt
        try:
            f = open(self.primary_txt)
        except:
            print "Primary.txt could not be opened.  Is it an absolute path, or relative from the directory?"
            sys.exit(-1)

        # Determine role from primary.txt
        l = f.readline()
        try:
            host, port = l.split()
            port = int(port)
            if host == self.host and port == self.port:
                self.becomePrimary()
            #elif host == 'localhost' or host == '127.0.0.1':
                #print "Cannot run on localhost.  Exiting."
                #sys.exit(-1)
            else:
                self.becomeSecondary((host,port))
        except ValueError:
            self.becomePrimary()
        finally:
            f.close()

        # Start listening
        factory = ServerFactory()
        factory.protocol = FilesystemProtocol
        factory.service = self
        reactor.listenTCP(self.port, factory, interface=self.host)
开发者ID:Hwesta,项目名称:basic-distributed-fileserver,代码行数:75,代码来源:server.py


注:本文中的twisted.internet.protocol.ServerFactory.service方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。