2011-03-13 1 views
4

J'utilise BaseHTTPServer pour diffuser du contenu Web. Je peux servir les types de contenu 'text/html' ou 'text/css' ou même 'text/js' et cela se fait du côté du navigateur. Mais lorsque je tente deComment utiliser les types de contenu d'image avec la méthode Python BaseHTTPServerRequestHandler do_GET?

self.send_header('Content-type', 'image/png')

pour un fichier .png, il ne rend pas du tout.

Voici un exemple:

    if self.path.endswith(".js"): 
          f = open(curdir + sep + self.path) 
          self.send_response(200) 
          self.send_header('Content-type',  'text/javascript') 
          self.end_headers() 
          self.wfile.write(f.read()) 
          f.close() 
          return 

cela fonctionne très bien pour le javascript

    if self.path.endswith(".png"): 
          f=open(curdir + sep + self.path) 
          self.send_response(200) 
          self.send_header('Content-type',  'image/png') 
          self.end_headers() 
          self.wfile.write(f.read()) 
          f.close() 
          return 

cela ne semble pas rendre le contenu de l'image quand je marque vers le haut pour le côté client. Il apparaît comme une image brisée.

Des idées?

Répondre

9

Vous avez ouvert le fichier en mode texte au lieu du mode binaire. Tous les caractères de nouvelle ligne sont susceptibles d'être foiré. Utilisez ceci:

f = open(curdir + sep + self.path, 'rb') 
+0

Cela prend tout son sens. Je ne me suis même pas demandé quel était le mode. C'est exactement ça, merci! –

+0

Cela a très bien fonctionné pour moi aussi, merci. – S4nd33p

1

vous pouvez toujours ouvrir le fichier en binaire ;-)

Peut-être que vous pourriez regarder SimpleHTTPServer.py à cette partie du code:

 
    ctype = self.guess_type(path) 
    try: 
     # Always read in binary mode. Opening files in text mode may cause 
     # newline translations, making the actual size of the content 
     # transmitted *less* than the content-length! 
     f = open(path, 'rb') 
    except IOError: 
     self.send_error(404, "File not found") 
     return None 

Ensuite, si vous regardez def guess_type (auto, chemin): son très simple, il utilise le fichier "extension" ;-)

 

    Return value is a string of the form type/subtype, 
    usable for a MIME Content-type header. 

    The default implementation looks the file's extension 
    up in the table self.extensions_map, using application/octet-stream 
    as a default; however it would be permissible (if 
    slow) to look inside the data to make a better guess. 

Juste au cas où, le code est:

 

    base, ext = posixpath.splitext(path) 
    if ext in self.extensions_map: 
     return self.extensions_map[ext] 
    ext = ext.lower() 
    if ext in self.extensions_map: 
     return self.extensions_map[ext] 
    else: 
     return self.extensions_map[''] 

+0

Votre coloration syntaxique semble avoir cassé – Sirens

3

Essayez d'utiliser SimpleHTTPServer

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 
    """modify Content-type """ 
    def guess_type(self, path): 
     mimetype = SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(
      self, path 
      ) 
     if mimetype == 'application/octet-stream': 
      if path.endswith('manifest'): 
       mimetype = 'text/cache-manifest' 
     return mimetype 

voir /usr/lib/python2.7/SimpleHTTPServer.py pour plus d'information.

Questions connexes