1. 程式人生 > >使用Python實現獲取裝置名及地址

使用Python實現獲取裝置名及地址

1.  查詢本機的裝置名及IP地址,開啟Python 3.6(32-bit),輸入:

      import  socket

      host_name = socket.gethostname()

     print(" Host name: %s" %host_name)

      print(" IP address: %s" %socket.gethostbyname(host_name))


2. 查詢本地的裝置名及IP地址,使用內建的類方法,定義成一個獨立的函式print_device_info()

import  socket
def  print_device_info():
      host_name = socket.gethostname()
      print(" Host name: %s"  %host_name)
      print(" IP address: %s"  %socket.gethostbyname(host_name))
if  __name__=='__main__':
      print_device_info();

3. 查詢遠端裝置名及IP地址,定義一個函式print_device_remote_info()

import  socket
def  print_device_remote_info():
      host_name = 'home.lenovo'
      print(" Host name: %s"  %host_name)
      print(" IP address: %s"  %socket.gethostbyname(host_name))
if  __name__=='__main__':
      print_device_remote_info();