本文整理汇总了Python中pyroute2.IPRoute.sendto_gate方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.sendto_gate方法的具体用法?Python IPRoute.sendto_gate怎么用?Python IPRoute.sendto_gate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.sendto_gate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import sendto_gate [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']})