import hashlib, struct, time
 
ver = 56600
prev_block = "000000000000004950205cca66572178be57a990f58b9a6741ad683a36c9a46e"
mrkl_root = "a259dffe797877b1542f563e8a70c127c788245f7d8a88af05fda7f0d5290fef"
time_ = int(time.time()) # get current time
bits = 0x1fffffff
#bits = 0x1d00ffff # testnet
#bits = 0x207fffff # regtest
 
print "bits:", hex(bits)
exponent = bits >> 24 # peel off right 24 bits (4*6) to leave 1f alone for our exponent
print "exponent:", hex(exponent)
coefficient = bits & 0xffffff
print "coefficient:", hex(coefficient)
target_hexstr = '%064x' % (coefficient * 2**(8*(exponent - 3)))
print "target_hexstr:", target_hexstr
target_str = target_hexstr.decode('hex')
print "target int:", int(target_hexstr,16)

# PRETENDING...
nonce = 0
MAXNONCE = 2**32-1
#while nonce < 0x100000000:
while nonce < MAXNONCE:
    header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
          mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
    hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
    print nonce, hash[::-1].encode('hex')
    if hash[::-1] < target_str:
        print 'success with nonce =', nonce
        break
    nonce += 1

