3

I have created a small python html server, but I am having issues sending external css and javascript. The html transfers as it should and inline css works fine. The chrome developer tool responds with this error:

Resource interpreted as Stylesheet but transferred with MIME type text/plain: "http://localhost:8888/style.css".

Unfortunately I have no knowledge on what a "MIME type" is.

Here is the python code:

# server.py
import socket

file = open('website/index.html', 'r')

def start_server(HOST, PORT):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST, PORT))
    s.listen(1)

    print('Serving HTTP on port %s ...' % PORT)
    while True:
        client_connection, client_address = s.accept()
        request = client_connection.recv(1024)
        print(request.decode('utf-8'))

        http_response = """\
http/1.1 200 OK

""" + file.read() + """
"""
        client_connection.sendall(bytes(http_response, 'utf-8'))
        client_connection.close()
4
  • I can offer any extra information at request. Commented Aug 9, 2016 at 23:02
  • 2
    In addition to the answer below, you must send another header: Connection: close because you dont have persistent connections. Commented Aug 9, 2016 at 23:30
  • @SuperSaiyan: actually you don't need to send a Connection header in this case because the response is closed with the close of the underlying TCP connection. Bad style but valid. Commented Aug 10, 2016 at 2:37
  • Im just learning, thanks for the responses! Commented Aug 10, 2016 at 6:39

1 Answer 1

2

Add this line to your response string right beneath the 200 OK line:

Content-Type: text/css

What's happening is that Chrome is attempting to interpret the HTML you sent as as stylesheet, which you want. But, when you send it, you're sending with a content header that's telling chrome "I'm just plain text, nothing special here!" So Chrome is like, well something is wrong with that, I was expecting a stylesheet, and throws the error you see. If you tell Chrome that you're sending it a stylesheet, the error should be resolved.

This is from Mozilla rather than Chrome, but it gives a good overview of MIME types.

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

2 Comments

I'll check this when I get home. Thanks for the response.
This prints all the html code in a "pre" tag. Screenshot: puu.sh/qw4mq.png

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.