本文整理汇总了Python中psutil.Process.username方法的典型用法代码示例。如果您正苦于以下问题:Python Process.username方法的具体用法?Python Process.username怎么用?Python Process.username使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil.Process
的用法示例。
在下文中一共展示了Process.username方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_procinfo_by_address
# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import username [as 别名]
def get_procinfo_by_address(ip_src, ip_dst, port_src=None, port_dst=None):
"""
Gets Infos about the Prozess associated with the given address information
Both port_src and port_dst must be not None or None at the same time.
return -- [pid, "/path/to/command", "user", "hash"] or []
"""
result = []
if port_src is not None:
pids = [c.pid for c in net_connections()
if len(c.raddr) != 0 and c.pid is not None and
(c.laddr[0], c.raddr[0], c.laddr[1], c.raddr[1]) == (ip_src, ip_dst, port_src, port_dst)]
else:
pids = [c.pid for c in net_connections()
if len(c.raddr) != 0 and c.pid is not None and (c.laddr[0], c.raddr[0]) == (ip_src, ip_dst)]
try:
if len(pids) > 1:
logger.warning("more than 1 matching process: %r", pids)
proc = Process(pids[0])
cmd = [pids[0], proc.cmdline(), proc.username()]
hash_input = "%d%s%s" % (cmd[0], cmd[1], cmd[2])
procinfo_hash = hashlib.sha256(hash_input.encode("UTF-8"))
cmd.append(procinfo_hash)
logger.debug("process info: %r", cmd)
return cmd
except IndexError:
pass
return []
示例2: get_info
# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import username [as 别名]
def get_info(component_path, format):
component_details = open(os.path.join(component_path, ZATO_INFO_FILE)).read()
out = {
'component_details': component_details,
'component_full_path': component_path,
'component_host': current_host(),
'component_running': False,
'current_time': datetime.now().isoformat(),
'current_time_utc': datetime.utcnow().isoformat(),
'master_proc_connections': None,
'master_proc_pid': None,
'master_proc_name': None,
'master_proc_create_time': None,
'master_proc_create_time_utc': None,
'master_proc_username': None,
'master_proc_workers_no': None,
'master_proc_workers_pids': None,
}
master_proc_pid = None
try:
master_proc_pid = int(open(os.path.join(component_path, MISC.PIDFILE)).read())
except(IOError, ValueError):
# Ok, no such file or it's empty
pass
if master_proc_pid:
out['component_running'] = True
master_proc = Process(master_proc_pid)
workers_pids = sorted(elem.pid for elem in master_proc.children())
out['master_proc_connections'] = format_connections(master_proc.connections(), format)
out['master_proc_pid'] = master_proc.pid
out['master_proc_create_time'] = datetime.fromtimestamp(master_proc.create_time()).isoformat()
out['master_proc_create_time_utc'] = datetime.fromtimestamp(master_proc.create_time(), UTC).isoformat()
out['master_proc_username'] = master_proc.username()
out['master_proc_name'] = master_proc.name()
out['master_proc_workers_no'] = len(workers_pids)
out['master_proc_workers_pids'] = workers_pids
for pid in workers_pids:
worker = Process(pid)
out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(worker.create_time()).isoformat()
out['worker_{}_create_time_utc'.format(pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat()
out['worker_{}_connections'.format(pid)] = format_connections(worker.connections(), format)
return out
示例3: _on_server
# 需要导入模块: from psutil import Process [as 别名]
# 或者: from psutil.Process import username [as 别名]
def _on_server(self, args):
os.chdir(self.original_dir)
abs_args_path = os.path.abspath(args.path)
component_details = open(os.path.join(abs_args_path, ZATO_INFO_FILE)).read()
out = {
'component_details': component_details,
'component_full_path': abs_args_path,
'component_host': current_host(),
'component_running': False,
'current_time': datetime.now().isoformat(),
'current_time_utc': datetime.utcnow().isoformat(),
'master_proc_connections': None,
'master_proc_pid': None,
'master_proc_name': None,
'master_proc_create_time': None,
'master_proc_create_time_utc': None,
'master_proc_username': None,
'master_proc_workers_no': None,
'master_proc_workers_pids': None,
}
master_proc_pid = None
try:
master_proc_pid = int(open(os.path.join(abs_args_path, MISC.PIDFILE)).read())
except(IOError, ValueError):
# Ok, no such file or it's empty
pass
if master_proc_pid:
out['component_running'] = True
master_proc = Process(master_proc_pid)
workers_pids = sorted(elem.pid for elem in master_proc.children())
out['master_proc_connections'] = master_proc.connections()
out['master_proc_pid'] = master_proc.pid
out['master_proc_create_time'] = datetime.fromtimestamp(master_proc.create_time()).isoformat()
out['master_proc_create_time_utc'] = datetime.fromtimestamp(master_proc.create_time(), UTC).isoformat()
out['master_proc_username'] = master_proc.username()
out['master_proc_name'] = master_proc.name()
out['master_proc_workers_no'] = len(workers_pids)
out['master_proc_workers_pids'] = workers_pids
for pid in workers_pids:
worker = Process(pid)
out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(worker.create_time()).isoformat()
out['worker_{}_create_time_utc'.format(pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat()
out['worker_{}_connections'.format(pid)] = worker.connections()
if getattr(args, 'json', False):
out['component_details'] = loads(out['component_details'])
self.logger.info(dumps(out))
else:
cols_width = args.cols_width if args.cols_width else DEFAULT_COLS_WIDTH
cols_width = (elem.strip() for elem in cols_width.split(','))
cols_width = [int(elem) for elem in cols_width]
table = Texttable()
table.set_cols_width(cols_width)
# Use text ('t') instead of auto so that boolean values don't get converted into ints
table.set_cols_dtype(['t', 't'])
rows = [['Key', 'Value']]
rows.extend(sorted(out.items()))
table.add_rows(rows)
self.logger.info(table.draw())