Simple Webserver problem

C

Colin Brown

I am attempting to construct a simple webserver for browsing
text files with arbitrary file extensions based on the standard
Python library modules. Not all files are being returned as text.
[Python 2.3.2, Platform: Win2K, Browser IE6]. Code as below.

Any assistance appreciated.

Colin Brown
PyNZ
-------------------------------------------
# Usage: Browse to: http://localhost:8090
import os, sys, BaseHTTPServer, SimpleHTTPServer

PORT = 8090
DEFPATH = r'C:\logs'

class TextHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def guess_type(self, path):
return 'text/plain'

# run an HTTP Server on PORT based at DEFPATH
sys.argv = ['',PORT]
os.chdir(DEFPATH)
BaseHTTPServer.test(TextHandler,BaseHTTPServer.HTTPServer)
 
I

Irmen de Jong

Colin said:
I am attempting to construct a simple webserver for browsing
text files with arbitrary file extensions based on the standard
Python library modules. Not all files are being returned as text.
[Python 2.3.2, Platform: Win2K, Browser IE6]. Code as below.

Any assistance appreciated.
^^^ that's your guy

IE is known to act "smart" on downloaded files, and often
ignores the content type that the web server supplies.
If you try a better browser such as Firebird you'll see that
it works (I tried, every file is served as text/plain).

There is one issue though.
On windows there's a bug in SimpleHTTPServer:
if files are served as text/* , it opens the file with
mode 'r' instead of 'rb'. This breaks the content-length
that is reported in the headers, because reading a file
with mode 'r' may yield a string of different length than
the file on disk (on windows).
And the filesize on disk is what's reported.

Strictly speaking this should be fixed, but for your
purpose it seems to work just fine as it is.

--Irmen de Jong
 
E

Emile van Sebille

Colin Brown asks:
I am attempting to construct a simple webserver for browsing
text files with arbitrary file extensions based on the standard
Python library modules. Not all files are being returned as text.

IIRC, and assuming you're testing with IE, IE presumptively (of course)
outsmarts the text/plain header. If you try with netscape it works.

HTH,

Emile van Sebille
(e-mail address removed)
 
C

Colin Brown

Irmen de Jong said:
Colin said:
I am attempting to construct a simple webserver for browsing
text files with arbitrary file extensions based on the standard
Python library modules. Not all files are being returned as text.
[Python 2.3.2, Platform: Win2K, Browser IE6]. Code as below.

Any assistance appreciated.
^^^ that's your guy

IE is known to act "smart" on downloaded files, and often
ignores the content type that the web server supplies.
If you try a better browser such as Firebird you'll see that
it works (I tried, every file is served as text/plain).

Thanks. Why am I not surprised!
There is one issue though.
On windows there's a bug in SimpleHTTPServer:
if files are served as text/* , it opens the file with
mode 'r' instead of 'rb'. This breaks the content-length
that is reported in the headers, because reading a file
with mode 'r' may yield a string of different length than
the file on disk (on windows).
And the filesize on disk is what's reported.

I noted that in the code. Yes, my files would all have
been "text".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top