importsubprocessdefget_local_hostnames():# 获取本地IP地址ip_address=get_local_ip()# 执行ping命令result=subprocess.run(['ping',ip_address,'-c','1'],capture_output=True,text=True)# 解析ping命令的结果output=result.stdout.strip()lines=output.split('\n')hostnames=[]forlineinlines:if'from'...
ip=input("请输入要查询的 IP 地址:")response=requests.get(f" data=json.loads(response.text)hostname=data["hostname"]print(f"IP 地址{ip}对应的主机名是:{hostname}") 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 总结 通过本文,我们学习了如何使用 Python 根据 IP 获取 Hostname。我们通过输入 I...
)[20:24])# 获取回环地址print(get_ip_address(b"lo"))# 127.0.0.1# 获取本地地址print(get_ip_address(b"eth0"))# 172.27.16.3 当然我们除了获取本机的ip,还可以获取其它网站的ip。 importsocket# 获取百度的ipprint(socket.gethostbyname("www.baidu.com"))# 61.135.169.121 gethostname()是获取本机...
经过查阅大量资料,终于发现socket模块中另一个函数的妙用,那就是getaddrinfo()函数,该函数用法为getaddrinfo(host, port, family=0, type=0, proto=0, flags=0),返回值是一个五元组的列表,该五元组形式为(family, type, proto, canonname, sockaddr),其中最后一个元素sockaddr对于IPV4协议是(IP address, ...
python 获取本机IP的三种方式 代码语言:javascript 代码运行次数:0 importsocketimportfcntlimportstruct defget_ip_address(ifname):s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)returnsocket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,#SIOCGIFADDRstruct.pack('256s',ifname[:15]))[20:24])#get_ip_...
host_name=socket.gethostname() ip_address=socket.gethostbyname(host_name) print"Host name:%s"%host_name print"IP address:%s"%ip_address if__name__=='__main__': print_machine_info() Test: Host name:kevins-MacBook-Pro.local IP address:192.168.0.100 ...
""" import http.client import string import re import os import sys import xml.etree.ElementTree as etree import stat import logging import traceback import glob import ops import ipaddress from hashlib import sha256 from urllib.request import urlretrieve from urllib.parse import urlparse, urlun...
# 获取本地主机IP地址 import socket local_ip = socket.gethostbyname(socket.gethostname()) print(f"本机IP地址: {local_ip}") 1.2.2 TCP协议与可靠性传输 TCP协议像是一位严谨的快递员,他不仅确保每个包裹都能送达,还确保包裹按顺序到达,如有丢失或损坏,还会重新发送。TCP通过握手建立连接,通过序列号和确...
{"hostname": hostname, "ip": ip, "mac": get_mac_address(), "user": users_list, "start_time": start_time}# 01.cpu信息cpu1 = psutil.cpu_count()cpu2 = str(psutil.cpu_percent(interval=1)) + '%'# print(u"物理CPU个数 %s" % psutil.cpu_count(logical=False))cpu = {"amount"...
{details.get('enabled', None)}")print(f"- Physical address: {details.get('phys_address', None)}")print(f"- Duplex mode: {details.get('duplex_mode', None)}")forcounter,countindetails.get('counters',{}).items():ifisinstance(count,int):ifcount>0:print(f"- {counter}: {count}")...