将snmp八位字节字符串转换为人类可读日期格式

Mic*_*ang 8 python decode snmp pysnmp

使用pysnmp框架我得到了一些snmp walk值.不幸的是对于oid

1.3.6.1.21.69.1.5.8.1.2(DOCS-CABLE-DEVICE-MIB)

我得到一个奇怪的结果,我不能正确打印在这里因为它包含ascii字符之类的 BEL ACK

做一个repr我得到:

八位组串( '\ X07\XD8 \吨\ X17\X03\x184\X00')

但输出应如下所示:

2008-9-23,3:24:52.0

格式称为"DateAndTime".如何将OctetString输出转换为"人类可读"的日期/时间?

Pau*_*ine 16

格式在这里.

A date-time specification. 
            field  octets  contents                  range
            -----  ------  --------                  -----
              1      1-2   year*                     0..65536
              2       3    month                     1..12
              3       4    day                       1..31
              4       5    hour                      0..23
              5       6    minutes                   0..59
              6       7    seconds                   0..60
                           (use 60 for leap-second)
              7       8    deci-seconds              0..9
              8       9    direction from UTC        '+' / '-'
              9      10    hours from UTC*           0..13
             10      11    minutes from UTC          0..59
* Notes:
            - the value of year is in network-byte order
            - daylight saving time in New Zealand is +13 For example, 
              Tuesday May 26, 1992 at 1:30:15 PM EDT would be displayed as:
                 1992-5-26,13:30:15.0,-4:0 
              Note that if only local time is known, then timezone
              information (fields 8-10) is not present.
Run Code Online (Sandbox Code Playgroud)

你可以使用struct.unpack:

>>> import struct, datetime
>>> s = '\x07\xd8\t\x17\x03\x184\x00'
>>> datetime.datetime(*struct.unpack('>HBBBBBB', s))
datetime.datetime(2008, 9, 23, 3, 24, 52)
Run Code Online (Sandbox Code Playgroud)