1

Why is my background not blue when I go to localhost:8080 in my browser? The following 3 files are all located in the same directory:

wsgiwebsite.py

#!/usr/bin/env python
from wsgiref.simple_server import make_server

cont = (open('wsgiwebsite_content.html').read())

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return [cont]

server = make_server('0.0.0.0', 8080, application)
server.serve_forever()

wsgiwebsite_content.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="wsgiwebsite_style.css">
  </head>

  <body>
    This is my first web page
  </body>
</html>

wsgiwebsite_style.css

body{background-color:blue;}

4 Answers 4

1

WSGI only serves your Python code and probably doesn't even know about existence of that CSS file.

You can either configure your web-server to handle static assets for you, or use something like static to also serve static media.

Sign up to request clarification or add additional context in comments.

2 Comments

When you say "configure your web server", do you mean configure the wsgiwebsite.py-file? How do I do that? Thanks for the static alternative, but I'd like to do it without resorting to external libraries.
Typically WSGI application is not exposed to the internet- some ordinary Web server is in front of it (Apache, nginx, lighttpd, etc). You can configure Web server to serve your Python app at URL "/", and configure "/media/" URL to point to directory that contains static files.
1

You try to load the css via your wsgi server but your server always just returns the html file. Have a look with firebug/web inspector/... to see the response from the server for the css file.

Comments

1

This code snippet below is just for learning purpose. I created a static directory and saved my index.html and css file there, thus my own source code files are not accessible.

from wsgiref.simple_server import make_server                                    
import os                                                                        


def content_type(path):                                                          
if path.endswith(".css"):                                                    
    return "text/css"                                                        
else:                                                                        
    return "text/html"                                                       


def app(environ, start_response):                                                
    path_info = environ["PATH_INFO"]                                             
    resource = path_info.split("/")[1]                                           

    headers = []                                                                 
    headers.append(("Content-Type", content_type(resource)))                     

    if not resource:                                                             
        resource = "index.html"                                                  

    resp_file = os.path.join("static", resource)                                 

    try:                                                                         
        with open(resp_file, "r") as f:                                          
            resp_file = f.read()                                                 
    except Exception:                                                            
        start_response("404 Not Found", headers)                                 
        return ["404 Not Found"]                                                 

    start_response("200 OK", headers)                                            
    return [resp_file]                                                           

s = make_server("0.0.0.0", 8080, app)                                            
s.serve_forever()                                              

Comments

0

The Denis code almost SOLVED the problem for ME, but I needed to make some changes/fixes, and this is what I got working:

from wsgiref.simple_server import make_server
from cgi import parse_qs, escape

import codecs                                                                       
                                                                             
                                                                             
def tipoDeConteudo(path):
    if path.endswith(".css"):
        return "text/css"
    else:
        return "text/html"                                                       
                                                                             
                                                                             
def web_app(environment, response):                                                
    caminho = environment["PATH_INFO"]
    resource = caminho.split("/")[1]

    headers = []
    headers.append(("Content-Type", tipoDeConteudo(resource)))                   
                                                                             
    try:
        resp_file = codecs.open("indexResposta.html", 'r').read()
    except Exception:
        response("404 Not Found", headers)
        return ["404 Not Found"]                                                 
                                                                             
    response('200 OK', headers)
    return [resp_file.encode()]                                                        
                                                                             
s = make_server("0.0.0.0", 8080, app)                                            
s.serve_forever()                                              

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.