通过python获取计算机名
<p>方法一:</p>
import ctypes import os #获取计算机名 def getname(): pcName = ctypes.c_char_p(''.encode('utf-8')) pcSize = 16 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()
方法二:
import socket #获取计算机名 def getname(): print(socket.gethostname()) def main(): getname() if __name__ == "__main__": main()
方法三:
from os import popen #获取计算机名 def getname(): print(popen('hostname').read()) def main(): getname() if __name__ == "__main__": main()
方法四:
import os #获取计算机名 def getname(): print (os.environ['COMPUTERNAME']) def main(): getname() if __name__ == "__main__": main()