How to find out the name of a computer?

Asked 2 years ago, Updated 2 years ago, 137 views

Creating chat applications to write on the local network. Not Windows XP, Linux, and OS X I want to distinguish who left the message based on the computer name (such as "Mwamwa's computer", "Mine", etc.) set by the user.

I've already made the network I don't know how to get the computer name.

python hostname

2022-09-21 17:01

1 Answers

socket.gethostname() returns the hostname of the machine currently running Python interpreter in string format

import socket
socket.gethostname()

gethostbyaddr(ip_address) returns a tuple that stores (hostname, aliaslist, ipaddrlist).

import socket
socket.gethostbyaddr(socket.gethostname())[0]

os.uname() receives the following five information (sysname, nodename, release, version, machine) from the current OS and returns it to the tuploo.

import os
myhost = os.uname()[1]

However, some systems may limit nodename to 8 characters, so we recommend using a 1/2 method.


2022-09-21 17:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.