标签:
方法一:
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()
标签:
原文地址:http://www.cnblogs.com/id404/p/4316115.html