Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (19k points)

I'm writing a chat program for a local network. I would like to be able to identify computers and get the user-set computer name with Python.

1 Answer

0 votes
by (106k points)
  • The best way to get hostname of the computer is by using socket and its gethostname() functionality.

  • Socket:-

  • The Socket module provides access to the socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms.

  • The gethostname() function returns a socket object whose methods implement the various socket system calls. Parameter types are somewhat higher-level than in the C interface: as with read()and write() operations on Python files, buffer allocation on receive operations is automatic, and buffer length is implicit on send operations.

  • socket.gethostname():-

  • This function returns a string containing the hostname of the machine where the Python interpreter is currently executing.

import socket 

print(socket.gethostname())

  • Another thing you can do is use a platform module.

import platform

platform.node()

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 24, 2020 in Linux by blackindya (18.4k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.7k users

Browse Categories

...