Thursday, 2 June 2011

Как получить hostname машины в Python?

import socket
hostname = socket.gethostname()

print hostname

Источник: http://docs.python.org/library/socket.html#socket.gethostname

5 comments:

  1. Скайп таки взломали! http://skype-open-source.blogspot.com/

    ReplyDelete
  2. import os
    hostname = os.uname()[1]

    ReplyDelete
  3. И будете не правы, потому что:
    os.uname()
    Return a 5-tuple containing information identifying the current operating system. The tuple contains 5 strings: (sysname, nodename, release, version, machine).

    Some systems truncate the nodename to 8 characters or to the leading component; a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname()).

    Источник: http://docs.python.org/library/os.html

    ReplyDelete
  4. ибо print os.uname()[1]
    или более кроссплатформенно
    import platform; print platform.node()

    ReplyDelete

Note: only a member of this blog may post a comment.