{"id":2817,"date":"2025-03-16T12:12:36","date_gmt":"2025-03-16T04:12:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2817"},"modified":"2025-03-16T12:12:36","modified_gmt":"2025-03-16T04:12:36","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b3%ef%bc%9ahttp-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b3%ef%bc%9ahttp-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b3\uff1aHTTP \u7f51\u7edc\u7f16\u7a0b"},"content":{"rendered":"\n<p>Python \u63d0\u4f9b\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c HTTP \u7f51\u7edc\u7f16\u7a0b\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>requests<\/code><\/strong>\uff08\u7528\u4e8e\u5ba2\u6237\u7aef HTTP \u8bbf\u95ee\uff09<\/li>\n\n\n\n<li><strong><code>http.server<\/code><\/strong>\uff08\u7528\u4e8e\u521b\u5efa\u7b80\u5355 HTTP \u670d\u52a1\u5668\uff09<\/li>\n\n\n\n<li><strong><code>aiohttp<\/code><\/strong>\uff08\u5f02\u6b65 HTTP \u5ba2\u6237\u7aef &amp; \u670d\u52a1\u5668\uff09<\/li>\n\n\n\n<li><strong>WebSocket<\/strong>\uff08\u57fa\u4e8e <code>websockets<\/code> \u5e93\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3.1 <code>requests<\/code> \u5ba2\u6237\u7aef<\/strong><\/h2>\n\n\n\n<p><code>requests<\/code> \u662f Python \u6700\u6d41\u884c\u7684 HTTP \u5ba2\u6237\u7aef\u5e93\uff0c\u652f\u6301 GET\u3001POST\u3001PUT\u3001DELETE \u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1.1 \u53d1\u9001 HTTP GET \u8bf7\u6c42<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nresponse = requests.get(\"https:\/\/jsonplaceholder.typicode.com\/todos\/1\")\nprint(response.status_code)  # HTTP \u72b6\u6001\u7801\nprint(response.json())       # JSON \u54cd\u5e94\u6570\u636e<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1.2 \u53d1\u9001 HTTP POST \u8bf7\u6c42<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\ndata = {\"title\": \"New Task\", \"completed\": False}\nresponse = requests.post(\"https:\/\/jsonplaceholder.typicode.com\/todos\", json=data)\n\nprint(response.status_code)\nprint(response.json())<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3.2 \u4f7f\u7528 <code>http.server<\/code> \u521b\u5efa HTTP \u670d\u52a1\u5668<\/strong><\/h2>\n\n\n\n<p>Python \u81ea\u5e26 <code>http.server<\/code> \u6a21\u5757\uff0c\u53ef\u5feb\u901f\u521b\u5efa HTTP \u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2.1 \u521b\u5efa HTTP \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from http.server import SimpleHTTPRequestHandler, HTTPServer\n\nclass MyHandler(SimpleHTTPRequestHandler):\n    def do_GET(self):\n        self.send_response(200)\n        self.send_header(\"Content-type\", \"text\/plain\")\n        self.end_headers()\n        self.wfile.write(b\"Hello, HTTP Server!\")\n\nserver = HTTPServer((\"0.0.0.0\", 8080), MyHandler)\nprint(\"HTTP \u670d\u52a1\u5668\u8fd0\u884c\u5728\u7aef\u53e3 8080...\")\nserver.serve_forever()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3.3 <code>aiohttp<\/code> \u5f02\u6b65 HTTP<\/strong><\/h2>\n\n\n\n<p><code>aiohttp<\/code> \u662f Python \u7684\u5f02\u6b65 HTTP \u6846\u67b6\uff0c\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u5e94\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3.1 <code>aiohttp<\/code> \u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import aiohttp\nimport asyncio\n\nasync def fetch():\n    async with aiohttp.ClientSession() as session:\n        async with session.get(\"https:\/\/jsonplaceholder.typicode.com\/todos\/1\") as resp:\n            print(await resp.json())\n\nasyncio.run(fetch())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3.2 <code>aiohttp<\/code> \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from aiohttp import web\n\nasync def handle(request):\n    return web.Response(text=\"Hello, aiohttp Server!\")\n\napp = web.Application()\napp.router.add_get(\"\/\", handle)\n\nweb.run_app(app, port=8080)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3.4 WebSocket<\/strong><\/h2>\n\n\n\n<p>WebSocket \u662f\u4e00\u79cd\u53cc\u5411\u901a\u4fe1\u534f\u8bae\uff0c\u9002\u7528\u4e8e\u5b9e\u65f6\u5e94\u7528\uff08\u5982\u804a\u5929\u5ba4\u3001\u6e38\u620f\u3001\u80a1\u7968\u63a8\u9001\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4.1 WebSocket \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528 <code>websockets<\/code> \u521b\u5efa WebSocket \u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nimport websockets\n\nasync def websocket_handler(websocket, path):\n    async for message in websocket:\n        print(f\"\u6536\u5230\u6d88\u606f: {message}\")\n        await websocket.send(f\"\u670d\u52a1\u5668\u6536\u5230: {message}\")\n\nserver = websockets.serve(websocket_handler, \"0.0.0.0\", 8765)\n\nasyncio.get_event_loop().run_until_complete(server)\nasyncio.get_event_loop().run_forever()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4.2 WebSocket \u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nimport websockets\n\nasync def websocket_client():\n    async with websockets.connect(\"ws:\/\/127.0.0.1:8765\") as websocket:\n        await websocket.send(\"Hello WebSocket!\")\n        response = await websocket.recv()\n        print(f\"\u670d\u52a1\u5668\u54cd\u5e94: {response}\")\n\nasyncio.run(websocket_client())<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u6df1\u5165\u4ecb\u7ecd <code>paramiko<\/code> \u8fdb\u884c SSH \u8fdc\u7a0b\u64cd\u4f5c\u3001<code>ftplib<\/code> \u8fdb\u884c FTP \u6587\u4ef6\u4f20\u8f93\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u63d0\u4f9b\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c HTTP \u7f51\u7edc\u7f16\u7a0b\uff0c\u5305\u62ec\uff1a 3.1 requests \u5ba2\u6237\u7aef requests [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2818,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2817","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2817"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2817\/revisions"}],"predecessor-version":[{"id":2819,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2817\/revisions\/2819"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2818"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}