from__future__ImportUnicode_literalsImportJson fromTimeImportSleep, Time# chinese CodedefEncode_str (_STR):Try:return ByteArray(_str,"Utf-8").Hex()except Exception asE:Print(e)# Chinese decodingdefDecode_str (_STR):Try:return ByteArray. Fromhex (_STR). Decode ("Utf-8")except Exception asE:Print(e) Math_bytecode= # 6060604052341561000f57600080fd5b5b6105078061001f6000396000f300606060405263ffffffff7c01000000000000000000000000000000000 000000000000000000000006000350416630c90d8448114610048578063d10fa94614610111575b600080fd5b341561005357600080fd5b6100996004 6024813581810190830135806020601f820181900481020160405190810160405281815292919060208401838380828437509496506101e3955050505 05050565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100d65780820151818401525b602001 6100bd565b50505050905090810190601f1680156101035780820380516001836020036101000a031916815260200191505b509250505060405180910 390f35b341561011c57600080fd5b61016260046024813581810190830135806020601f82018190048102016040519081016040528181529291906020 8401838380828437509496506102fa95505050505050565b6040518215158152604060208201818152908201838181518152602001915080519060200 19080838360005b838110156101a75780820151818401525b60200161018e565b50505050905090810190601f1680156101d457808203805160018360 20036101000a031916815260200191505b50935050505060405180910390f35b6101eb610429565b600080836040518082805190602001908083835b6020831061021f57805182525b601f199 0920191602091820191016101ff565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040 5180910390209050806001018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526 02001828054600181600116156101000203166002900480156102ec5780601f106102c1576101008083540402835291602001916102ec565b82019190 6000526020600020905b8154815290600101906020018083116102cf57829003601f168201915b505050505091505b50919050565b600061030461042 9565b60408051908101604052808481526020016040805190810160405280600981526020017fe4b88de5ad98e59ca800000000000000000000000000 000000000000000000008152508152506000846040518082805190602001908083835b6020831061038157805182525b601f199092019160209182019 101610361565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040519081900390208151 819080516103c992916020019061043b565b506020820151816001019080516103e492916020019061043b565b50905050600060408051908101604052600681527f6f6b6f6b3131000000000 0000000000000000000000000000000000000000000602082015290925090505b915091565b60206040519081016040526000815290565b8280546001 81600116156101000203166002900490600052602060002090601f016020900481019282601f1061047c57805160ff19168380011785556104a9565b8 28001600101855582156104a9579182015b828111156104a957825182559160200191906001019061048e565b5b506104b69291506104ba565b509056 5b6104d891905b808211156104b657600081556001016104c0565b5090565b905600a165627a7a72305820faf711785ac7369219291016aa91535aa22 004c60da82a8fcd956777c3b7a7000029Math_abi=Json.loads ("""[ {"constant": false,"Inputs": [ {"name": "_type","type": "Bytes32" }, {"name": "_metadata","type": "Bytes32" } ],"name": "Publish","outputs": [],"payable": false," statemutability": "Nonpayable",' type ': ' function ' }, {"Anonymous": false,"Inputs": [ {"indexed": True,"name": "_type","type": "Bytes32" }, {"indexed": false,"name": "_created","type": "uint256" }, {"indexed": false,"name": "_metadata","type": "Bytes32" } ],"name": "Articleoperationlog",' type ': ' Event ' } ] """)ImportWeb3.personal asWp fromWeb3ImportWEB3, HTTPPROVIDERWEB3=WEB3 (Httpprovider (' http://localhost:8545 ')) Web3.eth.defaultAccount=B' 0x3364aa6342c8c18 'Print("Unlockaccount") p=Wp. Personal (WEB3)Print(P.unlockaccount ("0x336394f4b93aa6342c8c18","123456"))Print("Unlockaccount OK") mathcontract=Web3.eth.contract (ABI=Math_abi,# Bytecode=math_bytecode,)# deploy_txn = Mathcontract.deploy ()# transaction={# ' from ': B ' 0x336495440ce8dd55029bd394f4b93aa6342c8c18 ',# ' value ': 12345,# },# args= (' DGD ',)Print("isconnected:", web3.isconnected ())# Print (DEPLOY_TXN)# deploy_receipt = Web3.eth.getTransactionReceipt (" 0x71ce48069f2e17079c2cf016f40bd47d995f4c77aab74d12ae89a9cf13cfeb77 ")# Print (deploy_receipt, "\ n")# Print (DEPLOY_TXN)# deploy_receipt = Web3.eth.getTransactionReceipt (b " 0xb2e4aacfbd147ddd6f807c0f4d47cc77c6a21ce7e2c9cbd17b7ba3d410473bd4 ")# Print (deploy_receipt)# assert Deploy_receipt is not None# deploy_receipt["contractaddress"]# math_contract = Mathcontract (address=deploy_receipt["contractaddress"])ImportRedefMy_callback (a):# Print (a)Args=A.get ("args")Print(Decode_str (Re.split (r "\w+", Args.get ("_type"))[0]))Print(Str(Args.get ("_created")))Print(Decode_str (Re.split (r "\w+", Args.get ("_metadata"))[0]))Print("\ n")defMy_callback1 (a):Print("\n\n\n\npastevents ")# Print (a)Args=A.get ("args")Print(Decode_str (Re.split (r "\w+", Args.get ("_type"))[0]))Print(Str(Args.get ("_created")))Print(Decode_str (Re.split (r "\w+", Args.get ("_metadata"))[0])) Math_contract=Mathcontract (Address="0x8851accb27fc4b149bda886d6d579f2fb5c1a492")# Math_contract.on ("Articleoperationlog", {' filter ': {' _type ': ' Hello '}}). Watch (My_callback)# Math_contract.on ("Articleoperationlog"). Watch (My_callback)# Math_contract.on ("Articleoperationlog", {"filter": {"_type": Encode_str ("Hello")}}). Watch (My_callback)# History LogMath_contract.pastevents ("Articleoperationlog", {"Filter": {"_type": Encode_str ("Hello1")}}). Watch (My_callback1)# History Log# math_contract.pastevents ("Articleoperationlog"). Watch (My_callback1) while 1:# Math_contract.transact (). Publish (Encode_str ("Hello1"), Encode_str ("Hello1 Shanghai" ) Print(Time ()) Sleep (2)# _a = "1234562"# Print (Math_contract.encodeabi ("Adddna", args= (_a,_a,_a)))# Send a TX to the blockchain# Print (Math_contract.transact (). Adddna (_a, _a, _a))# Send a TX to local execution, will not be written to the blockchain# Print (Math_contract.call (). Adddna (_a, _a, _a))# Print (Math_contract.call (). Getdna (_a))
Python Smart contract Log operations