Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_listen(name):
if sys.platform != 'win32' or (
ovs.daemon._detach and ovs.daemon._detached):
# On Windows the child is a new process created which should be the
# one that creates the PassiveStream. Without this check, the new
# child process will create a new PassiveStream overwriting the one
# that the parent process created.
error, pstream = ovs.stream.PassiveStream.open(name)
if error:
sys.stderr.write("could not listen on \"%s\": %s\n"
% (name, os.strerror(error)))
sys.exit(1)
ovs.daemon.daemonize()
rpcs = []
done = False
while True:
# Accept new connections.
error, stream = pstream.accept()
if stream:
rpcs.append(ovs.jsonrpc.Connection(stream))
elif error != errno.EAGAIN:
sys.stderr.write("PassiveStream.accept() failed\n")
def main(argv):
parser = argparse.ArgumentParser(
description="JSON-RPC test utility for Python.",
formatter_class=argparse.RawDescriptionHelpFormatter)
commands = {"listen": (do_listen, 1),
"request": (do_request, 3),
"notify": (do_notify, 3),
"help": (parser.print_help, (0,))}
group_description = """\
listen LOCAL listen for connections on LOCAL
request REMOTE METHOD PARAMS send request, print reply
notify REMOTE METHOD PARAMS send notification and exit
""" + ovs.stream.usage("JSON-RPC")
group = parser.add_argument_group(title="Commands",
description=group_description)
group.add_argument('command', metavar="COMMAND", nargs=1,
choices=commands, help="Command to use.")
group.add_argument('command_args', metavar="ARG", nargs='*',
help="Arguments to COMMAND.")
ovs.daemon.add_args(parser)
args = parser.parse_args()
ovs.daemon.handle_args(args)
command_name = args.command[0]
args = args.command_args
if command_name not in commands:
sys.stderr.write("%s: unknown command \"%s\" "
def __connect(self):
self.__disconnect()
name = self.reconnect.get_name()
if not self.reconnect.is_passive():
error, self.stream = ovs.stream.Stream.open(name)
if not error:
self.reconnect.connecting(ovs.timeval.msec())
else:
self.reconnect.connect_failed(ovs.timeval.msec(), error)
elif self.pstream is not None:
error, self.pstream = ovs.stream.PassiveStream.open(name)
if not error:
self.reconnect.listening(ovs.timeval.msec())
else:
self.reconnect.connect_failed(ovs.timeval.msec(), error)
self.seqno += 1
def create(path):
assert isinstance(path, str)
unix = "unix:%s" % ovs.util.abs_file_name(ovs.dirs.RUNDIR, path)
error, stream = ovs.stream.Stream.open_block(
ovs.stream.Stream.open(unix))
if error:
vlog.warn("failed to connect to %s" % path)
return error, None
return 0, UnixctlClient(ovs.jsonrpc.Connection(stream))
def _rpc_get_schema_json(self, database):
LOG.debug('remote %s', self.remote)
error, stream_ = stream.Stream.open_block(
stream.Stream.open(self.remote))
if error:
vsctl_fatal('error %s' % os.strerror(error))
rpc = jsonrpc.Connection(stream_)
request = jsonrpc.Message.create_request('get_schema', [database])
error, reply = rpc.transact_block(request)
rpc.close()
if error:
vsctl_fatal(os.strerror(error))
elif reply.error:
vsctl_fatal('error %s' % reply.error)
return reply.result
def run_command(self, args):
_COMMANDS = {
'list-dbs': self._list_dbs,
'get-schema': self._get_schema,
'get-schema-version': self._get_schema_version,
'list-tables': self._list_tables,
'list-columns': self._list_columns,
'transact': self._transact,
'monitor': self._monitor,
'dump': self._dump,
}
command = args[0]
args = args[1:]
error, stream_ = stream.Stream.open_block(
stream.Stream.open(self.remote))
if error:
raise RuntimeError('can not open socket to %s: %s' %
(self.remote, os.strerror(error)))
rpc = jsonrpc.Connection(stream_)
ret = _COMMANDS[command](rpc, *args)
LOG.info('ret %s', ret)
rpc.close()