0

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()

2 Answers 2

1

You're receiving raw bytes from the request; unless you need to modify the encoding, you should just write raw bytes instead of trying to write text. In this case, the only change needed is to change from:

txt = open("POST.txt", "w")
txt.write(str(body))

to:

txt = open("POST.txt", "wb")  # Binary mode
txt.write(body)  # Don't convert to str

To do it properly, it should really be:

with open("POST.txt", "wb") as txt:
    txt.write(body)

so the with statement ensures the data is promptly written and the file properly closed in a deterministic fashion.

Sign up to request clarification or add additional context in comments.

Comments

0

The data received over HTTP is in byte form. You need to decode it before writing it to the file. Replace txt.write(str(body)) with txt.write(body.decode("utf-8")). This will convert your code back to the string format. The complete code will be as such:

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(body.decode("utf-8"))
        response.write(body)
        self.wfile.write(response.getvalue())
        print(body)

httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()

1 Comment

Thanks, your code worked and my file no longer had a byte string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.