

{"id":152,"date":"2021-07-04T00:35:05","date_gmt":"2021-07-03T16:35:05","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/python\/?p=152"},"modified":"2021-07-04T14:56:33","modified_gmt":"2021-07-04T06:56:33","slug":"python%e7%9a%84tcp%e6%96%87%e4%bb%b6%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/python\/post-152.html","title":{"rendered":"python\u7684tcp\u6587\u4ef6\u4e0b\u8f7d"},"content":{"rendered":"\n<p>\u672c\u8282\u6f14\u793a\u7b80\u5355\u7684tcp\u6587\u4ef6\u4e0b\u8f7d\u6848\u4f8b<\/p>\n\n\n\n<h2>tcp\u670d\u52a1\u5668\u4ee3\u7801<\/h2>\n\n\n\n<p>\u8bfb\u53d6\u5ba2\u6237\u7aef\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u540d, \u6253\u5f00\u8be5\u6587\u4ef6, \u53d1\u9001\u5b57\u8282<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\ndef send_file_client(new_client_socket, client_addr):\n    # 1.\u83b7\u53d6\u5ba2\u6237\u7aef\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u540d\n    file_name = new_client_socket.recv(1024).decode(\"utf-8\")\n    print(\"\u5ba2\u6237\u7aef\"+str(client_addr)+\"\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u662f\"+file_name)\n\n    # 2.\u8bfb\u53d6\u6587\u4ef6\n    file_content = None\n    try:\n        f = open(file_name, \"rb\")\n        file_content = f.read()\n        f.close()\n    except Exception as ret:\n        print(\"\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6'\"+file_name+\"'\u4e0d\u5b58\u5728\")\n\n    # 3.\u53d1\u9001\u6587\u4ef6\u7ed9\u5ba2\u6237\u7aef\n    if file_content:\n        new_client_socket.send(file_content)\n\ndef main():\n    # 1.\u521b\u5efa\u5957\u63a5\u5b57\n    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    # 2.\u7ed1\u5b9a\u672c\u5730\u4fe1\u606f\n    tcp_server_socket.bind((\"\", 8888))\n    # 3.\u8bbe\u7f6e\u88ab\u88ab\u52a8\u6a21\u5f0f\n    tcp_server_socket.listen(128)\n    # \u5faa\u73af\u4e3a\u5ba2\u6237\u7aef\u670d\u52a1\n    while True:\n        # 4.\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\n        new_client_socket, client_addr = tcp_server_socket.accept()\n        # 5.\u8c03\u7528\u53d1\u9001\u6587\u4ef6\u51fd\u6570, \u5b8c\u6210\u5bf9\u5ba2\u6237\u7aef\u670d\u52a1\n        send_file_client(new_client_socket, client_addr)\n        # 6.\u5173\u95ed\u5957\u63a5\u5b57\n        new_client_socket.close()\n    tcp_server_socket.close()\n\nif __name__==\"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<h2>\u5ba2\u6237\u7aef\u4ee3\u7801<\/h2>\n\n\n\n<p>\u53d1\u9001\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u540d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\ndef main():\n    # 1.\u521b\u5efa\u5957\u63a5\u5b57\n    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    # 2.\u8f93\u5165\u670d\u52a1\u5668\u4fe1\u606f\n    dest_ip = input(\"\u8bf7\u8f93\u5165\u670d\u52a1\u5668ip:\")\n    dest_port = int(input(\"\u8bf7\u8f93\u5165\u670d\u52a1\u5668\u7aef\u53e3:\"))\n    # 3.\u8fde\u63a5\u670d\u52a1\u5668\n    tcp_socket.connect((dest_ip, dest_port))\n    # 4.\u8f93\u5165\u8981\u4e0b\u8f7d\u6587\u4ef6\u540d\n    download_name = input(\"\u8bf7\u8f93\u5165\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6:\")\n    # 5.\u53d1\u9001\u6587\u4ef6\u540d\u7ed9\u670d\u52a1\u5668\n    tcp_socket.send(download_name.encode(\"utf-8\"))\n    # 6.\u4ece\u670d\u52a1\u5668\u63a5\u6536\u6587\u4ef6, \u8981\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\n    rec_data = tcp_socket.recv(1024)\n    if rec_data:\n        # 7.\u4fdd\u5b58\u6570\u636e\u5230\u6587\u4ef6\u4e2d\n        with open(\"\u4e0b\u8f7d\"+download_name, \"wb\") as f:\n            f.write(rec_data)\n    # 8.\u5173\u95ed\u5957\u63a5\u5b57\n    tcp_socket.close()\n\nif __name__==\"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<p>\u5728python\u4e2d, with open(xxx) \u662f\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38\u7684\u81ea\u52a8close, \u800cas\u662f\u8d77\u4e00\u4e2a\u522b\u540d<\/p>\n\n\n\n<p>\u7f51\u7edc\u7f16\u7a0b\u9664\u4e86udp, tcp, \u5176\u5b9e\u8fd8\u6709\u5176\u4ed6\u65b9\u5f0f, \u4f46\u6ca1\u8fd9\u4e48\u5e38\u7528, \u5728\u7f51\u7edc\u5b89\u5168\u8bfe\u7a0b\u4e2d\u4f1a\u63a5\u89e6\u5230\u3002<\/p>\n\n\n\n<p>\u6b64\u5916, tcp_server_socket.listen(128)\u8868\u793a\u6700\u5927\u540c\u65f6\u76d1\u542c128\u4e2a\u5ba2\u6237\u7aef, \u800c\u6bcf\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6b64\u8bed\u53e5\u7684\u5b9e\u73b0\u5e76\u4e0d\u76f8\u540c( \u80fd\u540c\u65f6\u670d\u52a1\u591a\u5c11\u4e2a\u5ba2\u6237\u7aef, \u4e0e\u64cd\u4f5c\u7cfb\u7edf\u80fd\u529b\u6709\u5173 )<\/p>\n\n\n\n<p>tcp\u662f\u9762\u5411\u8fde\u63a5\u7684, \u4f46\u5e76\u4e0d\u662f\u4e00\u5bf9\u4e00, \u800c\u662f\u591a\u5bf9\u4e00,  \u4e00\u4e2a\u670d\u52a1\u5668\u53ef\u4ee5\u540c\u65f6\u670d\u52a1\u591a\u4e2a\u5ba2\u6237\u7aef, \u4f46\u6211\u4eec\u6240\u5b66\u4e60\u5230\u7684\u90fd\u53ea\u80fd\u540c\u65f6\u670d\u52a1\u5355\u4e2a\u5ba2\u6237\u7aef, \u8981\u540c\u65f6\u670d\u52a1\u591a\u4e2a\u5ba2\u6237\u7aef, \u5fc5\u987b\u4f7f\u7528\u5b66\u4e60\u591a\u4efb\u52a1, \u6211\u4eec\u540e\u7eed\u5b66\u4e60\u7684http\u534f\u8bae, \u4e5f\u5f80\u5f80\u662f\u57fa\u4e8e\u591a\u4efb\u52a1\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u8282\u6f14\u793a\u7b80\u5355\u7684tcp\u6587\u4ef6\u4e0b\u8f7d\u6848\u4f8b tcp\u670d\u52a1\u5668\u4ee3\u7801 \u8bfb\u53d6\u5ba2\u6237\u7aef\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u540d, \u6253\u5f00\u8be5\u6587\u4ef6, \u53d1\u9001\u5b57\u8282 \u5ba2\u6237\u7aef&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/python\/post-152.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">python\u7684tcp\u6587\u4ef6\u4e0b\u8f7d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/152"}],"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=152"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}