VB_
VB_

Reputation: 45692

python -m SimpleHttpServer [port] on OSX

I created simplest html page

<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>App</title>
    </head>
    <body>
        Hello, world.
    </body>
</html>

and run python -m SimpleHTTPServe 8000 in the page's folder.

Problem: When I go to localhost:8000 I see the text of my page. Means the http server instead of interpreting it as html page wraps each string of my page into <p> tag.

If I open sources I get:

<html> 
........
<body>
<p class="p1"><span class="s1">&lt;</span><b>html</b><span class="s2"> </span><span class="s3">lang</span><span class="s1">=</span><span class="s4">"en"</span><span class="s1">&gt;</span></p>
<p class="p2">    <span class="s1">&lt;</span><span class="s5"><b>head</b></span><span class="s1">&gt;</span></p>
<p class="p2">        <span class="s1">&lt;</span><span class="s5"><b>meta</b></span> <span class="s3">charset</span><span class="s1">=</span><span class="s4">"utf-8"</span><span class="s1">&gt;</span></p>
<p class="p3"><span class="s2">        </span>&lt;<span class="s5"><b>title</b></span>&gt;Backbone.js Todo App&lt;/<span class="s5"><b>title</b></span>&gt;</p>
<p class="p2">    <span class="s1">&lt;/</span><span class="s5"><b>head</b></span><span class="s1">&gt;</span></p>
<p class="p2">    <span class="s1">&lt;</span><span class="s5"><b>body</b></span><span class="s1">&gt;</span></p>
<p class="p3"><span class="s2">        </span>Hello, world.</p>
<p class="p3"><span class="Apple-converted-space">    </span>&lt;/<span class="s5"><b>body</b></span>&gt;</p>
<p class="p1"><span class="s1">&lt;/</span><b>html</b><span class="s1">&gt;</span></p>
</body>
</html>

Upvotes: 1

Views: 1258

Answers (3)

Ari Lotter
Ari Lotter

Reputation: 615

If you're editing your page in TextEdit, make sure you open the Format menu and choose "Make Plain Text."

Plain Text

If you saved as HTML without this option, TextEdit would have added those tags to keep your text file's formatting.

Upvotes: 1

user861537
user861537

Reputation:

If your page show up as text just as you are describing then that most likely means that the server does not use the correct content-type in its HTTP headers. SimpleHTTPServer uses the module mimetypes to determine what content-type to use for most file types.

You can check it yourself:

>>> import mimetypes
>>> mimetypes.init()
>>> print mimetypes.types_map
...

or more specifically

>>> print mimetypes.types_map['.html']
text/html

Make sure your file extension matches with what mimetypes expects.

Upvotes: 0

Bruce
Bruce

Reputation: 7132

I just tried on my mac and I don't observe similar behaviour: I start server slightly differently:

  • python -mSimpleHTTPServer 8000 : no space between m and SimpleHTTPServer (checked with space, same behaviour)
  • I have 2 files : a.out and a.html with same content : it looks like a.out gets downloaded while a.html gets displayed (with chrome)

My guess is that your file has no extension .html and that your browser itself is adding the tags to display it.

You can try downloading the file with curl to make sure : curl http://localhost:8000/a.out from a terminal

Note : I'm running MacOS 10.9.4 on a McBook air with python 2.7.5

Upvotes: 0

Related Questions