{"id":1108637,"date":"2025-01-08T17:02:14","date_gmt":"2025-01-08T09:02:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108637.html"},"modified":"2025-01-08T17:02:16","modified_gmt":"2025-01-08T09:02:16","slug":"cat%e7%9b%91%e6%8e%a7python%e7%89%88%e5%ae%a2%e6%88%b7%e7%ab%af%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108637.html","title":{"rendered":"cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072221\/c046f639-bfa3-4c83-af89-8b5726319a26.webp\" alt=\"cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u5982\u4f55\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u7684\u6b65\u9aa4\u5305\u62ec<strong>\u5b89\u88c5\u5ba2\u6237\u7aef\u3001\u914d\u7f6e\u5ba2\u6237\u7aef\u3001\u521d\u59cb\u5316\u5ba2\u6237\u7aef\u3001\u57cb\u70b9\u76d1\u63a7\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u76d1\u63a7\u3001\u67e5\u770b\u76d1\u63a7\u6570\u636e<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5ba2\u6237\u7aef<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5cat\u76d1\u63a7\u7684python\u7248\u5ba2\u6237\u7aef\u3002\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-cat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u5ba2\u6237\u7aef<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9700\u8981\u914d\u7f6e\u5ba2\u6237\u7aef\u3002\u901a\u5e38\u9700\u8981\u914d\u7f6eCAT\u670d\u52a1\u5668\u7684\u5730\u5740\u548c\u9879\u76ee\u540d\u79f0\u3002\u8fd9\u4e9b\u914d\u7f6e\u901a\u5e38\u653e\u5728\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982<code>cat.config<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[cat]<\/p>\n<p>server = http:\/\/cat.server.address<\/p>\n<p>app = your_project_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521d\u59cb\u5316\u5ba2\u6237\u7aef<\/h3>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\uff0c\u5bfc\u5165\u5e76\u521d\u59cb\u5316cat\u76d1\u63a7\u5ba2\u6237\u7aef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pycat import CatClient<\/p>\n<h2><strong>\u521d\u59cb\u5316Cat\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>cat_client = CatClient(config_file=&#39;path\/to\/your\/cat.config&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u57cb\u70b9\u76d1\u63a7\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u9700\u8981\u76d1\u63a7\u7684\u6570\u636e\u70b9\u6dfb\u52a0\u57cb\u70b9\uff0c\u901a\u5e38\u4f7f\u7528<code>CatClient<\/code>\u7684<code>log_event<\/code>\u548c<code>log_metric<\/code>\u7b49\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bb0\u5f55\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><p>\u8bb0\u5f55\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cat_client.log_event(&#39;Category&#39;, &#39;EventName&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u8bb0\u5f55\u4e8b\u4ef6\u7684\u72b6\u6001\u548c\u9644\u52a0\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cat_client.log_event(&#39;Category&#39;, &#39;EventName&#39;, status=&#39;0&#39;, data=&#39;additional data&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bb0\u5f55\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u662f\u76d1\u63a7\u7cfb\u7edf\u7684\u91cd\u8981\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>    pass<\/p>\n<p>except Exception as e:<\/p>\n<p>    cat_client.log_error(&#39;ErrorName&#39;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bb0\u5f55\u670d\u52a1\u8c03\u7528<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u670d\u52a1\u8c03\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528<code>log_service<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with cat_client.log_service(&#39;ServiceName&#39;, &#39;MethodName&#39;) as service:<\/p>\n<p>    # \u4f60\u7684\u670d\u52a1\u8c03\u7528\u4ee3\u7801<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5f02\u5e38\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6240\u6709\u5f02\u5e38\u90fd\u88ab\u76d1\u63a7\u5230\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5168\u5c40\u5f02\u5e38\u5904\u7406\u90e8\u5206\u6dfb\u52a0cat\u76d1\u63a7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import traceback<\/p>\n<p>def handle_exception(exc_type, exc_value, exc_traceback):<\/p>\n<p>    if issubclass(exc_type, KeyboardInterrupt):<\/p>\n<p>        sys.__excepthook__(exc_type, exc_value, exc_traceback)<\/p>\n<p>        return<\/p>\n<p>    cat_client.log_error(&#39;UnhandledException&#39;, exc_value)<\/p>\n<p>    print(&quot;Exception caught by global handler:&quot;, exc_value)<\/p>\n<p>sys.excepthook = handle_exception<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u67e5\u770b\u76d1\u63a7\u6570\u636e<\/h3>\n<\/p>\n<p><p>Cat\u76d1\u63a7\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e2aWeb\u754c\u9762\u6765\u67e5\u770b\u76d1\u63a7\u6570\u636e\u3002\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u7684CAT\u670d\u52a1\u5668\u5730\u5740\u6253\u5f00\u6d4f\u89c8\u5668\u5373\u53ef\u67e5\u770b\u3002\u4f8b\u5982\uff1a<code>http:\/\/cat.server.address<\/code>\u3002\u5728\u8fd9\u4e2a\u754c\u9762\u4e2d\uff0c\u53ef\u4ee5\u67e5\u770b\u5230\u4f60\u6240\u8bb0\u5f55\u7684\u5404\u79cd\u4e8b\u4ef6\u3001\u9519\u8bef\u3001\u670d\u52a1\u8c03\u7528\u7b49\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\uff0c\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u5206\u6790\u6765\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u4e0a\u8ff0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684Flask Web\u5e94\u7528\uff0c\u6211\u4eec\u5e0c\u671b\u76d1\u63a7\u7528\u6237\u8bf7\u6c42\u7684\u5904\u7406\u65f6\u95f4\u548c\u6355\u6349\u5230\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Flask\u548ccat\u76d1\u63a7\u5ba2\u6237\u7aef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p>pip install python-cat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2aFlask\u5e94\u7528\u5e76\u5728\u5176\u4e2d\u96c6\u6210cat\u76d1\u63a7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request<\/p>\n<p>from pycat import CatClient<\/p>\n<p>import time<\/p>\n<p>app = Flask(__name__)<\/p>\n<h2><strong>\u521d\u59cb\u5316Cat\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>cat_client = CatClient(config_file=&#39;path\/to\/your\/cat.config&#39;)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello_world():<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    try:<\/p>\n<p>        response = &quot;Hello, World!&quot;<\/p>\n<p>        cat_client.log_event(&#39;Request&#39;, &#39;HelloWorld&#39;)<\/p>\n<p>        return response<\/p>\n<p>    except Exception as e:<\/p>\n<p>        cat_client.log_error(&#39;RequestError&#39;, e)<\/p>\n<p>        return &quot;An error occurred&quot;, 500<\/p>\n<p>    finally:<\/p>\n<p>        duration = time.time() - start_time<\/p>\n<p>        cat_client.log_metric(&#39;RequestDuration&#39;, duration)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u505a\u4e86\u4ee5\u4e0b\u51e0\u4ef6\u4e8b\u60c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521d\u59cb\u5316Cat\u5ba2\u6237\u7aef\u3002<\/li>\n<li>\u5728\u5904\u7406\u8bf7\u6c42\u7684\u8fc7\u7a0b\u4e2d\u8bb0\u5f55\u4e00\u4e2a\u4e8b\u4ef6<code>Request<\/code>\uff0c\u8868\u793a\u4e00\u4e2a\u8bf7\u6c42\u88ab\u5904\u7406\u3002<\/li>\n<li>\u6355\u6349\u5f02\u5e38\u5e76\u8bb0\u5f55\u5230Cat\u76d1\u63a7\u7cfb\u7edf\u4e2d\u3002<\/li>\n<li>\u8bb0\u5f55\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u4f5c\u4e3a\u4e00\u4e2a\u6307\u6807\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Cat\u76d1\u63a7\u7cfb\u7edf\u7684Web\u754c\u9762\u4e2d\u67e5\u770b\u5230\u6240\u6709\u8bf7\u6c42\u7684\u5904\u7406\u60c5\u51b5\u3001\u5904\u7406\u65f6\u95f4\u4ee5\u53ca\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0c<strong>\u4f7f\u7528cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u5ba2\u6237\u7aef\u3001\u914d\u7f6e\u5ba2\u6237\u7aef\u3001\u521d\u59cb\u5316\u5ba2\u6237\u7aef\u3001\u57cb\u70b9\u76d1\u63a7\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u76d1\u63a7\u3001\u67e5\u770b\u76d1\u63a7\u6570\u636e<\/strong>\u3002\u8fd9\u4e9b\u6b65\u9aa4\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\uff0c\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u88c5cat\u76d1\u63a7\u7684Python\u5ba2\u6237\u7aef\uff1f<\/strong><br \/>\u8981\u5b89\u88c5cat\u76d1\u63a7\u7684Python\u5ba2\u6237\u7aef\uff0c\u7528\u6237\u9700\u8981\u786e\u4fdd\u4ed6\u4eec\u7684\u73af\u5883\u4e2d\u5df2\u5b89\u88c5Python\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install cat-monitor<\/code>. \u8fd9\u6837\u5c31\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6240\u9700\u7684\u4f9d\u8d56\u5305\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u524d\u68c0\u67e5Python\u548cpip\u7684\u7248\u672c\uff0c\u4ee5\u907f\u514d\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<p><strong>cat\u76d1\u63a7Python\u5ba2\u6237\u7aef\u652f\u6301\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><br \/>cat\u76d1\u63a7\u7684Python\u5ba2\u6237\u7aef\u5177\u5907\u591a\u79cd\u529f\u80fd\uff0c\u4f8b\u5982\u5b9e\u65f6\u6570\u636e\u76d1\u63a7\u3001\u5f02\u5e38\u62a5\u8b66\u3001\u6027\u80fd\u5206\u6790\u7b49\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5ba2\u6237\u7aef\u6536\u96c6\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u6570\u636e\uff0c\u76d1\u6d4b\u7cfb\u7edf\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728\u51fa\u73b0\u5f02\u5e38\u65f6\u81ea\u52a8\u53d1\u9001\u8b66\u62a5\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u53ca\u65f6\u53d1\u73b0\u5e76\u89e3\u51b3\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u914d\u7f6ecat\u76d1\u63a7Python\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5\uff1f<\/strong><br \/>\u5728\u914d\u7f6ecat\u76d1\u63a7Python\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7684\u8fde\u63a5\u65f6\uff0c\u7528\u6237\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u76f8\u5173\u7684\u8fde\u63a5\u53c2\u6570\u3002\u8fd9\u901a\u5e38\u5305\u62ec\u670d\u52a1\u5668\u7684IP\u5730\u5740\u3001\u7aef\u53e3\u53f7\u4ee5\u53ca\u5fc5\u8981\u7684\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u3002\u5177\u4f53\u7684\u914d\u7f6e\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff0c\u5176\u4e2d\u5305\u542b\u8be6\u7ec6\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u4e0a\u624b\u5e76\u5b8c\u6210\u8fde\u63a5\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528cat\u76d1\u63a7\u5ba2\u6237\u7aef\u7684API\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165cat\u76d1\u63a7\u7684Python\u5e93\u6765\u4f7f\u7528\u5176API\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8c03\u7528\u76f8\u5e94\u7684\u76d1\u63a7\u65b9\u6cd5\uff0c\u4f8b\u5982\u8bb0\u5f55\u4e1a\u52a1\u6307\u6807\u3001\u6355\u83b7\u5f02\u5e38\u7b49\u3002API\u7684\u4f7f\u7528\u65b9\u5f0f\u901a\u5e38\u4f1a\u5728\u5b98\u65b9\u6587\u6863\u4e2d\u63d0\u4f9b\u793a\u4f8b\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u7075\u6d3b\u5e94\u7528\uff0c\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u76d1\u63a7\u76ee\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u5982\u4f55\u4f7f\u7528 \u4f7f\u7528cat\u76d1\u63a7python\u7248\u5ba2\u6237\u7aef\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u5ba2\u6237\u7aef\u3001\u914d\u7f6e\u5ba2\u6237\u7aef\u3001\u521d [&hellip;]","protected":false},"author":3,"featured_media":1108642,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108637"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1108637"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108637\/revisions"}],"predecessor-version":[{"id":1108645,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108637\/revisions\/1108645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108642"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}