Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Understanding Code
#1
Hello Everyone
So i had to create a program that reads pcap files, takes the IP packet and shows it in a certain format. I had to ask for help and the person that helped me, didn't really explain what each line is doing. So if anyone can help me ill appreciate it.

def main():
	try:
		sys.argv[1]
		dmp_file = sys.argv[1]
		fp_dmp_file = open(dmp_file,'rb')
		pcap = dpkt.pcap.Reader(fp_dmp_file)
		pack_num = 0
		total_size = 0
		for ts, buf  in pcap:
			pkt_len = len(buf)
			eth = dpkt.ethernet.Ethernet(buf)
			ip = eth.data
			src_ip = socket.inet_ntop(socket.AF_INET, ip.src)
			dst_ip = socket.inet_ntop(socket.AF_INET, ip.dst)
			src_port = ip.data.sport
			dst_port = ip.data.dport
			ttl = ip.ttl
			protocol = ip.get_proto(ip.p).__name__
			print_packet_info(ts,src_ip,src_port,dst_ip,dst_port,protocol,pkt_len,ttl)
			pack_num += 1
			total_size += pkt_len
		avg_size = total_size/pack_num
		print_summary(pack_num,total_size,avg_size)
	except Exception as e:
		#print(e)
		print("Error: please supply pcap filename!\n")

if __name__ == '__main__':
	main()
Reply
#2
Why don't you start by adding comments about what you think is happening, and we can correct any misconceptions from there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code understanding: Need help in understanding dictionary code jt123 0 467 Jul-09-2023, 01:13 PM
Last Post: jt123
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,739 May-09-2023, 08:47 AM
Last Post: buran
  Understanding a piece of code Michael1 4 1,401 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Beginner: I need help understanding few lines of a code. hop_090 1 1,682 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 1,433 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123
  Need help understanding simple Array code. Please. stluwa 1 2,201 Apr-13-2019, 07:16 PM
Last Post: loomski
  Trouble Understanding Why This Code Works crocolicious 2 2,690 Apr-09-2019, 05:24 PM
Last Post: crocolicious
  Help Understanding Portion of Code caroline_d_124 3 2,703 Jan-15-2019, 12:12 AM
Last Post: caroline_d_124
  Understanding GerbMerge code djsb 1 2,387 Oct-27-2018, 02:04 PM
Last Post: Larz60+
  Understanding the code Eugine 1 1,929 Oct-22-2018, 05:56 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020