用于un*x系统的python utmp包装器

pyutmp的Python项目详细描述


简介

pyutmp模块为utmp 类Unix操作系统上的文件。套用linux程序员的 手动页面utmp(5),utmp文件允许用户发现信息 关于当前使用(即,已登录)系统的用户。utmp 文件是一系列条目,其结构通常由 utmp.hc头文件。

此模块为底层操作提供只读接口 系统的cutmpapi。

接口和用法

pyutmp模块提供两个类:utmpfile和utmp。一个 utmp file对象表示在 utmpfile对象,它产生连续的utmp对象。例如:

from pyutmp import UtmpFile
import time

for utmp in UtmpFile():
    # utmp is a Utmp object
    if utmp.ut_user_process:
        print '%s logged in at %s on tty %s' % (utmp.ut_user, time.ctime(utmp.ut_time), utmp.ut_line)

utmpfile

除了__iter__()generator方法之外,还允许在 在utmp文件的内容中,utmp file类还提供了 rewind()方法,允许您将文件指针重置到顶部 文件的。有关详细信息,请参见类文档。

utmp

utmp类的字段依赖于操作系统。然而,他们 将始终至少包含以下字段:

FieldTypeDescription
^{tt6}$^{tt7}$The user associated with the utmp entry, if any.
^{tt8}$^{tt7}$The tty or pseudo-tty associated with the entry, if any. In this API, the line will always be the full path to the device.
^{tt10}$^{tt7}$The host name associated with the entry, if any.
^{tt12}$timestampThe timestamp associated with the entry. This timestamp is in the form returned by ^{tt13}$ and may be passed directly to methods like ^{tt14}$.
^{tt15}$^{tt16}$Whether or not the utmp entry is a user process (as opposed to a reboot or some other system event).

在某些操作系统上,可能存在其他字段。例如,在 linux和solaris系统(以及其他系统v派生系统),utmp 包含以下字段:

Optional FieldTypeDescription
^{tt17}$^{tt7}$The type of the entry, typically one of the following string values: “RUN_LVL”, “BOOT_TIME”, “NEW_TIME”, “OLD_TIME”, “INIT_PROCESS”, “LOGIN_PROCESS”, “USER_PROCESS”, “DEAD_PROCESS”, “ACCOUNTING”. See the utmp(5) manual page for a description of these values
^{tt19}$^{tt20}$Associated process ID, if any.
^{tt21}$^{tt7}$The init(8) ID, or the abbreviated tty name.
^{tt23}$^{tt20}$Process exit code, if applicable.
^{tt25}$^{tt20}$Session ID, for windowing.
^{tt27}$^{tt20}$ arrayIPv4 address of remote host (if applicable), one octet per array element.

如果您正在编写可移植代码,则不应指望 这些属性——或者,至少,您应该将对它们的访问包装在 捕获AttributeErrortry/catch块。

注释

此模块已在以下操作系统上测试:

  • ubuntu linux,8.04版
  • FreeBSD
  • Mac OS X 10.4(老虎机)
  • opensolaris(2008.05,x86,使用sunstudio 12编译器套件)

添加对其他unix变体的支持应该很简单。

限制

  • utmp文件的访问是只读的。没有书面规定 到档案里去。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何通过编程将Android置于待机状态?   java mavenjarsignerplugin在Jenkins构建期间失败,但不是在命令行上   java创建具有公共ipv4地址的HttpServer   将数据从jsp传递到servlet的java返回null   Web应用程序中的java JDBC数据库连接   java无法显示unicode字符   java webview应用程序未上载图片或视频   Android客户端Java服务器,如果订单处理方式不正确,客户端将无法连接   java在不复制备份表的情况下创建备份表   集合如何在Java中实现列表折叠   java如何配置在JBoss7中使用EntityListeners?   java JVM ClassUnloadingWithConcurrentMark标志   java如何在Maven中排除特定的单元测试   java线程在SynchronousQueue中执行put操作后挂起   java中的hibernate Comparator和可比较的负面场景   数组中的java连接元素   拖放Java,让面板落在鼠标事件上   java是一种事务管理应用程序,无需在方法上使用@transactional   java有没有办法从安卓的软键板上删除这一行?