proc.py 文件源码

python
阅读 40 收藏 0 点赞 0 评论 0

项目:spiderfoot 作者: wi-fi-analyzer 项目源码 文件源码
def _decode_proc_address_encoding(addr):
  """
  Translates an address entry in the /proc/net/* contents to a human readable
  form (`reference <http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html>`_,
  for instance:

  ::

    "0500000A:0016" -> ("10.0.0.5", 22)

  :param str addr: proc address entry to be decoded

  :returns: **tuple** of the form **(addr, port)**, with addr as a string and port an int
  """

  ip, port = addr.split(':')

  # the port is represented as a two-byte hexadecimal number
  port = int(port, 16)

  if sys.version_info >= (3,):
    ip = ip.encode('ascii')

  # The IPv4 address portion is a little-endian four-byte hexadecimal number.
  # That is, the least significant byte is listed first, so we need to reverse
  # the order of the bytes to convert it to an IP address.
  #
  # This needs to account for the endian ordering as per...
  # http://code.google.com/p/psutil/issues/detail?id=201
  # https://trac.torproject.org/projects/tor/ticket/4777

  if sys.byteorder == 'little':
    ip = socket.inet_ntop(socket.AF_INET, base64.b16decode(ip)[::-1])
  else:
    ip = socket.inet_ntop(socket.AF_INET, base64.b16decode(ip))

  return (ip, port)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号