本文整理汇总了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)