I have a python webserver that handles mostly POST requests and writes them to a text file but in the text file its written b'string' which is not suitable for what i will be using the string for. So how do I convert it to a normal string?
Here is my code
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write("Page is only for POST")
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b"POST")
txt = open("POST.txt", "w")
txt.write(str(body))
response.write(body)
self.wfile.write(response.getvalue())
print(body)
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()