本文整理汇总了Python中pyroute2.IPRoute.getsockopt方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.getsockopt方法的具体用法?Python IPRoute.getsockopt怎么用?Python IPRoute.getsockopt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.getsockopt方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import getsockopt [as 别名]
def Server(cmdch, brdch):
try:
ipr = IPRoute()
lock = ipr._sproxy.lock
ipr._s_channel = brdch
poll = select.poll()
poll.register(ipr, select.POLLIN | select.POLLPRI)
poll.register(cmdch, select.POLLIN | select.POLLPRI)
except Exception as e:
cmdch.send({'stage': 'init',
'error': e})
return 255
# all is OK so far
cmdch.send({'stage': 'init',
'error': None})
# 8<-------------------------------------------------------------
while True:
events = poll.poll()
for (fd, event) in events:
if fd == ipr.fileno():
bufsize = ipr.getsockopt(SOL_SOCKET, SO_RCVBUF) // 2
with lock:
brdch.send({'stage': 'broadcast',
'data': ipr.recv(bufsize)})
elif fd == cmdch.fileno():
cmd = cmdch.recv()
if cmd['stage'] == 'shutdown':
poll.unregister(ipr)
poll.unregister(cmdch)
ipr.close()
return
elif cmd['stage'] == 'command':
error = None
try:
ret = getattr(ipr, cmd['name'])(*cmd['argv'],
**cmd['kwarg'])
except Exception as e:
error = e
error.tb = traceback.format_exc()
cmdch.send({'stage': 'command',
'error': error,
'return': ret,
'cookie': cmd['cookie']})
示例2: Server
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import getsockopt [as 别名]
#.........这里部分代码省略.........
{'stage': 'command',
'name': str,
'cookie': cookie,
'argv': [...],
'kwarg': {...}}
# response
{'stage': 'command',
'error': exception or None,
'return': retval,
'cookie': cookie}
Right now the protocol is synchronous, so there is not
need in cookie yet. But in some future it can turn into
async, and then cookies will be used to match messages.
The final stage is 'shutdown'. It terminates the worker
thread, has no response and no messages can passed after.
'''
def close(s, frame):
# just leave everything else as is
brdch.send({'stage': 'signal',
'data': s})
try:
ipr = IPRoute()
lock = ipr._sproxy.lock
ipr._s_channel = ProxyChannel(brdch, 'broadcast')
poll = select.poll()
poll.register(ipr, select.POLLIN | select.POLLPRI)
poll.register(cmdch, select.POLLIN | select.POLLPRI)
except Exception as e:
cmdch.send({'stage': 'init',
'error': e})
return 255
# all is OK so far
cmdch.send({'stage': 'init',
'error': None})
signal.signal(signal.SIGHUP, close)
signal.signal(signal.SIGINT, close)
signal.signal(signal.SIGTERM, close)
# 8<-------------------------------------------------------------
while True:
try:
events = poll.poll()
except:
continue
for (fd, event) in events:
if fd == ipr.fileno():
bufsize = ipr.getsockopt(SOL_SOCKET, SO_RCVBUF) // 2
with lock:
error = None
data = None
try:
data = ipr.recv(bufsize)
except Exception as e:
error = e
error.tb = traceback.format_exc()
brdch.send({'stage': 'broadcast',
'data': data,
'error': error})
elif fd == cmdch.fileno():
cmd = cmdch.recv()
if cmd['stage'] == 'shutdown':
poll.unregister(ipr)
poll.unregister(cmdch)
ipr.close()
return
elif cmd['stage'] == 'reconstruct':
error = None
try:
msg = cmd['argv'][0]()
msg.load(pickle.loads(cmd['argv'][1]))
msg.encode()
ipr.sendto_gate(msg, cmd['argv'][2])
except Exception as e:
error = e
error.tb = traceback.format_exc()
cmdch.send({'stage': 'reconstruct',
'error': error,
'return': None,
'cookie': cmd['cookie']})
elif cmd['stage'] == 'command':
error = None
try:
ret = getattr(ipr, cmd['name'])(*cmd['argv'],
**cmd['kwarg'])
except Exception as e:
error = e
error.tb = traceback.format_exc()
cmdch.send({'stage': 'command',
'error': error,
'return': ret,
'cookie': cmd['cookie']})
示例3: NetNServer
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import getsockopt [as 别名]
#.........这里部分代码省略.........
try:
return listnetns()
except OSError:
return []
# 8<-------------------------------------------------------------
def create_netns():
# FIXME validate and prepare NETNS_RUN_DIR
# create mountpoint
try:
os.close(os.open('%s/%s' % (NETNS_RUN_DIR, netns),
os.O_RDONLY | os.O_CREAT | os.O_EXCL, 0))
except OSError as e:
return e
except Exception as e:
return OSError(errno.ECOMM, str(e), netns)
# unshare
if libc.unshare(CLONE_NEWNET) < 0:
return OSError(errno.ECOMM, 'unshare failed', netns)
# bind the namespace
if libc.mount('/proc/self/ns/net',
'%s/%s' % (NETNS_RUN_DIR, netns),
'none', MS_BIND, None) < 0:
return OSError(errno.ECOMM, 'mount failed', netns)
return None
# 8<-------------------------------------------------------------
#
if netns in list_netns():
if flags & (os.O_CREAT | os.O_EXCL) == (os.O_CREAT | os.O_EXCL):
cmdch.send(OSError(errno.EEXIST, 'netns exists', netns))
return errno.EEXIST
else:
if flags & os.O_CREAT:
ret = create_netns()
if ret is not None:
cmdch.send(ret)
return ret.errno
try:
nsfd = os.open('%s/%s' % (NETNS_RUN_DIR, netns), os.O_RDONLY)
except OSError as e:
cmdch.send(e)
return e
except Exception as e:
cmdch.send(OSError(errno.ECOMM, str(e), netns))
return 255
#
ret = libc.syscall(__NR_setns, nsfd, CLONE_NEWNET)
if ret != 0:
cmdch.send(OSError(ret, 'failed to open netns', netns))
return ret
#
try:
ipr = IPRoute()
poll = select.poll()
poll.register(ipr, select.POLLIN | select.POLLPRI)
poll.register(cmdch, select.POLLIN | select.POLLPRI)
except Exception as e:
cmdch.send(e)
return 255
# all is OK so far
cmdch.send(None)
# 8<-------------------------------------------------------------
while True:
events = poll.poll()
for (fd, event) in events:
if fd == ipr.fileno():
bufsize = ipr.getsockopt(SOL_SOCKET, SO_RCVBUF) // 2
rcvch.send(ipr.recv(bufsize))
elif fd == cmdch.fileno():
try:
cmdline = cmdch.recv()
if cmdline is None:
poll.unregister(ipr)
poll.unregister(cmdch)
ipr.close()
os.close(nsfd)
return
(cmd, argv, kwarg) = cmdline
if cmd[:4] == 'send':
# Achtung
#
# It's a hack, but we just have to do it: one
# must use actual pid in netlink messages
#
# FIXME: there can be several messages in one
# call buffer; but right now we can ignore it
msg = argv[0][:12]
msg += struct.pack("I", os.getpid())
msg += argv[0][16:]
argv = list(argv)
argv[0] = msg
cmdch.send(getattr(ipr, cmd)(*argv, **kwarg))
except Exception as e:
e.tb = traceback.format_exc()
cmdch.send(e)