[英]Python http server giving error when connected to
我的服務器代碼出錯了。 它會直到瀏覽器嘗試連接到它。 我真的不知道它是什么。 任何人都可以看一看並指出我正確的方向嗎?
錯誤代碼是
Exception happened during processing of request from ('127.0.0.1', 57953)
Traceback (most recent call last):
File "C:\Python34\lib\socketserver.py", line 306, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python34\lib\socketserver.py", line 332, in process_request
self.finish_request(request, client_address)
File "C:\Python34\lib\socketserver.py", line 345, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python34\lib\socketserver.py", line 666, in __init__
self.handle()
File "C:\Python34\lib\http\server.py", line 400, in handle
self.handle_one_request()
File "C:\Python34\lib\http\server.py", line 388, in handle_one_request
method()
File "C:\Ny mapp\serverpy.py", line 44, in do_GET
self.wfile.write(f.read())
File "C:\Python34\lib\socket.py", line 391, in write
return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
劇本
#!/usr/bin/python
from http.server import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
import cgi
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
if self.path=="/":
self.path="/index.html"
try:
#Check the file extension required and
#set the right mime type
sendReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendReply = True
if self.path.endswith(".jpg"):
mimetype='image/jpg'
sendReply = True
if self.path.endswith(".gif"):
mimetype='image/gif'
sendReply = True
if self.path.endswith(".js"):
mimetype='application/javascript'
sendReply = True
if self.path.endswith(".css"):
mimetype='text/css'
sendReply = True
if sendReply == True:
#Open the static file requested and send it
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
#Handler for the POST requests
def do_POST(self):
if self.path=="/send":
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
print("Your name is: %s" % form["your_name"].value)
self.send_response(200)
self.end_headers()
self.wfile.write("Thanks %s !" % form["your_name"].value)
return
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print('Started httpserver on port ' , PORT_NUMBER)
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the web server')
server.socket.close()
你的錯誤:
TypeError: 'str' does not support the buffer interface
表示在socket
, self._sock.send(b)
只接受bytes
對象。 所以你需要發送一個字節編碼的字符串。
嘗試使用以下內容:
def do_GET(self):
if self.path=="/":
self.path="/index.html"
try:
sendReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendReply = True
if self.path.endswith(".jpg"):
mimetype='image/jpg'
sendReply = True
if self.path.endswith(".gif"):
mimetype='image/gif'
sendReply = True
if self.path.endswith(".js"):
mimetype='application/javascript'
sendReply = True
if self.path.endswith(".css"):
mimetype='text/css'
sendReply = True
if sendReply == True:
#Open the static file requested and send it
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
# save the contents
read = f.read()
# write the contents as bytes
self.wfile.write(bytes(read, 'utf-8'))
f.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
使用'rb'
。 webfont(.ttf; .woff; woff2)等沒關系。
f = open(filepath, 'rb')
data = f.read()
self.wfile.write(data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.