@@ -356,11 +356,11 @@ def send_error(self, code, message=None):
356356 content = (self .error_message_format %
357357 {'code' : code , 'message' : _quote_html (message ), 'explain' : explain })
358358 self .send_response (code , message )
359- self .send_header ("Content-Type" , "text/html" )
359+ self .send_header ("Content-Type" , "text/html;charset=utf-8 " )
360360 self .send_header ('Connection' , 'close' )
361361 self .end_headers ()
362362 if self .command != 'HEAD' and code >= 200 and code not in (204 , 304 ):
363- self .wfile .write (content )
363+ self .wfile .write (content . encode ( 'UTF-8' , 'replace' ) )
364364
365365 error_message_format = DEFAULT_ERROR_MESSAGE
366366
@@ -378,16 +378,16 @@ def send_response(self, code, message=None):
378378 else :
379379 message = ''
380380 if self .request_version != 'HTTP/0.9' :
381- self .wfile .write ("%s %d %s\r \n " %
382- (self .protocol_version , code , message ))
381+ self .wfile .write (( "%s %d %s\r \n " %
382+ (self .protocol_version , code , message )). encode ( 'ASCII' , 'strict' ))
383383 # print (self.protocol_version, code, message)
384384 self .send_header ('Server' , self .version_string ())
385385 self .send_header ('Date' , self .date_time_string ())
386386
387387 def send_header (self , keyword , value ):
388388 """Send a MIME header."""
389389 if self .request_version != 'HTTP/0.9' :
390- self .wfile .write ("%s: %s\r \n " % (keyword , value ))
390+ self .wfile .write (( "%s: %s\r \n " % (keyword , value )). encode ( 'ASCII' , 'strict' ))
391391
392392 if keyword .lower () == 'connection' :
393393 if value .lower () == 'close' :
@@ -398,7 +398,7 @@ def send_header(self, keyword, value):
398398 def end_headers (self ):
399399 """Send the blank line ending the MIME headers."""
400400 if self .request_version != 'HTTP/0.9' :
401- self .wfile .write ("\r \n " )
401+ self .wfile .write (b "\r \n " )
402402
403403 def log_request (self , code = '-' , size = '-' ):
404404 """Log an accepted request.
0 commit comments