我想使用ICMP和python的套接字发送回显请求。我正在发送数据包。但是我的程序是从套接字挂在recv上的。为什么我的插座挂断了? 操作系统是Windows10。 启动终端为管理员模式。
# -*- coding: utf-8 -*-
import socket
import struct
def ping(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) as sock:
sock.connect((host, port))
sock.settimeout(30)
icmp_message = struct.pack('!BBHHH', 8, 0, 8, 0, 0)
packet = icmp_message
print('send packet...')
sock.sendall(packet)
print('recv...')
data = sock.recv(1024)
def main():
ping('192.168.0.1', 80)
if __name__ == '__main__':
main()