I'm looking to improve the efficiency of my code. Although my current method works, I feel it can be improved.
if ouroraddrlen == (4,):
ouropip = struct.unpack(">bbbb", payload[6:10]) # Need to change this to accept ipv6 as well
print "Our I.P : ", ouropip
notheirip = struct.unpack(">b", payload[10])
print "No. of their I.P's : ", notheirip
theiroripv = struct.unpack(">b", payload[11])
print "Their I.P version:: ", theiroripv
theiroraddrlen = struct.unpack(">b", payload[12])
print "Length of their Ip : ", theiroraddrlen
theirfirstip = struct.unpack(">bbbb", payload[13:17])
print "First Ip : ", theirfirstip
theirsecondip = struct.unpack(">bbbb", payload[18:22])
print "Second Ip : ", theirsecondip
The output is:
Time : (1401734263,) Our onion address : Ip version : (4,) Ip length : (4,) Our I.P : ( ) No. of their I.P's : (2,) Their I.P version:: (4,) Length of their Ip : (4,) First Ip : ( ) Second Ip : ( )
I have removed the real IPs, but they are just IPV4 addresses.
However, what I am wondering, is if it is possible to include an if statement after this section of code:
notheirip = struct.unpack(">b", payload[10])
print "No. of their I.P's : ", notheirip
where if the notheirip
is greater than zero and depending on the length of:
theiroraddrlen = struct.unpack(">b", payload[12])
print "Length of their Ip : ", theiroraddrlen
which would be either 4 or 16, then it would set the payload values of the next section.
For example, if notheirip = (2,)
and theiroraddrlen = (4,)
, then I would want it to print out
theirip = struct.unpack(">b <<
the number of b required so either 4 or 16 and then the range, this will always start at 13 and go up to either 4 or 16 in the future and loop until all the IPs are displayed.