{"id":3955,"date":"2024-12-03T10:51:35","date_gmt":"2024-12-03T02:51:35","guid":{"rendered":"https:\/\/a88o.com\/3955.html"},"modified":"2024-12-03T10:51:35","modified_gmt":"2024-12-03T02:51:35","slug":"%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e5%8c%ba%e5%9d%97%e9%93%bedemo%e6%95%99%e7%a8%8b%ef%bc%9a%e4%b8%80%e6%ad%a5%e6%ad%a5%e6%95%99%e4%bd%a0%e6%90%ad%e5%bb%ba%e8%87%aa%e5%b7%b1%e7%9a%84%e5%8c%ba","status":"publish","type":"post","link":"https:\/\/www.a88o.com\/3955.html","title":{"rendered":"\u6700\u7b80\u5355\u7684\u533a\u5757\u94fedemo\u6559\u7a0b\uff1a\u4e00\u6b65\u6b65\u6559\u4f60\u642d\u5efa\u81ea\u5df1\u7684\u533a\u5757\u94fe\u7cfb\u7edf"},"content":{"rendered":"
\n

\u533a\u5757\u94fe\u6280\u672f\u4f5c\u4e3a\u5f53\u4eca\u79d1\u6280\u53d1\u5c55\u7684\u70ed\u70b9\u4e4b\u4e00\uff0c\u5176\u53bb\u4e2d\u5fc3\u5316\u3001\u900f\u660e\u3001\u5b89\u5168\u7b49\u7279\u70b9\u5438\u5f15\u7740\u5927\u91cf\u5f00\u53d1\u8005\u548c\u4f01\u4e1a\u7684\u5173\u6ce8\u3002\u672c\u6559\u7a0b\u65e8\u5728\u5e2e\u52a9\u521d\u5b66\u8005\u901a\u8fc7\u4e00\u6b65\u4e00\u6b65\u7684\u6307\u5bfc\uff0c\u642d\u5efa\u81ea\u5df1\u7684\u533a\u5757\u94fedemo\u7cfb\u7edf\u3002\u672c\u6587\u5c06\u6db5\u76d6\u533a\u5757\u94fe\u7684\u57fa\u672c\u6982\u5ff5\u3001\u642d\u5efa\u5de5\u5177\u3001\u4ee5\u53ca\u5404\u4e2a\u6b65\u9aa4\u7684\u8be6\u7ec6\u8bf4\u660e\uff0c\u5e76\u901a\u8fc7\u8868\u683c\u603b\u7ed3\u77e5\u8bc6\u70b9\u3002<\/p>\n

\"\u6700\u7b80\u5355\u7684\u533a\u5757\u94fedemo\u6559\u7a0b\uff1a\u4e00\u6b65\u6b65\u6559\u4f60\u642d\u5efa\u81ea\u5df1\u7684\u533a\u5757\u94fe\u7cfb\u7edf\"<\/p>\n

1. \u7406\u89e3\u533a\u5757\u94fe<\/h3>\n

\u5728\u5f00\u59cb\u642d\u5efa\u533a\u5757\u94fe\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u4e86\u89e3\u4ec0\u4e48\u662f\u533a\u5757\u94fe\u3002\u533a\u5757\u94fe\u662f\u4e00\u4e2a\u53bb\u4e2d\u5fc3\u5316\u7684\u5206\u5e03\u5f0f\u8d26\u672c\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728\u533a\u5757\u4e2d\uff0c\u5e76\u901a\u8fc7\u94fe\u5f0f\u7ed3\u6784\uff08\u5373\u94fe\u6761\uff09\u8fde\u63a5\u3002\u6bcf\u4e2a\u533a\u5757\u5305\u542b\uff1a
\n– \u4e8b\u52a1\u6570\u636e<\/strong>\uff1a\u5305\u542b\u5b9e\u9645\u7684\u4ea4\u6613\u4fe1\u606f\u3002
\n– \u65f6\u95f4\u6233<\/strong>\uff1a\u533a\u5757\u751f\u6210\u7684\u65f6\u95f4\u3002
\n– \u54c8\u5e0c\u503c<\/strong>\uff1a\u5f53\u524d\u533a\u5757\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u4ee5\u53ca\u4e0a\u4e00\u533a\u5757\u7684\u54c8\u5e0c\u503c\u3002
\n\u533a\u5757\u94fe\u7684\u7279\u5f81\u5305\u62ec\uff1a
\n– \u53bb\u4e2d\u5fc3\u5316<\/strong>\uff1a\u6570\u636e\u4e0d\u7531\u5355\u4e00\u8282\u70b9\u7ef4\u62a4\uff0c\u800c\u662f\u7531\u591a\u4e2a\u8282\u70b9\u5171\u540c\u7ef4\u62a4\u3002
\n– \u5b89\u5168\u6027<\/strong>\uff1a\u901a\u8fc7\u5bc6\u7801\u5b66\u624b\u6bb5\u4fdd\u62a4\u6570\u636e\u4e0d\u88ab\u7be1\u6539\u3002
\n– \u900f\u660e\u6027<\/strong>\uff1a\u4ea4\u6613\u8bb0\u5f55\u5411\u6240\u6709\u4eba\u516c\u5f00\uff0c\u4f46\u4fe1\u606f\u662f\u4ee5\u533f\u540d\u65b9\u5f0f\u8fdb\u884c\u3002<\/p>\n

2. \u9009\u62e9\u5f00\u53d1\u5de5\u5177<\/h3>\n

\u4e3a\u4e86\u642d\u5efa\u4e00\u4e2a\u533a\u5757\u94fedemo\uff0c\u6211\u4eec\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5f00\u53d1\u5de5\u5177\u548c\u7f16\u7a0b\u8bed\u8a00\u3002\u6b64\u5904\u6211\u4eec\u63a8\u8350\u4f7f\u7528Python\uff0c\u56e0\u4e3a\u5b83\u7684\u8bed\u6cd5\u7b80\u6d01\u4e14\u6709\u5f3a\u5927\u7684\u5e93\u652f\u6301\u3002
\n– \u7f16\u7a0b\u8bed\u8a00<\/strong>\uff1aPython
\n– \u5f00\u53d1\u73af\u5883<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\uff0c\u5982VS Code\u3001PyCharm\u7b49\u3002
\n– \u533a\u5757\u94fe\u5e93<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Flask<\/code>\u642d\u5efaAPI\uff0c\u53ef\u9009requests<\/code>\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u3002<\/p>\n

3. \u6b65\u9aa4\u8be6\u89e3<\/h3>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u533a\u5757\u94fe\u7cfb\u7edf\u7684\u642d\u5efa\uff1a<\/p>\n

3.1 \u521b\u5efa\u533a\u5757\u7c7b<\/h4>\n

\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u533a\u5757\u7c7b\uff0c\u7528\u4e8e\u5b58\u50a8\u533a\u5757\u7684\u6570\u636e\u7ed3\u6784\u3002
\n“`python
\nimport time
\nimport hashlib
\nclass Block:
\n def init<\/strong>(self, index, previoushash, timestamp, data, hash):
\n self.index = index
\n self.previous<\/em>hash = previous_hash
\n self.timestamp = timestamp
\n self.data = data
\n self.hash = hash<\/p>\n

@staticmethod\ndef calculate_hash(index, previous_hash, timestamp, data):\n    value = str(index) + previous_hash + str(timestamp) + str(data)\n    return hashlib.sha256(value.encode()).hexdigest()\n<\/code><\/pre>\n

“`<\/p>\n

3.2 \u521b\u5efa\u533a\u5757\u94fe\u7c7b<\/h4>\n

\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u533a\u5757\u94fe\u7c7b\uff0c\u7528\u4e8e\u7ba1\u7406\u533a\u5757\u548c\u7ef4\u62a4\u94fe\u7ed3\u6784\u3002
\npython
\nclass Blockchain:
\n def __init__(self):
\n self.chain = []
\n self.create_block(previous_hash='0') # \u521b\u4e16\u5757
\n def create_block(self, data):
\n index = len(self.chain) + 1
\n previous_hash = self.chain[-1].hash if self.chain else '0'
\n timestamp = time.time()
\n hash = Block.calculate_hash(index, previous_hash, timestamp, data)
\n new_block = Block(index, previous_hash, timestamp, data, hash)
\n self.chain.append(new_block)
\n return new_block
\n<\/code><\/p>\n

3.3 \u521b\u5efa\u533a\u5757\u94fe\u5b9e\u4f8b\u4e0e\u6dfb\u52a0\u533a\u5757<\/h4>\n

\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u533a\u5757\u94fe\u5b9e\u4f8b\u5e76\u6dfb\u52a0\u533a\u5757\uff0c\u793a\u4f8b\u5982\u4e0b\u3002
\n“`python
\ndef main():
\n blockchain = Blockchain()<\/p>\n

blockchain.create_block(\"Transaction 1\")\nblockchain.create_block(\"Transaction 2\")\nfor block in blockchain.chain:\n    print(f\"Index: {block.index}, Previous Hash: {block.previous_hash}, Timestamp: {block.timestamp}, Data: {block.data}, Hash: {block.hash}\")\n<\/code><\/pre>\n

if name<\/strong> == ‘main<\/strong>‘:
\n main()
\n“`<\/p>\n

3.4 \u542f\u52a8Web\u670d\u52a1<\/h4>\n

\u4e3a\u4e86\u4f7f\u533a\u5757\u94fe\u7684\u64cd\u4f5c\u66f4\u65b9\u4fbf\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Flask\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u3002
\npython
\nfrom flask import Flask, jsonify
\napp = Flask(__name__)
\nblockchain = Blockchain()
\n@app.route('\/mine_block', methods=['GET'])
\ndef mine_block():
\n block = blockchain.create_block(\"New Transaction!\")
\n return jsonify({
\n 'message': 'New block mined!',
\n 'index': block.index,
\n 'previous_hash': block.previous_hash,
\n 'timestamp': block.timestamp,
\n 'data': block.data,
\n 'hash': block.hash
\n })
\n@app.route('\/chain', methods=['GET'])
\ndef get_chain():
\n chain_data = []
\n for block in blockchain.chain:
\n chain_data.append({
\n 'index': block.index,
\n 'previous_hash': block.previous_hash,
\n 'timestamp': block.timestamp,
\n 'data': block.data,
\n 'hash': block.hash
\n })
\n return jsonify({'chain': chain_data})
\nif __name__ == '__main__':
\n app.run(port=5000)
\n<\/code><\/p>\n

4. \u6d4b\u8bd5\u533a\u5757\u94fe<\/h3>\n

\u5f53\u524d\u670d\u52a1\u542f\u52a8\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Postman\u6216\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/127.0.0.1:5000\/mine_block<\/code> \u6765\u6316\u6398\u65b0\u533a\u5757\uff0c\u8bbf\u95eehttp:\/\/127.0.0.1:5000\/chain<\/code> \u6765\u67e5\u770b\u5f53\u524d\u94fe\u7684\u6240\u6709\u533a\u5757\u3002<\/p>\n

5. \u7ed3\u675f\u8bed<\/h3>\n

\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u642d\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u533a\u5757\u94fedemo\u3002\u8fd9\u4e2ademo\u867d\u7136\u6bd4\u8f83\u57fa\u7840\uff0c\u4f46\u5b83\u4e3a\u7406\u89e3\u533a\u5757\u94fe\u6280\u672f\u5960\u5b9a\u4e86\u57fa\u7840\u3002\u540e\u7eed\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6269\u5c55\u66f4\u591a\u529f\u80fd\uff0c\u5982\u5b9e\u73b0\u5171\u8bc6\u673a\u5236\u3001\u667a\u80fd\u5408\u7ea6\u3001\u8282\u70b9\u95f4\u7684\u901a\u4fe1\u7b49\u3002<\/p>\n

6. \u77e5\u8bc6\u70b9\u603b\u7ed3<\/h3>\n\n\n\n\n\n\n\n\n\n\n\n
\u77e5\u8bc6\u70b9<\/th>\n\u5185\u5bb9\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
\u533a\u5757\u94fe\u6982\u5ff5<\/td>\n\u53bb\u4e2d\u5fc3\u5316\u7684\u5206\u5e03\u5f0f\u8d26\u672c\uff0c\u5305\u542b\u533a\u5757\u4e0e\u94fe\u7684\u7ed3\u6784<\/td>\n<\/tr>\n
\u57fa\u672c\u6784\u6210<\/td>\n\u4e8b\u52a1\u6570\u636e\u3001\u65f6\u95f4\u6233\u3001\u54c8\u5e0c\u503c\u7b49<\/td>\n<\/tr>\n
\u5f00\u53d1\u8bed\u8a00<\/td>\nPython\uff0c\u5b89\u88c5\u9700\u8981\u7684\u5e93\u5982Flask<\/code><\/td>\n<\/tr>\n
\u533a\u5757\u7c7b\u5b9a\u4e49<\/td>\n\u7528\u4e8e\u5b58\u50a8\u533a\u5757\u7684\u7ed3\u6784\u4e0e\u8ba1\u7b97\u54c8\u5e0c\u7684\u65b9\u6cd5<\/td>\n<\/tr>\n
\u533a\u5757\u94fe\u7c7b\u5b9a\u4e49<\/td>\n\u7ba1\u7406\u533a\u5757\u7684\u521b\u5efa\u4e0e\u94fe\u7684\u7ef4\u62a4<\/td>\n<\/tr>\n
Web\u670d\u52a1<\/td>\n\u4f7f\u7528Flask\u642d\u5efaRESTful API<\/td>\n<\/tr>\n
\u6d4b\u8bd5\u63a5\u53e3<\/td>\n\/mine_block<\/code>\uff1a\u6316\u6398\u65b0\u533a\u5757\uff0c\/chain<\/code>\uff1a\u67e5\u770b\u533a\u5757\u94fe<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u521d\u5b66\u8005\u53ef\u4ee5\u8f7b\u677e\u5165\u95e8\u5e76\u5f00\u59cb\u63a2\u7d22\u66f4\u590d\u6742\u7684\u533a\u5757\u94fe\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u4e2ademo\u80fd\u591f\u6fc0\u53d1\u4f60\u5bf9\u533a\u5757\u94fe\u6280\u672f\u66f4\u6df1\u5c42\u6b21\u7684\u5174\u8da3\u548c\u7814\u7a76\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

\u533a\u5757\u94fe\u6280\u672f\u4f5c\u4e3a\u5f53\u4eca\u79d1\u6280\u53d1\u5c55\u7684\u70ed\u70b9\u4e4b\u4e00\uff0c\u5176\u53bb\u4e2d\u5fc3\u5316\u3001\u900f\u660e\u3001\u5b89\u5168\u7b49\u7279\u70b9\u5438\u5f15\u7740\u5927\u91cf\u5f00\u53d1\u8005\u548c\u4f01\u4e1a\u7684\u5173\u6ce8\u3002\u672c\u6559\u7a0b\u65e8\u5728\u5e2e\u52a9…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/posts\/3955"}],"collection":[{"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/comments?post=3955"}],"version-history":[{"count":0,"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/posts\/3955\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/media?parent=3955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/categories?post=3955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.a88o.com\/wp-json\/wp\/v2\/tags?post=3955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}