473,320 Members | 1,845 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,320 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 3359
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.