Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使字节字符串成为普通字符串?_Python_Python 3.x_String_Windows - Fatal编程技术网

Python 如何使字节字符串成为普通字符串?

Python 如何使字节字符串成为普通字符串?,python,python-3.x,string,windows,Python,Python 3.x,String,Windows,我有一个pythonwebserver,它主要处理POST请求并将它们写入文本文件,但在文本文件中写入的b'string'不适合我将使用字符串的目的。那么如何将其转换为普通字符串呢 这是我的密码 from http.server import HTTPServer, BaseHTTPRequestHandler from io import BytesIO class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do

我有一个pythonwebserver,它主要处理POST请求并将它们写入文本文件,但在文本文件中写入的
b'string'
不适合我将使用字符串的目的。那么如何将其转换为普通字符串呢

这是我的密码

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

您正在从请求接收原始字节;除非您需要修改编码,否则您应该只写入原始字节,而不是尝试写入文本。在这种情况下,唯一需要更改的是:

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

要正确地完成这项工作,它实际上应该是:

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

因此
with
语句确保数据被及时写入,文件以确定的方式正确关闭。

通过HTTP接收的数据是字节形式的。在将其写入文件之前,需要对其进行解码。将
txt.write(str(body))
替换为
txt.write(body.decode(“utf-8”)
。这将把代码转换回字符串格式。 完整的代码如下所示:

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

谢谢,您的代码运行正常,我的文件不再有字节字符串。
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()