2
我有一个文件,我正在阅读它,如下所示。 [忽略所有的连接相关参数]无法发送文件内容以及python中的http标头
somefile=open(/path/to/some/file,'rb')
READ_somefile=somefile.read()
somefile.close()
client_connection.send((str.encode('HTTP/1.1 200 OK\nContent-Type: image/png\n\n')))
client_connection.send((READ_somefile))
我能够正确显示我的Html网页,当我用上面的代码。 但我想只使用一个发送而不是两个,这就产生了问题。 我尝试使用以下内容
client_connection.send((str.encode('HTTP/1.1 200 OK\nContent-Type: image/png\n\n',READ_somefile)))
我得到下面的错误。
client_connection.send((str.encode('HTTP/1.1 200 OK\nContent-Type: image/png\n\n',READ_somefile)))
TypeError: encode() argument 1 must be str, not bytes
然后我试着用这个。
client_connection.send((str.encode('HTTP/1.1 200 OK\nContent-Type: image/png\n\n',str(READ_somefile))))
我收到以下错误消息。
client_connection.send((str.encode('HTTP/1.1 200 OK\nContent-Type: image/png\n\n',str(READ_somefile))))
LookupError: unknown encoding: b'/*!
您能否让我知道我应该在这里使用什么样的编码来发送标题和内容?
请注意,我不能使用任何外部模块。
我猜你”重新尝试使用纯Python发送一个网页?你没有像烧瓶一样使用Web框架?乐于帮助,只是寻找更多的信息。干杯! –
嗨是的,我使用Python创建一个简单的Web服务器,然后发送一个网页。没有使用外部模块或框架。 –
一切工作正常,但我无法找出正确的编码和解码。 –