python套接字

时间:2015-08-05 11:40:42

标签: python windows sockets icmp

尝试使用以下代码段发送裸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'

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

1 个答案:

答案 0 :(得分:2)

您正在尝试在接收套接字绑定端口之前发送到端口22433。因此,skt_send.sendto()呼叫尝试发送到不存在的端点 - 在该呼叫时,端口22433上没有任何侦听。尝试在其前面调用skt_recv.bind()

看到解释器给出的回溯会很有帮助,因为只有模糊的错误描述我们才能猜到出错了。