{"id":932942,"date":"2024-12-26T18:07:32","date_gmt":"2024-12-26T10:07:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932942.html"},"modified":"2024-12-26T18:07:35","modified_gmt":"2024-12-26T10:07:35","slug":"python%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%aetimeout","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932942.html","title":{"rendered":"python\u5982\u4f55\u914d\u7f6etimeout"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070552\/4379849b-af38-4639-9f13-b7f8c185443b.webp\" alt=\"python\u5982\u4f55\u914d\u7f6etimeout\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u914d\u7f6etimeout\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528requests\u5e93\u3001socket\u6a21\u5757\u3001\u4ee5\u53ca\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65IO\u7b49\u65b9\u6cd5\u3002<\/strong>\u8bbe\u7f6etimeout\u7684\u4e3b\u8981\u76ee\u7684\u662f\u9632\u6b62\u7a0b\u5e8f\u5728\u7b49\u5f85\u7f51\u7edc\u54cd\u5e94\u65f6\u9677\u5165\u65e0\u9650\u671f\u7684\u963b\u585e\u72b6\u6001\u3002<strong>\u4f7f\u7528requests\u5e93\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u3001\u529f\u80fd\u5f3a\u5927\u3002<\/strong>\u4f7f\u7528requests\u5e93\u65f6\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u65b9\u6cd5\u4e2d\u6307\u5b9atimeout\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u503c\u6216\u5143\u7ec4\uff0c\u5206\u522b\u8868\u793a\u8fde\u63a5\u8d85\u65f6\u548c\u8bfb\u53d6\u8d85\u65f6\u3002\u6b64\u5916\uff0c\u4f7f\u7528socket\u6a21\u5757\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u7f51\u7edc\u7f16\u7a0b\u65f6\uff0c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u6765\u63a7\u5236\u7f51\u7edc\u64cd\u4f5c\u7684\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u914d\u7f6etimeout\u7684\u65b9\u6cd5\u524d\uff0c\u6211\u4eec\u9996\u5148\u6765\u7406\u89e3timeout\u7684\u57fa\u672c\u6982\u5ff5\u3002timeout\u662f\u6307\u5728\u8fdb\u884c\u67d0\u79cd\u64cd\u4f5c\u65f6\uff0c\u5141\u8bb8\u7b49\u5f85\u7684\u6700\u957f\u65f6\u95f4\u3002\u4e00\u65e6\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\uff0c\u64cd\u4f5c\u5c06\u88ab\u4e2d\u65ad\u6216\u629b\u51fa\u5f02\u5e38\u3002\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0ctimeout\u901a\u5e38\u7528\u4e8e\u9650\u5236\u7f51\u7edc\u8bf7\u6c42\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u7a33\u5b9a\u6027\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u8bf7\u6c42\u4e00\u4e2a\u7f51\u9875\u65f6\uff0c\u5982\u679c\u670d\u52a1\u5668\u6ca1\u6709\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u5185\u4f5c\u51fa\u54cd\u5e94\uff0c\u7a0b\u5e8f\u5c06\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u800c\u4e0d\u662f\u65e0\u9650\u671f\u5730\u7b49\u5f85\u4e0b\u53bb\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e0d\u540c\u573a\u666f\u4e0b\u914d\u7f6etimeout\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528requests\u5e93\u8bbe\u7f6etimeout<\/p>\n<\/p>\n<p><p>requests\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684HTTP\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u8fdb\u884cHTTP\u8bf7\u6c42\u3002requests\u5e93\u7684\u8bf7\u6c42\u65b9\u6cd5\uff0c\u5982get\u3001post\u7b49\uff0c\u652f\u6301timeout\u53c2\u6570\uff0c\u7528\u4e8e\u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528requests\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012timeout\u53c2\u6570\u6765\u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u3002timeout\u53c2\u6570\u53ef\u4ee5\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u79d2\u6570\uff0c\u6216\u8005\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u5206\u522b\u8868\u793a\u8fde\u63a5\u8d85\u65f6\u548c\u8bfb\u53d6\u8d85\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/example.com&#39;, timeout=5)<\/p>\n<p>    print(response.status_code)<\/p>\n<p>except requests.exceptions.Timeout:<\/p>\n<p>    print(&#39;Request timed out&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0ctimeout\u8bbe\u7f6e\u4e3a5\u79d2\uff0c\u610f\u5473\u7740\u8fde\u63a5\u548c\u8bfb\u53d6\u64cd\u4f5c\u603b\u5171\u4e0d\u80fd\u8d85\u8fc75\u79d2\u3002\u8d85\u65f6\u540e\uff0c\u5c06\u629b\u51farequests.exceptions.Timeout\u5f02\u5e38\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8fde\u63a5\u8d85\u65f6\u4e0e\u8bfb\u53d6\u8d85\u65f6<\/strong><\/li>\n<\/ol>\n<p><p>timeout\u53c2\u6570\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u7528\u4e8e\u5206\u522b\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u548c\u8bfb\u53d6\u8d85\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/example.com&#39;, timeout=(3.05, 27))<\/p>\n<p>    print(response.status_code)<\/p>\n<p>except requests.exceptions.Timeout:<\/p>\n<p>    print(&#39;Request timed out&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8fde\u63a5\u8d85\u65f6\u8bbe\u7f6e\u4e3a3.05\u79d2\uff0c\u8bfb\u53d6\u8d85\u65f6\u8bbe\u7f6e\u4e3a27\u79d2\u3002\u8fde\u63a5\u8d85\u65f6\u662f\u6307\u4ece\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5\u7684\u65f6\u95f4\uff0c\u800c\u8bfb\u53d6\u8d85\u65f6\u662f\u6307\u670d\u52a1\u5668\u5f00\u59cb\u53d1\u9001\u54cd\u5e94\u5230\u5ba2\u6237\u7aef\u63a5\u6536\u5230\u5b8c\u6574\u54cd\u5e94\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528socket\u6a21\u5757\u8bbe\u7f6etimeout<\/p>\n<\/p>\n<p><p>socket\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u5e95\u5c42\u7684\u7f51\u7edc\u63a5\u53e3\uff0c\u7528\u4e8e\u521b\u5efa\u3001\u8fde\u63a5\u548c\u64cd\u4f5c\u7f51\u7edc\u5957\u63a5\u5b57\u3002\u5728\u4f7f\u7528socket\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7settimeout\u65b9\u6cd5\u6765\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528socket.settimeout\u65b9\u6cd5\u8bbe\u7f6e\u5957\u63a5\u5b57\u64cd\u4f5c\u7684\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2<\/strong><\/h2>\n<p>sock.settimeout(5.0)<\/p>\n<p>try:<\/p>\n<p>    # \u5c1d\u8bd5\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/p>\n<p>    sock.connect((&#39;example.com&#39;, 80))<\/p>\n<p>    print(&#39;Connected successfully&#39;)<\/p>\n<p>except socket.timeout:<\/p>\n<p>    print(&#39;Connection timed out&#39;)<\/p>\n<p>finally:<\/p>\n<p>    sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57\uff0c\u5e76\u5c06\u5176\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u4e3a5\u79d2\u3002\u8d85\u65f6\u540e\uff0c\u5c06\u629b\u51fasocket.timeout\u5f02\u5e38\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5206\u522b\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u548c\u8bfb\u53d6\u8d85\u65f6\u3002\u53ef\u4ee5\u901a\u8fc7\u5206\u6b65\u9aa4\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTCP\/IP\u5957\u63a5\u5b57<\/strong><\/h2>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4<\/strong><\/h2>\n<p>sock.settimeout(3.0)<\/p>\n<p>try:<\/p>\n<p>    # \u5c1d\u8bd5\u8fde\u63a5\u5230\u670d\u52a1\u5668<\/p>\n<p>    sock.connect((&#39;example.com&#39;, 80))<\/p>\n<p>    print(&#39;Connected successfully&#39;)<\/p>\n<p>    # \u8bbe\u7f6e\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4<\/p>\n<p>    sock.settimeout(5.0)<\/p>\n<p>    data = sock.recv(1024)<\/p>\n<p>    print(&#39;Data received:&#39;, data)<\/p>\n<p>except socket.timeout:<\/p>\n<p>    print(&#39;Operation timed out&#39;)<\/p>\n<p>finally:<\/p>\n<p>    sock.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\uff0c\u7136\u540e\u5728\u8fde\u63a5\u6210\u529f\u540e\u8bbe\u7f6e\u8bfb\u53d6\u8d85\u65f6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5206\u522b\u63a7\u5236\u8fde\u63a5\u548c\u8bfb\u53d6\u8d85\u65f6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65IO\u8bbe\u7f6etimeout<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65IO\u6765\u5b9e\u73b0\u8d85\u65f6\u63a7\u5236\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7ebf\u7a0b\u6c60\u6765\u7ba1\u7406\u591a\u4e2a\u8bf7\u6c42\uff0c\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>import requests<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u8bf7\u6c42<\/strong><\/h2>\n<p>def fetch_url(url, timeout):<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, timeout=timeout)<\/p>\n<p>        return response.status_code<\/p>\n<p>    except requests.exceptions.Timeout:<\/p>\n<p>        return &#39;Timed out&#39;<\/p>\n<p>urls = [&#39;https:\/\/example1.com&#39;, &#39;https:\/\/example2.com&#39;]<\/p>\n<p>timeout = 5<\/p>\n<h2><strong>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6765\u5e76\u53d1\u5904\u7406\u8bf7\u6c42<\/strong><\/h2>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    futures = [executor.submit(fetch_url, url, timeout) for url in urls]<\/p>\n<p>    for future in concurrent.futures.as_completed(futures):<\/p>\n<p>        print(future.result())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528concurrent.futures.ThreadPoolExecutor\u6765\u5e76\u53d1\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\u3002\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u8bbe\u7f6e\u4e86\u8d85\u65f6\u65f6\u95f4\uff0c\u8d85\u65f6\u540e\u5c06\u8fd4\u56de&#39;Timed out&#39;\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u5f02\u6b65IO<\/strong><\/li>\n<\/ol>\n<p><p>Python\u4e2d\u7684asyncio\u5e93\u652f\u6301\u5f02\u6b65IO\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\uff0c\u540c\u65f6\u63a7\u5236\u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import <a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ohttp<\/p>\n<p>async def fetch_url(session, url, timeout):<\/p>\n<p>    try:<\/p>\n<p>        async with session.get(url, timeout=timeout) as response:<\/p>\n<p>            return response.status<\/p>\n<p>    except asyncio.TimeoutError:<\/p>\n<p>        return &#39;Timed out&#39;<\/p>\n<p>async def main():<\/p>\n<p>    urls = [&#39;https:\/\/example1.com&#39;, &#39;https:\/\/example2.com&#39;]<\/p>\n<p>    timeout = aiohttp.ClientTimeout(total=5)<\/p>\n<p>    async with aiohttp.ClientSession(timeout=timeout) as session:<\/p>\n<p>        tasks = [fetch_url(session, url, timeout) for url in urls]<\/p>\n<p>        results = await asyncio.gather(*tasks, return_exceptions=True)<\/p>\n<p>        for result in results:<\/p>\n<p>            print(result)<\/p>\n<h2><strong>\u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528aiohttp\u5e93\u6765\u5b9e\u73b0\u5f02\u6b65HTTP\u8bf7\u6c42\u3002\u901a\u8fc7aiohttp.ClientTimeout\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8bf7\u6c42\u7684\u603b\u8d85\u65f6\u65f6\u95f4\u3002\u8d85\u65f6\u540e\uff0c\u5c06\u8fd4\u56de&#39;Timed out&#39;\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u914d\u7f6etimeout\u662f\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7a0b\u5e8f\u5728\u7b49\u5f85\u7f51\u7edc\u54cd\u5e94\u65f6\u9677\u5165\u963b\u585e\u3002requests\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\u6765\u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570HTTP\u8bf7\u6c42\u573a\u666f\u3002\u5bf9\u4e8e\u9700\u8981\u66f4\u7ec6\u7c92\u5ea6\u63a7\u5236\u6216\u5e95\u5c42\u7f51\u7edc\u7f16\u7a0b\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528socket\u6a21\u5757\u6765\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65IO\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u8d85\u65f6\u63a7\u5236\u673a\u5236\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u7f6etimeout\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u8bbe\u7f6eHTTP\u8bf7\u6c42\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u901a\u8fc7\u5728\u8bf7\u6c42\u65b9\u6cd5\uff08\u5982<code>get<\/code>\u3001<code>post<\/code>\u7b49\uff09\u4e2d\u6dfb\u52a0<code>timeout<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u6307\u5b9a\u8bf7\u6c42\u7684\u6700\u957f\u7b49\u5f85\u65f6\u95f4\u3002\u4f8b\u5982\uff0c<code>requests.get(&#39;http:\/\/example.com&#39;, timeout=5)<\/code>\u8868\u793a\u5982\u679c\u57285\u79d2\u5185\u6ca1\u6709\u54cd\u5e94\uff0c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u8d85\u65f6\u5f02\u5e38\u3002<\/p>\n<p><strong>\u8d85\u65f6\u8bbe\u7f6e\u5bf9\u4e0d\u540c\u7f51\u7edc\u8bf7\u6c42\u7684\u5f71\u54cd\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8d85\u65f6\u8bbe\u7f6e\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u5982\u679c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u8fc7\u77ed\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b63\u5e38\u8bf7\u6c42\u88ab\u610f\u5916\u4e2d\u65ad\uff1b\u5982\u679c\u8bbe\u7f6e\u8fc7\u957f\uff0c\u53ef\u80fd\u4f1a\u8ba9\u7528\u6237\u957f\u65f6\u95f4\u7b49\u5f85\u65e0\u54cd\u5e94\u7684\u8bf7\u6c42\u3002\u56e0\u6b64\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4\u662f\u786e\u4fdd\u7a0b\u5e8f\u9ad8\u6548\u8fd0\u884c\u7684\u5173\u952e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d85\u65f6\u5f02\u5e38\uff1f<\/strong><br \/>\u5904\u7406\u8d85\u65f6\u5f02\u5e38\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u3002\u5f53\u8bf7\u6c42\u8d85\u65f6\u53d1\u751f\u65f6\uff0c<code>requests<\/code>\u5e93\u4f1a\u5f15\u53d1<code>requests.exceptions.Timeout<\/code>\u5f02\u5e38\u3002\u901a\u8fc7\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\uff0c\u53ef\u4ee5\u5bf9\u8d85\u65f6\u60c5\u51b5\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u6bd4\u5982\u91cd\u8bd5\u8bf7\u6c42\u6216\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\ntry:\n    response = requests.get(&#39;http:\/\/example.com&#39;, timeout=5)\nexcept requests.exceptions.Timeout:\n    print(&quot;\u8bf7\u6c42\u8d85\u65f6\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u7a0b\u5e8f\u4f18\u96c5\u5730\u5e94\u5bf9\u7f51\u7edc\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u914d\u7f6etimeout\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528requests\u5e93\u3001socket\u6a21\u5757\u3001\u4ee5\u53ca\u591a\u7ebf [&hellip;]","protected":false},"author":3,"featured_media":932951,"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\/932942"}],"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=932942"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932942\/revisions"}],"predecessor-version":[{"id":932956,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932942\/revisions\/932956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932951"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}