本文整理汇总了Python中psutil.CONN_LISTEN属性的典型用法代码示例。如果您正苦于以下问题:Python psutil.CONN_LISTEN属性的具体用法?Python psutil.CONN_LISTEN怎么用?Python psutil.CONN_LISTEN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类psutil
的用法示例。
在下文中一共展示了psutil.CONN_LISTEN属性的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_quiet
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def test_quiet(kind):
with TestProcess(sys.executable, __file__, 'daemon', 'test_quiet', kind) as proc:
with dump_on_error(proc.read):
wait_for_strings(proc.read, TIMEOUT,
'{a1}',
'{b1}')
psproc = psutil.Process(proc.proc.pid)
port = None
t = time.time()
while port is None and time.time() - t < 5:
for c in psproc.connections():
if c.status == psutil.CONN_LISTEN and c.laddr[0] == '127.0.0.1':
port = c.laddr[1]
with TestSocket(socket.create_connection(('127.0.0.1', int(port)), timeout=TIMEOUT)) as client:
with dump_on_error(client.read):
wait_for_strings(client.read, TIMEOUT, "-> print('{b2}')")
client.fh.write(b'continue\r\n')
wait_for_strings(proc.read, TIMEOUT, 'DIED.')
assert 'RemotePdb session open at' not in proc.read()
示例2: check_lnd
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def check_lnd(stub, proc_name="lnd", rpc_listen_ports=None):
if not rpc_listen_ports:
rpc_listen_ports = [10009]
pid_ok = False
listen_ok = False
unlocked = False
synced_to_chain = False
synced_to_graph = False
if IS_WIN32_ENV:
return pid_ok, listen_ok, unlocked, synced_to_chain, synced_to_graph
if not [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if proc_name in p.info['name']]:
return pid_ok, listen_ok, unlocked, synced_to_chain, synced_to_graph
else:
pid_ok = True
if not [net_con for net_con in psutil.net_connections(kind='inet')
if (net_con.status == psutil.CONN_LISTEN and net_con.laddr[1] in rpc_listen_ports)]:
return pid_ok, listen_ok, unlocked, synced_to_chain, synced_to_graph
else:
listen_ok = True
try:
get_info = stub.GetInfo(ln.GetInfoRequest())
unlocked = True
synced_to_chain = get_info.synced_to_chain
synced_to_graph = get_info.synced_to_graph
except grpc.RpcError as err:
if err._state.__dict__['code'] == grpc.StatusCode.UNIMPLEMENTED:
log.debug("wallet is 'locked'")
else:
log.warning("an unknown RpcError occurred")
log.warning(err)
except Exception as err:
log.warning("an error occurred: {}".format(err))
return pid_ok, listen_ok, unlocked, synced_to_chain, synced_to_graph
示例3: _detect_iface_name
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def _detect_iface_name(self, h_ipdb):
# Let's try config first
if config.CONF.binding.link_iface in h_ipdb.interfaces:
LOG.debug(f'Using configured interface '
f'{config.CONF.binding.link_iface} as bridge interface.')
return config.CONF.binding.link_iface
# Then let's try choosing the one where kubelet listens to
conns = [x for x in psutil.net_connections()
if x.status == psutil.CONN_LISTEN
and x.laddr.port == KUBELET_PORT]
if len(conns) == 1:
lookup_addr = conns[0].laddr.ip
for name, iface in h_ipdb.interfaces.items():
if type(name) is int: # Skip ones duplicated by id
continue
for addr in iface['ipaddr']:
if addr[0] == lookup_addr:
LOG.debug(f'Using kubelet bind interface {name} as '
f'bridge interface.')
return name
# Alright, just try the first non-loopback interface
for name, iface in h_ipdb.interfaces.items():
if type(name) is int: # Skip ones duplicated by id
continue
if iface['flags'] & pyroute2.netlink.rtnl.ifinfmsg.IFF_LOOPBACK:
continue # Skip loopback
LOG.debug(f'Using interface {name} as bridge interface.')
return name
raise exceptions.CNIBindingFailure('Cannot find bridge interface for '
'nested driver to use. Please set '
'[binding]link_iface option.')
示例4: test_tcp_v4
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def test_tcp_v4(self):
addr = ("127.0.0.1", get_free_port())
with closing(bind_socket(AF_INET, SOCK_STREAM, addr=addr)) as sock:
conn = self.check_socket(sock)
assert not conn.raddr
self.assertEqual(conn.status, psutil.CONN_LISTEN)
示例5: test_tcp_v6
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def test_tcp_v6(self):
addr = ("::1", get_free_port())
with closing(bind_socket(AF_INET6, SOCK_STREAM, addr=addr)) as sock:
conn = self.check_socket(sock)
assert not conn.raddr
self.assertEqual(conn.status, psutil.CONN_LISTEN)
示例6: _find_ngrok_inspection_port
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def _find_ngrok_inspection_port(self):
"""
:return (str, int):
"""
for c in psutil.Process(self.ngrok_proc.pid).connections():
if c.laddr[0] == '127.0.0.1' and c.raddr == () and c.status == psutil.CONN_LISTEN:
return c.laddr
raise RuntimeError('Did not find API interface of ngrok.')
示例7: popen
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def popen(path, wait_for_listen=False):
process = psutil.Popen(
['python', path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if wait_for_listen:
# It would be more correct to check ``conn.status ==
# psutil.CONN_LISTEN`` but this works
try:
while process.is_running() and not process.connections():
pass
except psutil.Error:
raise AssertionError(process.stderr.read())
success = True
# noinspection PyBroadException
try:
yield process
except:
success = False
raise
finally:
process.kill()
if not success:
dump_std_streams(path, process)
示例8: get_process_pid_by_listen_port
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import CONN_LISTEN [as 别名]
def get_process_pid_by_listen_port(self,
listen_port: int = 80,
listen_address: Union[None, str] = None,
listen_proto: Union[None, str] = None,
exit_on_failure: bool = True,
exit_code: int = 27,
quiet: bool = False) -> Union[None, List[int]]:
"""
Get list of processes ID by listen TCP or UDP port
:param listen_port: Listening TCP or UDP port integer (default: 80)
:param listen_address: Listening IPv4 or IPv6 address string (default: None)
:param listen_proto: Listening protocol string 'tcp' or 'udp' (default: 'tcp')
:param exit_on_failure: Exit in case of error (default: False)
:param exit_code: Set exit code integer (default: 27)
:param quiet: Quiet mode, if True no console output (default: False)
:return: List of processes ID by listen TCP or UDP port
"""
pids: List[int] = list()
try:
assert 1 < listen_port < 65535, \
'Bad listen port: ' + self.error_text(str(listen_port)) + \
' listen port must be in range: ' + self.info_text('1 - 65535')
assert (listen_proto is None or listen_proto == 'tcp' or listen_proto == 'udp'), \
'Bad value in listen proto: ' + self.error_text(str(listen_proto)) + \
' listen proto must be ' + self.info_text('None' + ' or ' + '\'tcp\'' + ' or ' + '\'udp\'')
if listen_proto is None:
listen_proto = 'tcp'
for process in ps.process_iter():
connections = process.connections()
for connection in connections:
(address, port) = connection.laddr
if connection.type == sock.SOCK_STREAM and connection.status == ps.CONN_LISTEN:
proto = 'tcp'
elif connection.type == sock.SOCK_DGRAM:
proto = 'udp'
else:
continue
if listen_address is not None:
if address == listen_address and proto == listen_proto \
and port == listen_port and process.pid is not None:
pids.append(process.pid)
else:
if proto == listen_proto and port == listen_port and process.pid is not None:
pids.append(process.pid)
return pids
except ps.NoSuchProcess:
return pids
except AssertionError as Error:
if not quiet:
self.print_error(Error.args[0])
if exit_on_failure:
exit(exit_code)
return None