Q:

How is this Calculating the Internet Checksum

I've been at this thing for an a few hours trying to figure it out:
data = b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'
v = sum(struct.unpack('!%sH' % int(len(data)/2), data))
for i in range(3):
    chksum += v & 0xffff
    v = v >> 16

I understand what it's doing until the for i in range part. One part that gets me is this: 
chksum += v & 0xffff

I'm sure v is 8 bytes, and I understand it's basically clearing bits. But it's half the size, so does it do upper or lower?
I don't know. Any help would be appreciated

A:

Python numbers are of unspecified bit width - they are of arbitrary size.
The "bitwise and" operator works as advertised, so let's take it step by step:

v & 0xffff - masking the least significant 16 bits (because 2^16-1=0xffff) of v is truncating v to mod 2^16
Add the truncated quantity to chksum
Handle any remaining carry by folding the sum on itself (the Internet Checksum is computed on 16-bit integers)

Your code is incomplete in handling (3), but the idea is the same. For more info, see the RFC.

