简体   繁体   English

Python需要类似字节的对象,而不是'str'

[英]Python a bytes-like object is required, not 'str'

I am new to Python. 我是Python的新手。 I am running following simple web server: 我正在运行以下简单的Web服务器:

from wsgiref.simple_server import make_server
from io import BytesIO

def message_wall_app(environ, start_response):
    output = BytesIO()
    status = '200 OK' # HTTP Status
    headers = [('Content-type', 'text/html; charset=utf-8')]
    start_response(status, headers)
    print(b"<h1>Message Wall</h1>",file=output)
##    if environ['REQUEST_METHOD'] == 'POST': 
##        size = int(environ['CONTENT_LENGTH'])
##        post_str = environ['wsgi.input'].read(size)
##        print(post_str,"<p>", file=output)
##    print('<form method="POST">User: <input type="text" '
##          'name="user">Message: <input type="text" '
##          'name="message"><input type="submit" value="Send"></form>', 
##           file=output)         
    # The returned object is going to be printed
    return [output.getvalue()]     

httpd = make_server('', 8000, message_wall_app)
print("Serving on port 8000...")

# Serve until process is killed
httpd.serve_forever()

Unfortunately i get following error: 不幸的是,我得到以下错误:

Traceback (most recent call last):
  File "C:\Users\xxx\Python36\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "C:/xxx/Python/message_wall02.py", line 9, in message_wall_app
    print("<h1>Message Wall</h1>".encode('ascii'),file=output)
TypeError: a bytes-like object is required, not 'str'....

Please suggest what i am doing wrong. 请提出我在做什么错。 thanks. 谢谢。

You can't use print() to write to binary files. 您不能使用print()写入二进制文件。 print() converts arguments to str() before writing to a text file object. print()在写入文本文件对象之前将参数转换为str()

From the print() function documentation : print()函数文档中

Print objects to the text stream file , separated by sep and followed by end . 将对象打印到文本流 文件中 ,并用sep分隔,然后用end分隔。 [...] [...]

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end . 所有非关键字参数都将像str()一样转换为字符串,并写入流中,以sep分隔,后跟end

Bold emphasis mine . 大胆强调我 Note that the file object must be a text stream, not a binary stream. 请注意,文件对象必须是文本流,而不是二进制流。

Either write to a TextIOWrapper() object wrapping your BytesIO() object, call .write() on the BytesIO() object to write bytes objects directly, or write to a StringIO() object and encode the resulting string value at the end. 或者写入一个TextIOWrapper()对象包裹您BytesIO()对象,可以调用.write()的上BytesIO()对象写入bytes的物品直接,或写入到StringIO()对象并在结束编码所得到的字符串值。

暂无
暂无

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

相关问题 python:&quot;\\&quot; in str 需要一个类似字节的对象,而不是 &#39;str&#39; - python:"\" in str a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是&#39;str&#39;python - TypeError: a bytes-like object is required, not 'str' python Python 错误:需要类似字节的对象,而不是“str” - Python error:a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是 Python 中 Image 命令的“str” - TypeError: a bytes-like object is required, not 'str' for Image command in Python Python TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - Python TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是&#39;str&#39;python3 - TypeError: a bytes-like object is required, not 'str' python3 类型错误:需要类似字节的 object,而不是使用子进程 python 的“str” - TypeError: a bytes-like object is required, not 'str' using Subprocess python Python 错误类型错误:需要类似字节的 object,而不是“str” - Python ERROR TypeError: a bytes-like object is required, not 'str' python错误TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - python error TypeError: a bytes-like object is required,not 'str' 类型错误:需要一个类似字节的对象,而不是 python 和 CSV 中的“str” - TypeError: a bytes-like object is required, not 'str' in python and CSV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM