

{"id":181,"date":"2021-11-20T18:36:27","date_gmt":"2021-11-20T10:36:27","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/python\/?p=181"},"modified":"2021-11-20T18:47:46","modified_gmt":"2021-11-20T10:47:46","slug":"python%e5%ae%9e%e7%8e%b0http%e5%8d%8f%e8%ae%ae","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/python\/post-181.html","title":{"rendered":"python\u5b9e\u73b0http\u534f\u8bae"},"content":{"rendered":"\n<p>HTTP\u662f\u57fa\u4e8eTCP\u7684\u9ad8\u7ea7\u534f\u8bae\uff0c\u540c\u65f6HTTP\u662f\u4e00\u4e2a\u8f6f\u534f\u8bae\uff0c\u5b83\u53ea\u662f\u5bf9TCP\u8fdb\u884c\u4e86\u4f20\u8f93\u683c\u5f0f\u7684\u89c4\u5b9a\u3002<\/p>\n\n\n\n<p>HTTP\u662f\u65e0\u72b6\u6001\u7684\u534f\u8bae\uff0c\u6309\u8bf7\u6c42\u4e0e\u54cd\u5e94\u5206\u4e3ahttp\u8bf7\u6c42\u4e0ehttp\u54cd\u5e94\uff0c\u5b83\u4eec\u7684\u683c\u5f0f\u57fa\u672c\u76f8\u540c<\/p>\n\n\n\n<h2>HTTP\u534f\u8bae\u7ec4\u6210<\/h2>\n\n\n\n<p>HTTP\u534f\u8bae\u75314\u90e8\u5206\u7ec4\u6210\uff0c\u4ee5<strong>HTTP\u8bf7\u6c42<\/strong>\u6765\u770b\uff0c\u5206\u522b\u662f<\/p>\n\n\n\n<ul><li>\u8bf7\u6c42\u884c<\/li><li>\u8bf7\u6c42\u5934<\/li><li>\u8bf7\u6c42\u7a7a\u884c<\/li><li>\u8bf7\u6c42\u4f53<\/li><\/ul>\n\n\n\n<h3>\u8bf7\u6c42\u884c<\/h3>\n\n\n\n<p>\u5176\u4e2d\uff0c\u8bf7\u6c42\u884c\u7684\u683c\u5f0f\u56fa\u5b9a\u4e3a\u4e00\u884c\uff0c\u4f8b\u5982\u4ee5\u4e0b\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>get \/index.html HTTP\/1.1 <\/code><\/pre>\n\n\n\n<p>\u6362\u6210\u6587\u5b57\u8bf4\u660e\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bf7\u6c42\u65b9\u5f0f URI \u534f\u8bae\/\u7248\u672c<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6c42\u65b9\u5f0f\u6709\u591a\u79cd\uff0cget\/post\/put\u7b49\uff0c\u534f\u8bae\u53ef\u80fd\u662fhttp\/https\uff0c\u7248\u672c\u6709\u591a\u79cd\uff0c\u5982\u679c\u662f1.0\u5219\u56fa\u5b9a\u4e3a\u77ed\u8fde\u63a5\uff0c\u800c1.1\u4ee5\u540e\u5747\u4e3a\u957f\u8fde\u63a5\u3002<\/p>\n\n\n\n<p>\u8bf7\u6c42\u884c\u7684\u51e0\u4e2a\u53c2\u6570\u4e4b\u95f4\u662f\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u7a7a\u683c\u5206\u9694\u3002<\/p>\n\n\n\n<h3>\u8bf7\u6c42\u5934<\/h3>\n\n\n\n<p>\u8bf7\u6c42\u5934\u7684\u4f5c\u7528\u662f\uff0c\u7ed9\u670d\u52a1\u5668\u4e00\u4e9b\u63cf\u8ff0\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u80fd\u5305\u62ec\uff1a\u6d4f\u89c8\u5668\u7248\u672c\uff0c\u53ef\u63a5\u53d7\u7684\u6570\u636e\u7c7b\u578b\u7b49<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u4fe1\u606f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3aHTTP\u662f\u65e0\u72b6\u6001\u7684\uff0c\u5982\u679c\u6ca1\u6709\u8bf7\u6c42\u5934\uff0c\u5728\u67d0\u4e9b\u65f6\u5019\u4f60\u65e0\u6cd5\u5206\u8fa8\u5230\u5e95\u662f\u8c01\u8bf7\u6c42\u4e86\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u53ea\u60f3\u7ed9\u643a\u5e26\u4e86User-Agent\u4e3axxx\u7684\u8bf7\u6c42\u54cd\u5e94\u8d44\u6e90\uff0c\u90a3\u4e48\u4f60\u5c31\u5fc5\u987b\u6dfb\u52a0\u8fd9\u4e2a\u8bf7\u6c42\u5934\u4fe1\u606f\uff0c\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5f88\u597d\u7684\u8fc7\u6ee4\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5982\u679c\u5b58\u5728\u8bf7\u6c42\u4f53\uff0c\u5219\u5fc5\u987b\u5728\u8bf7\u6c42\u5934\u6307\u5b9a\u8bf7\u6c42\u4f53\u7684\u5927\u5c0f\uff0c\u4f7f\u7528Content-Length\u8868\u793a\u5b57\u8282\u5927\u5c0f\uff0c\u8fd9\u662f\u4e3a\u4e86\u6b63\u786e\u7684\u63a5\u6536\u6570\u636e\u4ee5\u53ca\u53ca\u65f6\u7684\u53d6\u51fa\u7cfb\u7edf\u7684\u4e34\u65f6\u5b58\u50a8\u6570\u636e\u9632\u6b62\u670d\u52a1\u5668\u5d29\u6e83\u3002<\/p>\n\n\n\n<p>\u5e38\u89c1\u7684\u8bf7\u6c42\u5934\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p><strong>Accept-Charset<\/strong>\uff1a\u6d4f\u89c8\u5668\u53ef\u63a5\u53d7\u7684\u5b57\u7b26\u96c6\uff1b<\/p>\n\n\n\n<p><strong>Content-Length<\/strong>\uff1a\u8868\u793a\u8bf7\u6c42\u6d88\u606f\u6b63\u6587\u7684\u957f\u5ea6\uff1b<\/p>\n\n\n\n<p><strong>User-Agent<\/strong>\uff1a\u6d4f\u89c8\u5668\u7c7b\u578b\uff0c\u5982\u679cServlet\u8fd4\u56de\u7684\u5185\u5bb9\u4e0e\u6d4f\u89c8\u5668\u7c7b\u578b\u6709\u5173\u5219\u8be5\u503c\u975e\u5e38\u6709\u7528\uff1b<\/p>\n\n\n\n<h3>\u8bf7\u6c42\u7a7a\u884c<\/h3>\n\n\n\n<p>\u8bf7\u6c42\u7a7a\u884c\u662f\u4e3a\u4e86\u6b63\u786e\u7684\u5206\u5272\u8bf7\u6c42\u5934\u4e0e\u8bf7\u6c42\u4f53\u3002<\/p>\n\n\n\n<p>\u8bf7\u6c42\u5934\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\uff0c\u6211\u4eec\u65e0\u6cd5\u786e\u5b9a\u5b83\u6709\u51e0\u4e2a\uff0c\u8fd9\u70b9\u4e0e\u8bf7\u6c42\u4f53\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u5fc5\u987b\u6709\u8bf7\u6c42\u7a7a\u884c\u3002<\/p>\n\n\n\n<p>\u4f46\u6b64\u90e8\u5206\u5728\u63cf\u8ff0\u65f6\u5f80\u5f80\u4f1a\u88ab\u7701\u7565\uff0c\u5374\u53c8\u662f\u5fc5\u987b\u5b58\u5728\u7684\u3002<\/p>\n\n\n\n<h3>\u8bf7\u6c42\u4f53<\/h3>\n\n\n\n<p>\u8bf7\u6c42\u4f53\u4e0d\u4e00\u5b9a\u5b58\u5728\uff0c\u53ea\u6709\u7528\u6237\u5411\u670d\u52a1\u5668\u4e0a\u4f20\u6570\u636e\u65f6\uff0c\u624d\u9700\u8981\u6b64\u9879\u3002<\/p>\n\n\n\n<p>\u8bf7\u6c42\u4f53\u4e2d\u5b58\u653e\u4e0a\u4f20\u7684\u6570\u636e\uff0c\u5176\u957f\u5ea6\u5fc5\u987b\u5728\u8bf7\u6c42\u5934\u4e2d\u6307\u660e\u5b57\u8282\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u8bf7\u6c42\u4f53\u4e2d\u7684\u6570\u636e\u6709\u53ef\u80fd\u662f\u52a0\u5bc6\u7684\uff0c\u6216\u8005\u662f\u8f6c\u7f16\u7801\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u67e5\u770b\u3002<\/p>\n\n\n\n<h2>Py\u5b9e\u73b0\u9759\u6001\u670d\u52a1\u5668<\/h2>\n\n\n\n<p>\u9759\u6001\u670d\u52a1\u5668\u5728\u8fd9\u91cc\u6307\u7684\u662f\u76f4\u63a5\u8fd4\u56deHTML\u6587\u6863\u3002<\/p>\n\n\n\n<p>\u57fa\u672c\u7684\u5b9e\u73b0\u6d41\u7a0b\u662f\uff1a\u7528\u6237\u5728\u6d4f\u89c8\u5668\u8bbf\u95eeURL -&gt; Python\u63a5\u6536\u5e76\u89e3\u6790URI\uff0c\u4ee5I\/O\u6d41\u83b7\u53d6\u8be5\u8def\u5f84\u4e0b\u7684\u8d44\u6e90\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong>HTMP\u6587\u6863\u4e2d\u5b58\u5728\u5176\u4ed6\u8d44\u6e90\u65f6\uff0c\u6bd4\u5982\u56fe\u7247\uff0ccss\uff0cjs\u6587\u4ef6\uff0c\u4f1a\u81ea\u52a8\u518d\u6b21\u53d1\u51fa\u8bf7\u6c42\uff0c\u8fd9\u610f\u5473\u7740\u670d\u52a1\u5668\u6bcf\u6b21\u670d\u52a1\u5b8c\u6bd5\u540e\u4e0d\u80fd\u76f4\u63a5\u5173\u95ed\u8fde\u63a5\uff0cHTTP\u534f\u8bae\u4e2d\u4e00\u822c\u7531\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5\uff0c\u5173\u95ed\u65f6\u4e5f\u4f1a\u81ea\u52a8\u53d1\u51fa\u4e00\u4e2a\u8bf7\u6c42\u800c\u6570\u636e\u4e3a\u7a7a\uff0c\u670d\u52a1\u5668\u63a5\u6536\u540e\u5224\u65ad\u4e3a\u7a7a\u8bf4\u660e\u5ba2\u6237\u7aef\u65ad\u5f00\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u518d\u65ad\u5f00\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\nimport re\n\n\ndef service_client(new_socket):\n    \"\"\"\u4e3a\u8fd9\u4e2a\u5ba2\u6237\u7aef\u8fd4\u56de\u6570\u636e\"\"\"\n\n    # 1. \u63a5\u6536\u6d4f\u89c8\u5668\u53d1\u9001\u8fc7\u6765\u7684\u8bf7\u6c42 \uff0c\u5373http\u8bf7\u6c42  \n    # GET \/ HTTP\/1.1\n    # .....\n    request = new_socket.recv(1024).decode(\"utf-8\")\n    # print(\"&gt;&gt;&gt;\"*50)\n    # print(request)\n\n    request_lines = request.splitlines()\n    print(\"\")\n    print(\"&gt;\"*20)\n    print(request_lines)\n\n    # GET \/index.html HTTP\/1.1\n    # get post put del\n    file_name = \"\"\n    ret = re.match(r\"&#91;^\/]+(\/&#91;^ ]*)\", request_lines&#91;0])\n    if ret:\n        file_name = ret.group(1)\n        # print(\"*\"*50, file_name)\n        if file_name == \"\/\":\n            file_name = \"\/index.html\"\n\n\n\n\n    # 2. \u8fd4\u56dehttp\u683c\u5f0f\u7684\u6570\u636e\uff0c\u7ed9\u6d4f\u89c8\u5668\n    \n    try:\n        f = open(\".\/html\" + file_name, \"rb\")\n    except:\n        response = \"HTTP\/1.1 404 NOT FOUND\\r\\n\"\n        response += \"\\r\\n\"\n        response += \"------file not found-----\"\n        new_socket.send(response.encode(\"utf-8\"))\n    else:\n        html_content = f.read()\n        f.close()\n        # 2.1 \u51c6\u5907\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668\u7684\u6570\u636e---header\n        response = \"HTTP\/1.1 200 OK\\r\\n\"\n        response += \"\\r\\n\"\n        # 2.2 \u51c6\u5907\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668\u7684\u6570\u636e---boy\n        # response += \"hahahhah\"\n\n        # \u5c06response header\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668\n        new_socket.send(response.encode(\"utf-8\"))\n        # \u5c06response body\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668\n        new_socket.send(html_content)\n\n\n    # \u5173\u95ed\u5957\u63a5\n    new_socket.close()\n    \n\ndef main():\n    \"\"\"\u7528\u6765\u5b8c\u6210\u6574\u4f53\u7684\u63a7\u5236\"\"\"\n    # 1. \u521b\u5efa\u5957\u63a5\u5b57\n    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n\n    # 2. \u7ed1\u5b9a\n    tcp_server_socket.bind((\"\", 7890))\n\n    # 3. \u53d8\u4e3a\u76d1\u542c\u5957\u63a5\u5b57\n    tcp_server_socket.listen(128)\n\n    while True:\n        # 4. \u7b49\u5f85\u65b0\u5ba2\u6237\u7aef\u7684\u94fe\u63a5\n        new_socket, client_addr = tcp_server_socket.accept()\n\n        # 5. \u4e3a\u8fd9\u4e2a\u5ba2\u6237\u7aef\u670d\u52a1\n        service_client(new_socket)\n\n    # \u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57\n    tcp_server_socket.close()\n\n\nif __name__ == \"__main__\":\n    main()\n\n<\/code><\/pre>\n\n\n\n<p>\u6b64\u4ee3\u7801\u7ed1\u5b9a7890\u7aef\u53e3\uff0c\u9ed8\u8ba4\u8fd4\u56de.\/html\u76ee\u5f55\u4e0b\u7684\u9759\u6001\u9875\u9762\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u5185\u5bb9\uff0c\u8bf7\u67e5\u770b\u4e0b\u4e00\u5c0f\u8282\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HTTP\u662f\u57fa\u4e8eTCP\u7684\u9ad8\u7ea7\u534f\u8bae\uff0c\u540c\u65f6HTTP\u662f\u4e00\u4e2a\u8f6f\u534f\u8bae\uff0c\u5b83\u53ea\u662f\u5bf9TCP\u8fdb\u884c\u4e86\u4f20\u8f93\u683c\u5f0f\u7684\u89c4\u5b9a\u3002 HTTP\u662f\u65e0\u72b6&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/python\/post-181.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">python\u5b9e\u73b0http\u534f\u8bae<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/181"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}