Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Python to get the system hostname?

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

like image 402
John Avatar asked Nov 24 '10 21:11

John


People also ask

How can I see my hostname?

Locating Your Computer's Hostname on a PC (Windows 10)In the window the window that appears on the bottom-left hand corner of your screen, type in cmd and click OK. The command prompt window will appear. In this window, type hostname and press Enter. The name of your computer will be displayed.

What is the hostname of the system?

The hostname is what a device is called on a network. Alternative terms for this are computer name and site name. The hostname is used to distinguish devices within a local network. In addition, computers can be found by others through the hostname, which enables data exchange within a network, for example.


1 Answers

Use socket and its gethostname() functionality. This will get the hostname of the computer where the Python interpreter is running:

import socket print(socket.gethostname()) 
like image 68
Alex Avatar answered Sep 27 '22 16:09

Alex