获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name) # DESKTOP-S3BGVQN # method two name = os.popen("hostname").read() print(name) # DESKTOP-S3BGVQN # method three name = os.environ.get("COMP
python2.7 #!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. import socket def print_machine_info(): host_name = socket.ge
import os, socket def public_ip(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(()) ip = s.getsockname()[] finally: s.close() print ip return ip if __name__ == "__main__": public_ip()