Method One:
Import ctypesimport os# Get computer name def getname (): pcname = ctypes.c_char_p ('. Encode ') utf-8 = Pcname = Ctypes.cast (Pcname, ctypes.c_char_p) try: Ctypes.windll.kernel32.GetComputerNameA (Pcname, Ctypes.byref (Ctypes.c_int (pcsize)) except Exception: print ("Sth wrong in getname!") Print (PcName.value.decode (' Utf-8 ')) def main (): getname () if __name__ = = "__main__": Main ()
Method Two:
Import socket# Get computer name def getname (): print (Socket.gethostname ()) def main (): getname () if __name__ = = "__main_ _ ": Main ()
Method Three:
From OS import popen# gets the computer name Def getname (): print (popen (' hostname '). Read ()) def main (): getname () If __name__ = = "__MAIN__": Main ()
Method Four:
Import os# Get computer name def getname (): print (os.environ[' COMPUTERNAME ') def main (): getname () If __name__ = "__main_ _ ": Main ()
[Python] Get computer name