python套接字中的端口无效

我尝试使用以下代码片段发送裸ICMP消息时出错:

Windows说这个端口对于给定的IP范围是无效的。

def send_ping(source_ip,target_ip,data_function=construct_icmp_echo): fail = 0 skt_send = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP) skt_recv = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP) (src_binary,) = struct.unpack (">L",socket.inet_aton(source_ip)) (tgt_binary,) = struct.unpack (">L",socket.inet_aton(source_ip)) skt_send.setsockopt(socket.SOL_IP, socket.IP_TTL, 16) ipheader = struct.pack("BBHHHBBHLL",0x54,0xdc,48,50,8,16,1,0,src_binary,tgt_binary) cksum = icmpcksum(ipheader) ipheader = ipheader = struct.pack("BBHHHBBHLL",0x54,0xdc,48,50,8,16,1,cksum,src_binary,tgt_binary) skt_send.sendto(data_function(),(target_ip,22433)) skt_recv.bind((target_ip,22433)) skt_recv.settimeout(5) thetime = datetime.datetime.now() try: s = datetime.datetime.now() while time.time - s < 60: a = skt_recv.recvfrom(1024)[0] hdr_reply = a[20:] icmp_type = a[20] if icmp_type == 0 : print ("got a ping") fl = True return target_ip if fl: break except socket.timeout as e: raise e return 

这里: skt.recv_bind((target_ip,22433))我所馈送的IP地址是B级和C级IP地址。

我得到这个回溯:

 File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_launcher.py", li ne 78, in <module> vspd.debug(filename, port_num, debug_id, debug_options, run_as) File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_debugger.py", li ne 2465, in debug exec_file(file, globals_obj) File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_util.py", line 1 11, in exec_file exec_code(code, file, global_variables) File "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensio ns\Microsoft\Python Tools for Visual Studio\2.2\visualstudio_py_util.py", line 8 7, in exec_code exec(code_obj, global_variables) File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python -ipscan\python_ipscan.py", line 86, in <module> if __name__=="__main__": main () File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python -ipscan\python_ipscan.py", line 83, in main send_ping (myaddr,"""155.223.197.1""",construct_icmp_echo) File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python -ipscan\python_ipscan.py", line 70, in send_ping traceback.print_stack() Traceback (most recent call last): File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python -ipscan\python_ipscan.py", line 86, in <module> if __name__=="__main__": main () File "C:\Users\user\Documents\Visual Studio 2015\Projects\python-ipscan\python -ipscan\python_ipscan.py", line 84, in main traceback.extract_tb(sys.exc_info()) File "C:\Program Files\Python34\lib\traceback.py", line 106, in extract_tb return list(_extract_tb_iter(tb, limit=limit)) File "C:\Program Files\Python34\lib\traceback.py", line 59, in _extract_tb_or_ stack_iter f, lineno, next_item = extractor(curr) AttributeError: 'tuple' object has no attribute 'tb_frame' 

未由用户代码处理的exception: OSError was unhandled by user code Message: [WinError 10049] Invalid request for given address未被用户代码OSError was unhandled by user code Message: [WinError 10049] Invalid request for given address

您正尝试在接收套接字绑定端口之前发送到端口22433。 因此, skt_send.sendto()调用尝试发送到一个不存在的端点 – 在那个调用的时候没有任何监听端口22433.试着把skt_recv.bind()调用放在它之前。

看到解释者给出的追溯将是有帮助的,因为只有对错误的模糊描述,我们才能猜出错误。