Using Python to debug network programs

Source: Internet
Author: User

Need to be linked to the program, the communication is always a lot of trouble, if you can write a simple "test machine", things are easy to do, or sometimes want to do "middleman" to see what is on the Internet. The front has written a serial port test machine, today tried a bit of UDP.

ImportSocket fromstructImport*s=Socket.socket (socket.af_inet, socket. SOCK_RAW, Socket. IPPROTO_UDP) whileTrue:packet= S.recvfrom (65565) Packet=Packet[0] Ip_header= Packet[0:20] Iph= Unpack ('! Bbhhhbbh4s4s', Ip_header) VERSION_IHL=Iph[0] Version= VERSION_IHL >> 4IHL= VERSION_IHL & 0xfTTL= Iph[5] Protocol= Iph[6] S_addr= Socket.inet_ntoa (iph[8]) d_addr= Socket.inet_ntoa (iph[9])    Print '---> src_addr:'+s_addr+', Dst_addr:'+d_addr Udp_header= packet[20:28] UDPH= Unpack ('! HHHH', Udp_header) S_port=Udph[0] D_port= Udph[1] Data_length= Udph[3]    Print 'Src_port:'+str (S_port) +', Dst_port:'+str (D_port)

Simply make a comment:

1. Raw_socket, literally understood as "bare" sockets, this "bare" for the application programmer is, not parsed.

2. Recvfrom (length), note that the parameter here is the size of the buffer, not the port. Because we are here recvfrom () to, is native all UDP datagram (TCP uses IPPROTO_TCP,ICMP uses ipproto_icmp).

3. The return of Recvfrom is a tuple, similar to the following:

("E \x00x\xcc\xfc\x00\x000\x06j%j}g\x13\xc0\xa8\x01\x06\x01\xbb\xa3\xdc\x0b\xbei\xbf\x1af[\x83p\ X18\xff\xff\x88\xf6\x00\x00\x17\x03\x01\x00\x1c\xbbt\xb3\x07}\xb0\xedqe\x1e\xe7;-\x03\x9bu\xb7\xb1r\xd2\x9e]\ xa1\xb8\xac\xa4v\x9a\x17\x03\x01\x00*\xed\x1f\xda\xa4# #Qe \x9a\xe9\xd6\xadn\xf4\x9b\xc4\xf0c ' \x01\xc4\x82\xdb\ xb2\x8d (\xa5\xd0\x06\x95\x13wo\x0f\x8e\x1c\xa6f\x1d\xdf\xe1x", ('74.125.71.19 ', 0))

4. Unpack is a function in a struct package, and the meaning of the format string is explained in the Pydoc struct. such as: '! ' Indicate Big-endian, can also use ' > ' instead; ' H ' means unsigned short.

Another, the code that intercepts the TCP packet, the IP header part is the same as above UDP parsing:

Tcp_header = packet[20:40]    #Now unpack them:)TCPH = Unpack ('! Hhllbbhhh', Tcp_header) Source_port=Tcph[0] Dest_port= Tcph[1] Sequence= Tcph[2] Acknowledgement= Tcph[3] doff_reserved= Tcph[4] Tcph_length= doff_reserved >> 4Print 'Source Port:'+ STR (source_port) +'Dest Port:'+ STR (dest_port) +'Sequence Number:'+ STR (sequence) +'Acknowledgement:'+ str (acknowledgement) +'TCP Header Length:'+Str (tcph_length) h_size= IHL * 4 + tcph_length * 4data_size= Len (packet)-h_size#get Data from the packetdata =packet[data_size:]Print 'Data:'+ Data

In addition, this method supports Pf_packet, so it can be used to crawl Ethernet data directly (only the X-nix system supports Pf_packet, so the Windows system has to use WPCAP).

If you want to crawl all the packets that pass through your network card, you need the NIC to enter "promiscuous mode". There are two methods: Manual "Ifconfig eth0 promisc" open; using IOCTL, modify socket Configuration "Ifr.ifr_flags |= iff_promisc".

Use Python to debug network programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.