简体   繁体   中英

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...

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.

Either add a .encode() on the end of the string you want to write, or prefix it with a b.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM