{"id":10021265,"date":"2026-02-20T14:46:46","date_gmt":"2026-02-20T06:46:46","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021265"},"modified":"2026-02-20T14:46:46","modified_gmt":"2026-02-20T06:46:46","slug":"python-%e5%8c%ba%e5%9d%97%e9%93%be%e5%bc%80%e5%8f%91%ef%bc%9a%e6%99%ba%e8%83%bd%e5%90%88%e7%ba%a6%e4%b8%8e%e5%8e%bb%e4%b8%ad%e5%bf%83%e5%8c%96%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021265","title":{"rendered":"Python \u533a\u5757\u94fe\u5f00\u53d1\uff1a\u667a\u80fd\u5408\u7ea6\u4e0e\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528"},"content":{"rendered":"<h2>Python \u533a\u5757\u94fe\u5f00\u53d1\uff1a\u667a\u80fd\u5408\u7ea6\u4e0e\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528<\/h2>\n<p>\u533a\u5757\u94fe\u6280\u672f\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u91cd\u8981\u521b\u65b0\uff0cPython \u5728\u533a\u5757\u94fe\u5f00\u53d1\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 Python \u5728\u533a\u5757\u94fe\u5f00\u53d1\u4e2d\u7684\u5e94\u7528\uff0c\u5305\u62ec\u667a\u80fd\u5408\u7ea6\u3001\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\u3001\u52a0\u5bc6\u7b97\u6cd5\u7b49\u6838\u5fc3\u6280\u672f\u3002<\/p>\n<h3>\u533a\u5757\u94fe\u57fa\u7840<\/h3>\n<p>\u533a\u5757\u94fe\u662f\u5206\u5e03\u5f0f\u8d26\u672c\u6280\u672f\uff0c\u901a\u8fc7\u5bc6\u7801\u5b66\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u4e0d\u53ef\u7be1\u6539\u6027\u3002<\/p>\n<pre><code class=\"python\">import hashlib\nimport json\nfrom time import time\n\nclass Block:\n    def __init__(self, index, transactions, previous_hash):\n        self.index = index\n        self.timestamp = time()\n        self.transactions = transactions\n        self.previous_hash = previous_hash\n        self.nonce = 0\n        self.hash = self.calculate_hash()\n    \n    def calculate_hash(self):\n        block_string = json.dumps(self.__dict__, sort_keys=True).encode()\n        return hashlib.sha256(block_string).hexdigest()\n\ndef blockchain_basic_demo():\n    print(&quot;\u533a\u5757\u94fe\u57fa\u7840\u6f14\u793a:&quot;)\n    \n    genesis_block = Block(0, [], &quot;0&quot;)\n    print(f&quot;\u521b\u4e16\u533a\u5757\u54c8\u5e0c: {genesis_block.hash}&quot;)\n\nblockchain_basic_demo()<\/code><\/pre>\n<h3>\u667a\u80fd\u5408\u7ea6<\/h3>\n<p>\u667a\u80fd\u5408\u7ea6\u662f\u8fd0\u884c\u5728\u533a\u5757\u94fe\u4e0a\u7684\u81ea\u52a8\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"python\">class SmartContract:\n    def __init__(self, address, code):\n        self.address = address\n        self.code = code\n        self.storage = {}\n        self.balance = 0\n    \n    def execute(self, function, args):\n        if function in self.code:\n            return self.code[function](self, *args)\n        return None\n    \n    def set_value(self, key, value):\n        self.storage[key] = value\n    \n    def get_value(self, key):\n        return self.storage.get(key)\n\ndef smart_contract_demo():\n    print(&quot;\u667a\u80fd\u5408\u7ea6\u6f14\u793a:&quot;)\n    \n    contract_code = {\n        'set_value': SmartContract.set_value,\n        'get_value': SmartContract.get_value\n    }\n    \n    contract = SmartContract('0x123', contract_code)\n    contract.execute('set_value', ('name', '\u5f20\u4e09'))\n    \n    value = contract.execute('get_value', ('name',))\n    print(f&quot;\u5408\u7ea6\u5b58\u50a8\u503c: {value}&quot;)\n\nsmart_contract_demo()<\/code><\/pre>\n<h3>\u52a0\u5bc6\u7b97\u6cd5<\/h3>\n<p>\u52a0\u5bc6\u7b97\u6cd5\u4fdd\u62a4\u533a\u5757\u94fe\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<pre><code class=\"python\">from cryptography.hazmat.primitives.asymmetric import rsa\nfrom cryptography.hazmat.primitives import hashes\nfrom cryptography.hazmat.primitives.asymmetric import padding\n\ndef cryptography_demo():\n    print(&quot;\u52a0\u5bc6\u7b97\u6cd5\u6f14\u793a:&quot;)\n    \n    private_key = rsa.generate_private_key(\n        public_exponent=65537,\n        key_size=2048\n    )\n    \n    public_key = private_key.public_key()\n    \n    message = b&quot;Hello Blockchain!&quot;\n    encrypted = public_key.encrypt(\n        message,\n        padding.OAEP(\n            mgf=padding.MGF1(algorithm=hashes.SHA256()),\n            algorithm=hashes.SHA256(),\n            label=None\n        )\n    )\n    \n    decrypted = private_key.decrypt(\n        encrypted,\n        padding.OAEP(\n            mgf=padding.MGF1(algorithm=hashes.SHA256()),\n            algorithm=hashes.SHA256(),\n            label=None\n        )\n    )\n    \n    print(f&quot;\u52a0\u5bc6\u6d88\u606f: {encrypted.hex()}&quot;)\n    print(f&quot;\u89e3\u5bc6\u6d88\u606f: {decrypted.decode()}&quot;)\n\ncryptography_demo()<\/code><\/pre>\n<h3>\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528<\/h3>\n<p>\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\u8fd0\u884c\u5728\u533a\u5757\u94fe\u7f51\u7edc\u4e0a\uff0c\u4e0d\u4f9d\u8d56\u4e2d\u5fc3\u5316\u670d\u52a1\u5668\u3002<\/p>\n<pre><code class=\"python\">class DApp:\n    def __init__(self, blockchain):\n        self.blockchain = blockchain\n        self.users = {}\n    \n    def register_user(self, username, public_key):\n        self.users[username] = public_key\n    \n    def create_transaction(self, from_user, to_user, amount):\n        if from_user in self.users and to_user in self.users:\n            transaction = {\n                'from': from_user,\n                'to': to_user,\n                'amount': amount,\n                'timestamp': time()\n            }\n            return transaction\n        return None\n\ndef dapp_demo():\n    print(&quot;\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\u6f14\u793a:&quot;)\n    \n    blockchain = []\n    dapp = DApp(blockchain)\n    \n    dapp.register_user('alice', 'public_key_1')\n    dapp.register_user('bob', 'public_key_2')\n    \n    transaction = dapp.create_transaction('alice', 'bob', 100)\n    print(f&quot;\u4ea4\u6613: {transaction}&quot;)\n\ndapp_demo()<\/code><\/pre>\n<h3>\u533a\u5757\u94fe\u67b6\u6784<\/h3>\n<pre style=\"display:none;\"><code class=\"mermaid\">graph TD\n    A[\u7528\u6237] --&gt; B[\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528]\n    B --&gt; C[\u667a\u80fd\u5408\u7ea6]\n    C --&gt; D[\u533a\u5757\u94fe\u7f51\u7edc]\n    D --&gt; E[\u8282\u70b9 1]\n    D --&gt; F[\u8282\u70b9 2]\n    D --&gt; G[\u8282\u70b9 N]\n    E --&gt; H[\u5171\u8bc6\u673a\u5236]\n    F --&gt; H\n    G --&gt; H<\/code><\/pre>\n<h3>\u5171\u8bc6\u673a\u5236<\/h3>\n<p>\u5171\u8bc6\u673a\u5236\u786e\u4fdd\u533a\u5757\u94fe\u7f51\u7edc\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<pre><code class=\"python\">def consensus_demo():\n    print(&quot;\u5171\u8bc6\u673a\u5236\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u5de5\u4f5c\u91cf\u8bc1\u660e PoW&quot;)\n    print(&quot;2. \u6743\u76ca\u8bc1\u660e PoS&quot;)\n    print(&quot;3. \u59d4\u6258\u6743\u76ca\u8bc1\u660e DPoS&quot;)\n    print(&quot;4. \u5b9e\u7528\u62dc\u5360\u5ead\u5bb9\u9519 PBFT&quot;)\n\nconsensus_demo()<\/code><\/pre>\n<h3>Web3 \u96c6\u6210<\/h3>\n<p>Web3 \u96c6\u6210\u8fde\u63a5 Python \u5e94\u7528\u4e0e\u533a\u5757\u94fe\u7f51\u7edc\u3002<\/p>\n<pre><code class=\"python\">def web3_integration_demo():\n    print(&quot;Web3 \u96c6\u6210\u6f14\u793a:&quot;)\n    \n    print(&quot;1. \u8fde\u63a5\u4ee5\u592a\u574a\u7f51\u7edc&quot;)\n    print(&quot;2. \u8c03\u7528\u667a\u80fd\u5408\u7ea6&quot;)\n    print(&quot;3. \u53d1\u9001\u4ea4\u6613&quot;)\n    print(&quot;4. \u76d1\u542c\u4e8b\u4ef6&quot;)\n\nweb3_integration_demo()<\/code><\/pre>\n<h3>\u4ee3\u5e01\u5f00\u53d1<\/h3>\n<p>\u4ee3\u5e01\u5f00\u53d1\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u52a0\u5bc6\u8d27\u5e01\u3002<\/p>\n<pre><code class=\"python\">class Token:\n    def __init__(self, name, symbol, total_supply):\n        self.name = name\n        self.symbol = symbol\n        self.total_supply = total_supply\n        self.balances = {}\n    \n    def mint(self, address, amount):\n        if address not in self.balances:\n            self.balances[address] = 0\n        self.balances[address] += amount\n    \n    def transfer(self, from_address, to_address, amount):\n        if self.balances.get(from_address, 0) &gt;= amount:\n            self.balances[from_address] -= amount\n            if to_address not in self.balances:\n                self.balances[to_address] = 0\n            self.balances[to_address] += amount\n            return True\n        return False\n\ndef token_demo():\n    print(&quot;\u4ee3\u5e01\u5f00\u53d1\u6f14\u793a:&quot;)\n    \n    token = Token('MyToken', 'MTK', 1000000)\n    token.mint('alice', 1000)\n    \n    success = token.transfer('alice', 'bob', 500)\n    print(f&quot;\u8f6c\u8d26\u6210\u529f: {success}&quot;)\n    print(f&quot;Alice \u4f59\u989d: {token.balances.get('alice', 0)}&quot;)\n    print(f&quot;Bob \u4f59\u989d: {token.balances.get('bob', 0)}&quot;)\n\ntoken_demo()<\/code><\/pre>\n<h3>NFT \u5f00\u53d1<\/h3>\n<p>NFT \u5f00\u53d1\u521b\u5efa\u4e0d\u53ef\u66ff\u4ee3\u7684\u4ee3\u5e01\u3002<\/p>\n<pre><code class=\"python\">class NFT:\n    def __init__(self, contract_address):\n        self.contract_address = contract_address\n        self.tokens = {}\n        self.owners = {}\n    \n    def mint(self, token_id, owner, metadata):\n        self.tokens[token_id] = metadata\n        self.owners[token_id] = owner\n    \n    def transfer(self, token_id, from_owner, to_owner):\n        if self.owners.get(token_id) == from_owner:\n            self.owners[token_id] = to_owner\n            return True\n        return False\n\ndef nft_demo():\n    print(&quot;NFT \u5f00\u53d1\u6f14\u793a:&quot;)\n    \n    nft = NFT('0x123')\n    nft.mint(1, 'alice', {'name': 'Digital Art', 'image': 'ipfs:\/\/...'})\n    \n    success = nft.transfer(1, 'alice', 'bob')\n    print(f&quot;\u8f6c\u8d26\u6210\u529f: {success}&quot;)\n    print(f&quot;\u6240\u6709\u8005: {nft.owners[1]}&quot;)\n\nnft_demo()<\/code><\/pre>\n<h3>DeFi \u5e94\u7528<\/h3>\n<p>DeFi \u5e94\u7528\u63d0\u4f9b\u53bb\u4e2d\u5fc3\u5316\u91d1\u878d\u670d\u52a1\u3002<\/p>\n<pre><code class=\"python\">class DeFi:\n    def __init__(self):\n        self.pools = {}\n        self.users = {}\n    \n    def create_pool(self, token_a, token_b, amount_a, amount_b):\n        pool_id = f&quot;{token_a}-{token_b}&quot;\n        self.pools[pool_id] = {\n            'token_a': token_a,\n            'token_b': token_b,\n            'amount_a': amount_a,\n            'amount_b': amount_b\n        }\n        return pool_id\n    \n    def add_liquidity(self, pool_id, user, amount_a, amount_b):\n        if pool_id in self.pools:\n            pool = self.pools[pool_id]\n            pool['amount_a'] += amount_a\n            pool['amount_b'] += amount_b\n            \n            if user not in self.users:\n                self.users[user] = {}\n            if pool_id not in self.users[user]:\n                self.users[user][pool_id] = 0\n            self.users[user][pool_id] += 1\n\ndef defi_demo():\n    print(&quot;DeFi \u5e94\u7528\u6f14\u793a:&quot;)\n    \n    defi = DeFi()\n    pool_id = defi.create_pool('ETH', 'USDT', 1000, 100000)\n    \n    defi.add_liquidity(pool_id, 'alice', 100, 10000)\n    print(f&quot;\u6d41\u52a8\u6027\u6c60: {defi.pools[pool_id]}&quot;)\n\ndefi_demo()<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>Python \u533a\u5757\u94fe\u5f00\u53d1\u901a\u8fc7\u667a\u80fd\u5408\u7ea6\u3001\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\u7b49\u6280\u672f\uff0c\u5b9e\u73b0\u4e86\u521b\u65b0\u7684\u5206\u5e03\u5f0f\u5e94\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u533a\u5757\u94fe\u6280\u672f\uff0c\u5bf9\u4e8e\u6784\u5efa\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u533a\u5757\u94fe\u5e73\u53f0\u548c\u5f00\u53d1\u6846\u67b6\uff0c\u5e73\u8861\u5b89\u5168\u6027\u548c\u6027\u80fd\u3002\u826f\u597d\u7684\u533a\u5757\u94fe\u5f00\u53d1\u5b9e\u8df5\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7684\u53ef\u9760\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047614872\">https:\/\/segmentfault.com\/a\/1190000047614872<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u533a\u5757\u94fe\u5f00\u53d1\uff1a\u667a\u80fd\u5408\u7ea6\u4e0e\u53bb\u4e2d\u5fc3\u5316\u5e94\u7528 \u533a\u5757\u94fe\u6280\u672f\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u91cd\u8981\u521b\u65b0\uff0cPython \u5728\u533a\u5757\u94fe\u5f00\u53d1 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021265","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021265"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021265\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}