{"id":49736,"date":"2024-12-03T15:45:48","date_gmt":"2024-12-03T07:45:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49736\/"},"modified":"2024-12-03T15:45:48","modified_gmt":"2024-12-03T07:45:48","slug":"python-3-%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-typeerror-a-bytes-like-object-is-required-not-str-%e7%bc%96%e7%a0%81%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49736\/","title":{"rendered":"Python 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f<\/h1>\n<p>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/17305268866725bea602219.jpg\" class=\"aligncenter\" title=\"Python 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"Python 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python 3 \u7f16\u7801\u95ee\u9898\u8be6\u89e3<\/strong><\/p>\n<p>\u5728 python 3 \u4e2d\u89e3\u51b3\u7f16\u7801\u95ee\u9898\u65f6\uff0c\u9700\u8981\u683c\u5916\u6ce8\u610f bytes \u548c str \u5bf9\u8c61\u3002<\/p>\n<p>\u95ee\u9898\u4e2d\u63d0\u5230\u7684\u62a5\u9519\u4fe1\u606f &#8220;typeerror: a bytes-like object is required, not &#8216;str'&#8221; \u7684\u610f\u601d\u662f\uff0c\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002\u5373\u4f7f data \u5df2\u7ecf\u662f bytes \u7c7b\u578b\uff0c\u4f46 ctime() \u7f16\u7801\u540e\u7684\u7ed3\u679c\u4ecd\u7136\u662f\u5b57\u7b26\u4e32\uff0c\u5bfc\u81f4\u4e86\u8fd9\u4e2a\u9519\u8bef\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5c06 ctime() \u7f16\u7801\u7684\u7ed3\u679c\u4e5f\u8f6c\u6362\u4e3a bytes \u7c7b\u578b\u3002\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre># \u670d\u52a1\u5668\n\nfrom socket import *\nfrom time import ctime\n\nHOST = ''\nPORT = 21507\nBUFSIZE = 1024\nADDR = (HOST, PORT)\n\ntcpSerSock = socket(AF_INET, SOCK_STREAM)\ntcpSerSock.bind(ADDR)\ntcpSerSock.listen(5)\n\nwhile True:\n    print('waiting for connection...')\n    tcpCliSock, addr = tcpSerSock.accept()\n    print('...connected from:', addr)\n\n    while True:\n        data = tcpCliSock.recv(BUFSIZE)\n        if not data:\n            break\n        print(type(data))\n        tcpCliSock.send(('[{}] {}'.format(ctime().encode('utf-8'), data.decode()).encode()))\n    tcpCliSock.close()<\/pre>\n<p>\u901a\u8fc7\u5c06 ctime() \u7f16\u7801\u540e\u7684\u7ed3\u679c\u8f6c\u6362\u4e3a bytes \u7c7b\u578b\uff0c\u6211\u4eec\u89e3\u51b3\u4e86\u7f16\u7801\u95ee\u9898\uff0c\u670d\u52a1\u5668\u53ef\u4ee5\u6b63\u786e\u5730\u63a5\u6536\u548c\u53d1\u9001\u6570\u636e\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPython 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f \u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPython 3 \u4e2d\u5982\u4f55\u89e3\u51b3 \\&#8221;TypeError: a bytes-like object is required, not \\&#8217;str\\&#8217;\\&#8221; \u7f16\u7801\u9519\u8bef\uff1f\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 python 3 \u7f16\u7801\u95ee\u9898\u8be6\u89e3 \u5728 python 3 \u4e2d\u89e3\u51b3\u7f16\u7801\u95ee\u9898\u65f6\uff0c\u9700\u8981\u683c\u5916\u6ce8\u610f bytes \u548c str \u5bf9\u8c61\u3002 \u95ee\u9898\u4e2d\u63d0\u5230\u7684\u62a5\u9519\u4fe1\u606f &#8220;typeerror: a bytes-like object is required, not &#8216;str&#8217;&#8221; \u7684\u610f\u601d\u662f\uff0c\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002\u5373\u4f7f data \u5df2\u7ecf\u662f bytes \u7c7b\u578b\uff0c\u4f46 ctime() \u7f16\u7801\u540e\u7684\u7ed3\u679c\u4ecd\u7136\u662f\u5b57\u7b26\u4e32\uff0c\u5bfc\u81f4\u4e86\u8fd9\u4e2a\u9519\u8bef\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5c06 ctime() \u7f16\u7801\u7684\u7ed3\u679c\u4e5f\u8f6c\u6362\u4e3a bytes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49736","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49736"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49736\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}