Sniffed any good packets lately, dude? Hopefully you haven’t done this illegally, but rest assured that there are some who have. If you surf the Internet, you have probably visited companies’ net sites where they sell their products online: you give them your credit card number and they ship you the goods. That’s a convenient way to shop if you can ensure that your credit card number isn’t being “sniffed” up by wily hackers and used illicitly.
The Internet is an example of a packet-switched network. This means that information is sent in discrete groups of bits called packets from computer to computer. For example, when I send email to someone in the Philippines, my message (at the binary level) is broken up into packets, and routed packet by packet (not all at once) from computer to computer, ultimately to the recipient’s computer.
“Packet sniffing” refers to writing a program that grabs the individual packets that come your computer’s way and reads their contents. Now the term “packet sniffing” has obvious unethical connotations: usually it refers to writing a program that reads packets addressed to computers other than your own. But the principle is exactly the same when you only intercept those packets that are intended for you.
Let’s suppose that a network packet consists of three parts: a special start-of-packet bit pattern,the packet content, and a special end-of-packet bit pattern. Suppose that the start- and end-of-packet pattern are both 1000001 and that the packet content is no more than three consecutive sequences of 7 bits(1’s and 0’s). So, to write a program to “sniff” packets you need only to write a program that scans its input and “decodes” everything between pairs of 1000001.
For this problem assume that the content of each packet is binary representation of no more than three ASCII characters, each encoded in 7 “bits” (0’s and 1’s). Your task is to write a program that prints out the message (in English) that is being transmitted by a sequence of packets.
Take your input from the text file
prob4.in. Here’s a sample of its content:
You are guaranteed two things: (1) the encoding is correct, and (2) only lower case letters and punctuation are encoded in the packet.
Your program should direct its output to the screen. The correct output for the sample input above is:
with open('prob4.in') as f: for line in f: line = line.strip().replace('1000001', '') print(''.join([chr(int(c, 2)) for c in [line[i:i+7] for i in range(0, len(line), 7)]]))
Any advice on performance enhancement, solution simplification or that is topical is appreciated!