简体   繁体   English

Python SocketServer 错误:TypeError:需要类似字节的对象,而不是“str”

[英]Python SocketServer Error : TypeError: a bytes-like object is required, not 'str'

I am making a Growtopia Server Emulator (For Educational Purposes : I'll test it and maybe making a private server in the future. I hope i'll accomplish) Weirdly i just got this error and I am using Stack overflow to fix goddamn errors thank y'all...我正在制作一个 Growtopia 服务器模拟器(出于教育目的:我会测试它,也许将来会制作一个私人服务器。我希望我能完成)奇怪的是,我刚收到这个错误,我正在使用堆栈溢出来修复该死的错误谢谢大家...

Here's my code :这是我的代码:

import http.server
import socketserver

class ServerHandler(http.server.BaseHTTPRequestHandler):
            def do_POST(self):
                self.send_response(200)
                self.end_headers()
                self.wfile.write("server|127.0.0.1\nport|17091\ntype|1\n#maint|Server is not available!\n\nbeta_server|127.0.0.1\nbeta_port|17091\n\nbeta_type|1\nmeta|localhost\nRTENDMARKERBS1001")
            def do_GET(self):
                self.send_response(200)
                self.end_headers()
                self.wfile.write("server|127.0.0.1\nport|17091\ntype|1\n#maint|Server is not available!\n\nbeta_server|127.0.0.1\nbeta_port|17091\n\nbeta_type|1\nmeta|localhost\nRTENDMARKERBS1001")
            def log_message(self, format, *args):
                return

            Handler = http.server.SimpleHTTPRequestHandler
PORT = 80
HOST = ""



OUT_HOST = HOST
info = OUT_HOST,PORT

httpd = socketserver.TCPServer((HOST, PORT), ServerHandler)

print("Server Port : ", PORT)
if "" in (OUT_HOST):
    print("Server Hostname : ", "localhost")
else:
    print("Server Hostname : ", HOST)

httpd.serve_forever()

Here's the Stacktrace:这是堆栈跟踪:

Exception happened during processing of request from ('127.0.0.1', 52331)
Traceback (most recent call last):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\http\server.py", line 427, in handle
    self.handle_one_request()
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\http\server.py", line 415, in handle_one_request
    method()
  File "C:/Users/HP/PycharmProjects/Crescentstar/main.py", line 10, in do_POST
    self.wfile.write("server|127.0.0.1\nport|17091\ntype|1\n#maint|Server is not available!\n\nbeta_server|127.0.0.1\nbeta_port|17091\n\nbeta_type|1\nmeta|localhost\nRTENDMARKERBS1001").encode()
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\socketserver.py", line 799, in write
    self._sock.sendall(b)
TypeError: a bytes-like object is required, not 'str'

Based on the information provided without a stack trace, I believe you probably should be providing bytes rather than a string to wfile.write.根据提供的没有堆栈跟踪的信息,我相信您可能应该向 wfile.write 提供字节而不是字符串。

Either add a .encode() on the end of the string you want to write, or prefix it with a b.在要写入的字符串的末尾添加一个 .encode(),或者在它前面加上一个 b。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python错误:TypeError:需要一个类似字节的对象,而不是'str' - Python error: TypeError: a bytes-like object is required, not 'str' python错误TypeError:需要一个类似字节的对象,而不是'str' - python error TypeError: a bytes-like object is required,not 'str' 需要一个类似字节的对象,而不是'str':TypeError - a bytes-like object is required, not 'str' : TypeError TypeError:需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”? - TypeError: a bytes-like object is required, not 'str'? TypeError:需要一个类似字节的对象,对于无服务器和Python3来说不是'str' - TypeError: a bytes-like object is required, not 'str' with serverless and Python3 Python 3,TypeError:需要一个类似字节的对象,而不是'str' - Python 3, TypeError: a bytes-like object is required, not 'str' Python3 TypeError:需要一个类似字节的对象,而不是“str” - Python3 TypeError: a bytes-like object is required, not 'str' Python TypeError-必需的类似字节的对象,而不是str - Python TypeError - required bytes-like object instead of str python - 类型错误:需要类似字节的 object,而不是“str” - python - TypeError: a bytes-like object is required, not 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM