{"id":268,"date":"2021-11-14T14:37:27","date_gmt":"2021-11-14T06:37:27","guid":{"rendered":"http:\/\/madapapa.com\/wordpress\/?p=268"},"modified":"2021-11-14T14:59:40","modified_gmt":"2021-11-14T06:59:40","slug":"tao-jie-zipython2hepython3bu-tong","status":"publish","type":"post","link":"http:\/\/madapapa.com\/wordpress\/?p=268","title":{"rendered":"\u5957\u63a5\u5b57python2\u548cpython3\u4e0d\u540c"},"content":{"rendered":"<p>\u7f16\u5199web\u5e94\u7528\u670d\u52a1\u5668\u62a5\uff0cTypeError: a bytes-like object is required, not \u2018str\u2019\u9519\u8bef\u3002<br \/>\n\u56e0\u4e3apython2\u548cpython3\u7684\u7248\u672c\u6709\u4e9b\u5dee\u5f02\uff0c\u800cpython3\u6700\u91cd\u8981\u7684\u65b0\u7279\u6027\u4e5f\u662f\u5bf9\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u505a\u4e86\u66f4\u6e05\u6670\u7684\u533a\u5206\u3002<\/p>\n<pre><code class=\"language-plain_text\">\u6587\u672c\u7528unicode\u7f16\u7801\uff0c\u4e3astr\u7c7b\u578b\uff0c\u4e8c\u8fdb\u5236\u6570\u636e\u5219\u4e3abytes\u7c7b\u578b\n<\/code><\/pre>\n<h2><a id=\"python2%E7%9A%84%E4%BE%8B%E5%AD%90\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>python2\u7684\u4f8b\u5b50<\/h2>\n<pre><code class=\"language-plain_text\">import socket,sys \n\ns = socket.socket(socket.AF_INET,socket.SOCK_STREAM)\ns.connect(('www.madapapa.com',80))\n#code=str.encode('GET \/api.json HTTP\/1.0\\r\\n\\r\\n')\n#s.send(code)\ns.send('GET \/api.json HTTP\/1.0\\r\\n\\r\\n')\n\nwhile 1:\n    buf = s.recv(1000)\n    if not buf:\n        break\n    sys.stdout.write(buf)\n\ns.close()\n<\/code><\/pre>\n<p>\u5728python2\u7684\u73af\u5883\u4e0b\u8fd0\u884c\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b<br \/>\n<img decoding=\"async\" src=\"http:\/\/madapapa.com\/wordpress\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-14-at-2.45.47-PM.png\" alt=\"Screen Shot 2021-11-14 at 2.45.47 PM\" \/><\/p>\n<h2><a id=\"python3%E7%9A%84%E4%BE%8B%E5%AD%90\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>python3\u7684\u4f8b\u5b50<\/h2>\n<pre><code class=\"language-plain_text\">import socket,sys \n\ns = socket.socket(socket.AF_INET,socket.SOCK_STREAM)\ns.connect(('www.madapapa.com',80))\n#s.send(str.encode('GET \/api.json HTTP\/1.0\\r\\n\\r\\n'))\ns.send('GET \/api.json HTTP\/1.0\\r\\n\\r\\n'.encode())\n\n\nwhile 1:\n    buf = s.recv(1000).decode()\n    #s1 = buf.decode()\n    if not buf:\n        break\n    sys.stdout.write(buf)\n\ns.close()\n<\/code><\/pre>\n<p>\u5728\u9ed8\u8ba4\u73af\u5883\u4e0b\u8fd0\u884c\uff08python3\uff09\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b<br \/>\n<img decoding=\"async\" src=\"http:\/\/madapapa.com\/wordpress\/wp-content\/uploads\/2021\/11\/16368722865083.jpg\" alt=\"\" \/><\/p>\n<h1><a id=\"%E9%99%84%E5%BD%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u9644\u5f55<\/h1>\n<p>\u5957\u63a5\u5b57<\/p>\n<p>\u5957\u63a5\u5b57\u662f\u4e00\u5957\u7528C\u8bed\u8a00\u5199\u6210\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u5e93\uff0c\u5b83\u9996\u5148\u662f\u4e00\u4e2a\u5e93\u3002\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1\u548c\u7f51\u7edc\u7f16\u7a0b\uff0c\u56e0\u6b64\u5728\u7f51\u7edc\u5e94\u7528\u5f00\u53d1\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<p>\u5957\u63a5\u5b57\uff08socket\uff09\u662f\u4e00\u4e2a\u62bd\u8c61\u5c42\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5b83\u53d1\u9001\u6216\u63a5\u6536\u6570\u636e\uff0c\u53ef\u5bf9\u5176\u8fdb\u884c\u50cf\u5bf9\u6587\u4ef6\u4e00\u6837\u7684\u6253\u5f00\u3001\u8bfb\u5199\u548c\u5173\u95ed\u7b49\u64cd\u4f5c\u3002\u5957\u63a5\u5b57\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u4e0e\u7f51\u7edc\u4e2d\u7684\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u901a\u4fe1\u3002\u7f51\u7edc\u5957\u63a5\u5b57\u662fIP\u5730\u5740\u4e0e\u7aef\u53e3\u7684\u7ec4\u5408\u3002<\/p>\n<p>\u5728Python\u4e2d\u53ef\u4ee5\u57fa\u4e8e\u5957\u63a5\u5b57\u6765\u4f7f\u7528\u4f20\u8f93\u5c42\u63d0\u4f9b\u7684\u4f20\u8f93\u670d\u52a1\uff0c\u5e76\u4ee5\u6b64\u8fdb\u884c\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u3002\u5b9e\u9645\u5f00\u53d1\u4e2d\u4f7f\u7528\u7684\u5957\u63a5\u5b57\u53ef\u4ee5\u5206\u4e3a\u4e09\u7c7b\uff1a\u6d41\u5957\u63a5\u5b57\uff08TCP\u5957\u63a5\u5b57\uff09\u3001\u6570\u636e\u62a5\u5957\u63a5\u5b57\u548c\u539f\u59cb\u5957\u63a5\u5b57\u3002<\/p>\n<p>\u6d41\u5f0f\u5957\u63a5\u5b57<\/p>\n<p>\u6d41\u5f0f\u5957\u63a5\u5b57\uff08SOCK-STREAM\uff09\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u53ef\u9760\u7684\u3001\u53ef\u4ee5\u8fdb\u884c\u53cc\u5411\u8fde\u63a5\u7684\u6570\u636e\u4f20\u8f93\u670d\u52a1\u3002\u5176\u5b9e\u73b0\u4e86\u6570\u636e\u65e0\u5dee\u9519\u3001\u65e0\u91cd\u590d\u7684\u53d1\u9001\u3002\u6d41\u5f0f\u5957\u63a5\u5b57\u81ea\u8eab\u4fbf\u5185\u8bbe\u4e86\u6d41\u91cf\u63a7\u5236\u529f\u80fd\u3002\u5728TCP\/IP\u534f\u8bae\u7c07\u4e2d\uff0c\u4f7f\u7528TCP\u534f\u8bae\u6765\u5b9e\u73b0\u5b57\u8282\u6d41\u7684\u4f20\u8f93\uff0c\u5f53\u7528\u6237\u60f3\u8981\u53d1\u9001\u5927\u6279\u91cf\u7684\u6570\u636e\u6216\u8005\u5bf9\u6570\u636e\u4f20\u8f93\u6709\u8f83\u9ad8\u7684\u8981\u6c42\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d41\u5f0f\u5957\u63a5\u5b57\u3002<\/p>\n<p>\u6570\u636e\u62a5\u5957\u63a5\u5b57<\/p>\n<p>\u6570\u636e\u62a5\u5957\u63a5\u5b57\uff08SOCK-DGRAM\uff09\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u53cc\u5411\u6570\u636e\u4f20\u8f93\u670d\u52a1\u3002\u6570\u636e\u5305\u4ee5\u72ec\u7acb\u7684\u5f62\u5f0f\u88ab\u53d1\u9001\uff0c\u4e0d\u63d0\u4f9b\u53ef\u9760\u6027\u4fdd\u8bc1\u3002\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u4e22\u5931\u6216\u91cd\u590d\uff0c\u5e76\u4e14\u4e0d\u80fd\u4fdd\u8bc1\u5728\u63a5\u6536\u7aef\u6309\u53d1\u9001\u987a\u5e8f\u63a5\u6536\u6570\u636e\u3002\u5728TCP\/IP\u534f\u8bae\u7c07\u4e2d\uff0c\u4f7f\u7528UDP\u534f\u8bae\u6765\u5b9e\u73b0\u6570\u636e\u62a5\u5957\u63a5\u5b57\u3002\u5728\u51fa\u73b0\u5dee\u9519\u7684\u53ef\u80fd\u6027\u8f83\u5c0f\u6216\u5141\u8bb8\u90e8\u5206\u4f20\u8f93\u51fa\u9519\u7684\u5e94\u7528\u573a\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u62a5\u5957\u63a5\u5b57\u8fdb\u884c\u6570\u636e\u4f20\u8f93\uff0c\u8fd9\u6837\u901a\u4fe1\u7684\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n<p>\u539f\u59cb\u5957\u63a5\u5b57<\/p>\n<p>\u539f\u59cb\u5957\u63a5\u5b57\uff08SOCK-RAW\uff09\u3002\u8be5\u5957\u63a5\u5b57\u5141\u8bb8\u5bf9\u8f83\u4f4e\u5c42\u534f\u8bae\uff08\u5982IP\u6216ICMP\uff09\u8fdb\u884c\u76f4\u63a5\u8bbf\u95ee\uff0c\u5e38\u7528\u4e8e\u7f51\u7edc\u534f\u8bae\u5206\u6790\uff0c\u68c0\u9a8c\u65b0\u7684\u7f51\u7edc\u534f\u8bae\u5b9e\u73b0\uff0c\u4e5f\u53ef\u7528\u4e8e\u6d4b\u8bd5\u65b0\u914d\u7f6e\u6216\u5b89\u88c5\u7684\u7f51\u7edc\u8bbe\u5907\u3002<\/p>\n<p>\u4e0a\u8ff0\u5c31\u662f\u5957\u63a5\u5b57\u7684\u5927\u4f53\u77e5\u8bc6\uff0c\u5bf9\u5957\u63a5\u5b57\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6b65\u5165\u6b63\u9898\u4e86\u3002<\/p>\n<p>TCP\u5957\u63a5\u5b57<\/p>\n<p>TCP\u5957\u63a5\u5b57\u5c31\u662f\u4e0a\u9762\u6240\u8bf4\u7684\u6d41\u5f0f\u5957\u63a5\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528TCP\u534f\u8bae\u63d0\u4f9b\u7684\u4f20\u8f93\u670d\u52a1\u6765\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1\u7684\u7f16\u7a0b\u63a5\u53e3\u3002<\/p>\n<p>\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u521b\u5efasocket\u5bf9\u8c61\u5e76\u6307\u5b9atype\u5c5e\u6027\u4e3aSOCK_STREAM\u6765\u4f7f\u7528TCP\u5957\u63a5\u5b57\u3002<\/p>\n<p>\u7531\u4e8e\u4e00\u53f0\u4e3b\u673a\u53ef\u80fd\u62e5\u6709\u591a\u4e2aIP\u5730\u5740\uff0c\u6240\u4ee5\u4f5c\u4e3a\u670d\u52a1\u5668\u7aef\u7684\u7a0b\u5e8f\uff0c\u9700\u8981\u5728\u521b\u5efa\u5957\u63a5\u5b57\u5bf9\u8c61\u540e\u5c06\u5176\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u4e0a\u3002<\/p>\n<p>\u8fd9\u4e2a\u7aef\u53e3\u662f\u6307\u5bf9IP\u5730\u5740\u7684\u6269\u5c55\uff0c\u7528\u4e8e\u533a\u5206\u4e0d\u540c\u7684\u670d\u52a1\u3002<\/p>\n<p>\u4e0d\u540c\u7684\u670d\u52a1\u901a\u5e38\u4e0e\u76f8\u5bf9\u7684\u7aef\u53e3\u6240\u7ed1\u5b9a\u3002<\/p>\n<p>\u5f53\u670d\u52a1\u5668\u6536\u5230\u7528\u6237\u8bf7\u6c42\u65f6\u5c31\u53ef\u4ee5\u6839\u636e\u7aef\u53e3\u53f7\u6765\u786e\u5b9a\u5230\u5e95\u7528\u6237\u8bf7\u6c42\u7684\u662f\u4f55\u79cd\u670d\u52a1\u3002<\/p>\n<p>\u5173\u4e8e\u670d\u52a1\u4e8e\u7aef\u53e3\u7684\u7ed1\u5b9a\u7c7b\u578b\uff0c\u6211\u4eec\u4f1a\u5728\u4e4b\u540e\u7684\u5f00\u53d1\u7684\u5f53\u4e2d\u5177\u4f53\u63cf\u8ff0\u3002<\/p>\n<p>\u5982\u4f55\u7528Python\u8bbe\u7f6e\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u529f\u80fd\uff0c\u7b80\u6613\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>fromsocketimportsocket<\/p>\n<p>def main():<\/p>\n<h2><a id=\"1%E5%88%9B%E5%BB%BA%E5%A5%97%E6%8E%A5%E5%AD%97%E5%AF%B9%E8%B1%A1%E9%BB%98%E8%AE%A4%E4%BD%BF%E7%94%A8-ipv4%E5%92%8Ctcp%E5%8D%8F%E8%AE%AE\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1.\u521b\u5efa\u5957\u63a5\u5b57\u5bf9\u8c61\u9ed8\u8ba4\u4f7f\u7528IPv4\u548cTCP\u534f\u8bae<\/h2>\n<p>client= socket()<\/p>\n<h2><a id=\"2%E8%BF%9E%E6%8E%A5%E5%88%B0%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%8C%E6%8C%87%E5%AE%9A-ip%E5%9C%B0%E5%9D%80%E5%92%8C%E7%AB%AF%E5%8F%A3\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2.\u8fde\u63a5\u5230\u670d\u52a1\u5668 \uff0c\u6307\u5b9aIP\u5730\u5740\u548c\u7aef\u53e3<\/h2>\n<p>client.connect((&#8216;192.168.1.8&#8217;, 1080))<\/p>\n<h2><a id=\"3%E4%BB%8E%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%8E%A5%E6%94%B6%E6%95%B0%E6%8D%AE\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3.\u4ece\u670d\u52a1\u5668\u63a5\u6536\u6570\u636e<\/h2>\n<p>print(client.recv(1024).decode(&#8216;utf-8&#8217;))<\/p>\n<p>client.close()<\/p>\n<p>if__name__ == &#8216;<strong>main<\/strong>&#8216;:<\/p>\n<p>main()<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c31\u5b9e\u73b0\u4e86\u5ba2\u6237\u7aef\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u3002\u5f53\u7136\uff0c\u4e0a\u8ff0\u4ee3\u7801\u53ea\u80fd\u4f5c\u4e3a\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u5ba2\u6237\u7aef\u6784\u5efa\u65b9\u5f0f\u3002\u66f4\u590d\u6742\u7684\u6784\u5efa\u65b9\u5f0f\uff0c\u5b66\u8bb0\u5c06\u5728\u4e4b\u540e\u7684\u5b66\u4e60\u4e2d\u4e3a\u5927\u5bb6\u7ee7\u7eed\u4ecb\u7ecd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199web\u5e94\u7528\u670d\u52a1\u5668\u62a5\uff0cTypeError: a bytes-like object is required, not \u2018str\u2019\u9519\u8bef\u3002 \u56e0\u4e3apython2\u548cpython3\u7684\u7248\u672c\u6709\u4e9b\u5dee\u5f02\uff0c\u800cpython3\u6700\u91cd\u8981\u7684\u65b0\u7279\u6027\u4e5f\u662f\u5bf9\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u505a\u4e86\u66f4\u6e05\u6670\u7684\u533a\u5206\u3002 \u6587\u672c\u7528unicode\u7f16\u7801\uff0c\u4e3astr\u7c7b\u578b\uff0c\u4e8c\u8fdb\u5236\u6570\u636e\u5219\u4e3abytes\u7c7b\u578b python2\u7684\u4f8b\u5b50 import socket,sys s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((&#8216;www.madapapa.com&#8217;,80)) #code=str.encode(&#8216;GET \/api.json HTTP\/1.0\\r\\n\\r\\n&#8217;) #s.send(code) s.send(&#8216;GET \/api.json HTTP\/1.0\\r\\n\\r\\n&#8217;) while 1: buf = s.recv(1000) if not buf: break sys.stdout.write(buf) s.close() \u5728python2\u7684\u73af\u5883\u4e0b\u8fd0\u884c\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b python3\u7684\u4f8b\u5b50 import socket,sys s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((&#8216;www.madapapa.com&#8217;,80)) #s.send(str.encode(&#8216;GET \/api.json HTTP\/1.0\\r\\n\\r\\n&#8217;)) s.send(&#8216;GET \/api.json HTTP\/1.0\\r\\n\\r\\n&#8217;.encode()) while 1: buf = s.recv(1000).decode() #s1 &hellip; <a href=\"http:\/\/madapapa.com\/wordpress\/?p=268\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u5957\u63a5\u5b57python2\u548cpython3\u4e0d\u540c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[5,11],"tags":[42],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-linux","category-tech","tag-python"],"_links":{"self":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=268"}],"version-history":[{"count":2,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":270,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/268\/revisions\/270"}],"wp:attachment":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}