【问题标题】:Keep getting exception in python webserver在 python 网络服务器中不断出现异常
【发布时间】:2012-10-30 20:51:42
【问题描述】:

我有一个非常简单的 python 网络服务器,它返回几个网页,并且不断抛出 TypeError: 'str' does not support the buffer interface。这是我的代码,谁能告诉我哪里错了?

from os import curdir
from os.path import join as pjoin
from http.server import BaseHTTPRequestHandler, HTTPServer
class StoreHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/store.json":
            with open(pjoin(curdir, 'store.json')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/json')
                self.end_headers()
                self.wfile.write(fh.read())
        elif self.path == "/Stock.htm":
            with open(pjoin(curdir, 'stock.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read())
        else:
            with open(pjoin(curdir, 'index.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read())
    def do_POST(self):
        if self.path == '/store.json':
            length = self.headers.getheader('content-length')
            data = self.rfile.read(int(length))
            with open(pjoin(curdir, 'store.json'), 'w') as fh:
                fh.write(data)
            self.send_response(200)

server = HTTPServer(('', 8080), StoreHandler)
server.serve_forever()

这里是异常输出:

127.0.0.1 - - [30/Oct/2012 16:48:17] "GET / HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 58645)
Traceback (most recent call last):
  File "C:\Program Files\Python33\lib\socketserver.py", line 306, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files\Python33\lib\socketserver.py", line 332, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files\Python33\lib\socketserver.py", line 345, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files\Python33\lib\socketserver.py", line 666, in __init__
    self.handle()
  File "C:\Program Files\Python33\lib\http\server.py", line 400, in handle
    self.handle_one_request()
  File "C:\Program Files\Python33\lib\http\server.py", line 388, in handle_one_request
    method()
  File "C:\Users\Arlen\Desktop\Stock Recorder\webserver.py", line 25, in do_GET
    self.wfile.write(fh.read())
  File "C:\Program Files\Python33\lib\socket.py", line 317, in write
    return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
----------------------------------------

更新:这是我更新后的代码的样子:

from os import curdir
from os.path import join as pjoin

from http.server import BaseHTTPRequestHandler, HTTPServer

class StoreHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/store.json":
            with open(pjoin(curdir, 'store.json')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/json')
                self.end_headers()
                self.wfile.write(fh.read(), 'rb')
        elif self.path == "/Stock.htm":
            with open(pjoin(curdir, 'stock.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read(), 'rb')
        else:
            with open(pjoin(curdir, 'index.htm')) as fh:
                self.send_response(200)
                self.send_header('Content-type','text/html')
                self.end_headers()
                self.wfile.write(fh.read(),'rb')
    def do_POST(self):
        if self.path == '/store.json':
            length = self.headers.getheader('content-length')
            data = self.rfile.read(int(length))
            with open(pjoin(curdir, 'store.json'), 'w') as fh:
                fh.write(data)
            self.send_response(200)

server = HTTPServer(('', 8080), StoreHandler)
server.serve_forever()

【问题讨论】:

    标签: python exception python-3.x


    【解决方案1】:

    套接字发送和接收字节,但您尝试通过 unicode 字符串发送,因为您在未指定模式的情况下打开文件(请记住,在 Python 3 中,默认情况下所有字符串都是 unicode)。

    您可以:

    - 或 -

    • 以二进制模式打开文件 - 将 open(pjoin(curdir, 'a.file')) 更改为 open(pjoin(curdir, 'store.json'), 'rb')(注意额外的 rb 参数)。

    【讨论】:

    • 上面写着TypeError: write() takes 2 positional arguments but 3 were given
    • @ArlenBeiler - 应该是您的 with open 呼叫添加了 , 'rb' - write 呼叫不知道如何处理它(再次阅读我的答案); -)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多