windows - Invalid port in python sockets -
i getting error while trying send bare icmp message using following snippet:
windows says port invalid given ip range.
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: = skt_recv.recvfrom(1024)[0] hdr_reply = a[20:] icmp_type = a[20] if icmp_type == 0 : print ("got ping") fl = true return target_ip if fl: break except socket.timeout e: raise e return
here: skt.recv_bind((target_ip,22433))
ip address have fed b- , c-class ip addresses.
i traceback:
file "c:\program files (x86)\microsoft visual studio 14.0\common7\ide\extensio ns\microsoft\python tools 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 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 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 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 when not handled user code: oserror unhandled user code message: [winerror 10049] invalid request given address
you trying send port 22433 before receiving socket has bound port. skt_send.sendto()
call therefore trying send non-existent endpoint - @ time of call there's nothing listening on port 22433. try putting skt_recv.bind()
call before it.
it helpful see traceback given interpreter, vague description of error have guess went wrong.
Comments
Post a Comment