2016-11-29 33 views
0

我試圖讓簡單的HTTP服務器加載字體真棒,但可以得到它的工作,我只得到小方塊。蟒蛇簡單的HTTP服務器不加載字體真棒字體

我認爲這可能是某種類型的MIME類型問題。 如果頁面直接在瀏覽器中打開,但不是由簡單HTTP服務器加載,則會加載HTML頁面和字體真棒字體。

這個問題似乎是相同的,但放置

<meta charset="UTF-8"> 

在HTML代碼中並沒有幫助我。 Font awesome not loading when using Python simple http server

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer 
from os import curdir, sep 
import cgi 

import subprocess as sub 
import time 

PORT_NUMBER = 8080 


class myHandler(BaseHTTPRequestHandler): 


    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(".png"): 
       mimetype='image/png' 
       sendReply = True 
      if self.path.endswith(".gif"): 
       mimetype='image/gif' 
       sendReply = True 
      if self.path.endswith(".svg"): 
       mimetype='image/svg+xml' 
       sendReply = True 
      if self.path.endswith(".css"): 
       mimetype='text/css' 
       sendReply = True 
      if self.path.endswith(".js"): 
       mimetype='application/javascript' 
       sendReply = True 
      if self.path.endswith(".ttf"): 
       mimetype='application/x-font-ttf' 
       sendReply = True 
      if self.path.endswith(".otf"): 
       mimetype='application/x-font-opentype' 
       sendReply = True 
      if self.path.endswith(".woff"): 
       mimetype='application/font-woff' 
       sendReply = True 

      if sendReply == True: 
       #Open the static file requested and send it 
       f = open(curdir + sep + self.path, 'rb') 
       self.send_response(200) 
       self.send_header('Content-type',mimetype) 
       self.end_headers() 
       self.wfile.write(f.read()) 
       f.close() 
       print(curdir + sep + self.path) 
      return 

     except IOError: 
      self.send_error(404,'File Not Found: %s' % self.path) 

    def do_POST(self): 

     if self.path=="/run": 
      form = cgi.FieldStorage(
       fp=self.rfile, 
       headers=self.headers, 
       environ={'REQUEST_METHOD':'POST', 
         'CONTENT_TYPE':self.headers['Content-Type'], 
      }) 


         #self.send_response(200) 
         #self.end_headers() 
      return 

這是HTML頁面的一部分:

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>HW Test</title> 

     <!-- Bootstrap --> 

     <link href = "css/bootstrap.min.css" rel="stylesheet"></link> 
     <link href = "css/custom.css" rel="stylesheet"></link> 
     <link href = "css/font-awesome.min.css" rel="stylesheet"></link> 

     <script src="js/bootstrap.min.js"></script> 

</head> 


<body> 
    <div class = "container"> 
     <div class="row"> 
      <input id="box1" type="checkbox" /> 
      <label for="box1">Checkbox 1</label> 
      <input id="box2" type="checkbox" /> 
      <label for="box2">Checkbox 2</label> 
     </div> 
... 

的複選框顯示爲小方塊,而不是複選框。 這些應該由字體真棒顯示,不會加載。

回答

1

我認爲這個問題是在字體文件名,例如,font-awesome.css最後一個版本包含:

@font-face { 
    font-family: 'FontAwesome'; 
    src: url('../fonts/fontawesome-webfont.eot?v=4.7.0'); 
    src: url('./fonts/fontawesome-webfont.woff2?v=4.7.0') ... 

這意味着self.path包含類似:

/fonts/fontawesome-webfont.woff2?v=4.7.0 

而且檢查一樣if self.path.endswith(".woff")會失敗。

+0

是的,就是這樣。謝謝! – Tobbe