import hashlib import random import string import sys
CHARS = string.letters + string.digits
defVerifymd5(substr, str_len, start=0, size=20): global CHARS flag = 1 while flag == 1: rnds = ''.join(random.choice(CHARS) for _ in range(size)) md5 = hashlib.md5(rnds) if md5.hexdigest()[start: start+str_len] == substr: flag = 0 return rnds
defxxor(l): r = 0 for i in l: r ^= i return r
if __name__ == '__main__': p = remote('199.247.6.180',14002) s1 = p.recvline_contains('md5') substr = s1[-6:-1] str_len = len(substr) s2 = Verifymd5(substr, str_len, 0, 20) print s2 p.sendline(s2) for i in xrange(100): while1: try: print p.recvline() p.recvuntil('the game: ') #handle the exception using try except r = p.recvline() print r r = r.lstrip('[') r = r[0:-2] r = map(int, r.strip().split(', ')) #print r xor = 0 for j in xrange(15): #print r[j] xor ^= r[j] print"The XOR of the piles: %d" % xor if (xor == 0): break else: for mx in xrange(15): for d in xrange(r[mx] + 1): l = list(r) l[mx] -= d if (xxor(l) == 0): q = [d, mx] break print"I choose Pile: %d" % q[1] print"I choose Quantity: %d" % q[0] p.sendline("%d" % q[1]) #p.interactive() p.sendline("%d" % q[0]) r[q[1]] -= q[0] except: print p.recvall() break else: break