471,073 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Problem - Serving web pages on the desktop (SimpleHTTPServer)

Ben
Hi there,

Perhaps someone can help me. For some reason, when my Python script runs
and loads an HTML page in a new browser window at the local host
(desktop), the links to my stylesheet and all the images are broken. I
did check the HTML file by itself...everything loaded fine ;)

Here's my script:
--------------------
# File: webbrowser-test.py

import webbrowser, SimpleHTTPServer
from StringIO import StringIO

f=open('testpage.html', 'rb')
myPage = f.read()

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPReques tHandler):
def send_head(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
return StringIO(myPage)
webbrowser.open("http://127.0.0.1:8000", new=0, autoraise=1)
SimpleHTTPServer.test(MyRequestHandler)

--------------------

Here's my sample directory:
---------------------------------
webbrowser-test.py
testpage.html
m_files/
|_____stylesheet.css
|_____logo.gif
--------------------------------

Thanks for having a look. My next step is to process form input using
AJAX. I'll post working snippets of code here as I progress.

Ben
May 10 '06 #1
1 3182
Ben wrote:

Perhaps someone can help me. For some reason, when my Python script runs
and loads an HTML page in a new browser window at the local host
(desktop), the links to my stylesheet and all the images are broken. I
did check the HTML file by itself...everything loaded fine ;)
I've just had a brief look at the SimpleHTTPServer documentation, and
whilst I may have misunderstood what is required from the admittedly
dry prose, I think I can make a few suggestions.
Here's my script:
--------------------
# File: webbrowser-test.py

import webbrowser, SimpleHTTPServer
from StringIO import StringIO

f=open('testpage.html', 'rb')
myPage = f.read()
Here, you're reading the page to serve it up later, but why aren't you
letting the handler fetch the page from the file? Surely, that's what
the handler does in this case.
class MyRequestHandler(SimpleHTTPServer.SimpleHTTPReques tHandler):
def send_head(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
return StringIO(myPage)
Here, you're serving up the page. However, send_head appears not to be
part of the public interface to the handler, so it looks like you're
overriding some fairly fundamental behaviour of the handler which stops
any other file from being served as well. Perhaps you should remove
this method.
webbrowser.open("http://127.0.0.1:8000", new=0, autoraise=1)
SimpleHTTPServer.test(MyRequestHandler)

--------------------

Here's my sample directory:
---------------------------------
webbrowser-test.py
testpage.html
m_files/
|_____stylesheet.css
|_____logo.gif
--------------------------------
With testpage.html in the current directory, without the send_head
method, you should still get the page served up. However, you need to
make sure that the files in the subdirectory are referenced properly in
your page: something like "m_files/logo.gif" should cause the logo to
be fetched.
Thanks for having a look. My next step is to process form input using
AJAX. I'll post working snippets of code here as I progress.


Good luck!

Paul

May 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by vincent wehren | last post: by
2 posts views Thread by Jed Parsons | last post: by
2 posts views Thread by Bryan Rasmussen | last post: by
3 posts views Thread by Sanghyeon Seo | last post: by
2 posts views Thread by Ben Finney | last post: by
9 posts views Thread by jbrewer | last post: by
10 posts views Thread by Robert Huff | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.