在python webserver中保持异常 [英] Keep getting exception in python webserver
问题描述
我有一个非常简单的python webserver返回一些网页,并且它不断地抛出$ code> TypeError:'str'不支持缓冲区接口。这是我的代码,任何人都可以告诉什么是错的?
from os import curdir
from os.path import join作为pjoin
从http.server import BaseHTTPRequestHandler,HTTPServer
class StoreHandler(BaseHTTPRequestHandler):
def do_GET(self):
如果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:
打开(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):
如果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.1200 -
-------------------- --------------------
从('127.0.0.1',58645)的请求处理过程中发生异常
追溯(最近的呼叫最后):
文件C:\Program Fi文件C:\Program Files\Python33\lib\\,在_handle_request_noblock
中,self.process_request(request,client_address)
文件les \Python33\lib\socketserver.py \\ socketserver.py,第332行,process_request
self.finish_request(request,client_address)
文件C:\Program Files\Python33\lib\socketserver.py,第345行,在finish_request
self.RequestHandlerClass(request,client_address,self)
文件C:\Program Files\Python33\lib\socketserver.py,第666行,__init__
self.handle()
文件C:\Program Files\Python33\lib\http\server.py,行400,在句柄
self.handle_one_request()
文件C:\Program Files\Python33\lib\http\server.py,第388行,handle_one_request
method()
文件C:\Users\\ \\ Arlen\Desktop\Stock Recorder\webserver.py,第25行,在do_GET
self.wfile.write(fh.read())
文件C:\Program Files\Python33\lib\socket.py,第317行,写入
return self._sock .send(b)
TypeError:'str'不支持缓冲区接口
-------------------------- --------------
更新: strong>以下是我更新的代码如何:
从os import curdir
从os.path import join作为pjoin
来自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 ==/库存.htm:
与ope n(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):
如果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 ()
套接字发送和接收字节,但你是尝试发送unicode字符串,因为您打开文件而不指定模式(记住,在Python 3中,所有字符串默认为unicode)。
您可以: p>
- 使用
bytes
内置函数来转换数据
- 或 -
- 以二进制模式打开文件 - 更改
open(pjoin(curdir,'a.file'))
toopen(pjoin(curdir,'store.json'),'rb')
(注意额外的rb
参数)。
I have a pretty simple python webserver that returns a few web pages, and it keeps throwing TypeError: 'str' does not support the buffer interface
. Here is my code, can anyone tell what is wrong?
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()
Here is the exception output:
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
----------------------------------------
Update: Here is how my updated code looks:
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()
Sockets send and receive bytes, but you are attempting to send over unicode strings since you opened the file without specifying the mode (remember, in Python 3 all strings are unicode by default).
You can either:
- Use the
bytes
built-in function to transform the data
- or -
- Open the file in binary mode - change
open(pjoin(curdir, 'a.file'))
toopen(pjoin(curdir, 'store.json'), 'rb')
(note the additionalrb
parameter).
这篇关于在python webserver中保持异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!