473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 3366
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: vincent wehren | last post by:
Hi, as a small capabilities demo I coded the piece below to show how to use Python for cgi'ing on localhost and it more or less does the trick :-). However, I when I freeze it with py2exe,...
2
by: Jed Parsons | last post by:
Hola - I'm curious about using SimpleHTTPServer as the web server on my home machine. It would pretty much just be hit by me, search robots, and automated attacks :) So, before I ditch...
2
by: Bryan Rasmussen | last post by:
Hey just doing some preliminary testing with SimpleHTTPServer, and i noticed that when i request a resource with a query string parameters that this was not handled. is this correct, or is there...
0
by: jack | last post by:
I am getting this error on my web server runnint ASP.NET on Windows 2003, it kills the serving web pages. The only way to get the web pages to start serving is to restart the server. I can't find...
3
by: Sanghyeon Seo | last post by:
I took some time to write this HOWTO: http://sparcs.kaist.ac.kr/~tinuviel/fepy/howto/simplehttpserver-ironpython-mono-howto.html IronPython seems to get much less interest than it deserves. This...
0
by: test | last post by:
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...
2
by: Ben Finney | last post by:
Howdy all, I'm trying to implement some new functionality for an existing PHP web application. Rather than writing a whole lot of stuff in PHP, and looking toward a future when more of the...
9
by: jbrewer | last post by:
I'm currently implementing an XML-RPC service in Python where binary data is sent to the server via URLs. However, some clients that need to access the server may not have access to a web server,...
10
by: Robert Huff | last post by:
Can someone offer suggestions why, on the same server (Apache 2.2.8), this works <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en-US"> <head> <link rel=stylesheet...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.