Discussion:
open an html string in a browser?
Kevin Altis
2003-01-18 20:20:21 UTC
Permalink
I don't think you have any choice but to create an actual file for the
browser to read if you want to use the webbrowser module; the browser will
know nothing of zip files. If you want to use the actual name of the file,
but use the temporary directory on your system, make a call to
tempfile.mktemp() and then you can use the tempfile.tempdir variable for
creating the files. Something like:

import tempfile
tempfile.mktemp()
path = os.path.join(tempfile.tempdir, 'myfile.html')

Then just write the file contents before you call webbrowser with the path
you created.

http://www.python.org/doc/lib/module-tempfile.html

Note that the documenation is a bit out-of-date for Python 2.2.2, the docs
for 2.3a1 seem to better reflect what the module actually does:

http://www.python.org/doc/2.3a1/lib/module-tempfile.html

As an alternative to using the webbrowser module, you could use your own GUI
to display the HTML in which case you don't have to write out a temporary
file. wxPython has a wxHtmlWindow control which is cross-platform as well as
wrappers for the Windows IE control, which of course only works on Windows.
PythonCard has samples for using both.

http://pythoncard.sourceforge.net/samples/simpleBrowser.html

http://pythoncard.sourceforge.net/samples/simpleIEBrowser.html

Since it is possible to set the page by providing both a URL or text, it is
possible to read the file from the zip and then display that content
directly without using an intermediate file. I just created a zip containing
an HTML file to test this with the simpleIEBrowser sample, run with the -s
import zipfile
f = zipfile.ZipFile('index-in-zip.zip')
f.namelist()
['index-in-zip.html']
data = f.read('index-in-zip.html')
f.close()
comp.htmlDisplay.text = data
The last line is referencing the html component and its text attribute and
works as expected.

Finally, if you are on Windows, then you could also access the browser
directly using win32com (part of Mark Hammond's win32all package) and set
its content directly, but I don't have an example of that offhand.

ka

"Lance" <lbrannma at cablespeed.com> wrote in message
Hi All,
I have a zip file containing perhaps 800 html files. I want to use Python
to
open one of these files in a browser.
Suppose the filename is myfile.html.
I've tried webbrowser and zipfile. These don't work.
The reason is that zipfile.read('myfile.html') returns a string,
containing
the contents of the file. However, Webbrowser requires a URL (in this case
the name of an html file).
I don't want to save the string from zipfile.read to disk.
Any suggestions will be appreciated.
Thanks,
Lance
Mike Meyer
2003-01-19 15:58:59 UTC
Permalink
Post by Kevin Altis
"Lance" <lbrannma at cablespeed.com> wrote in message
It would be nice if browsers would be prepared to read their standard input,
so you could pipe the content into it, but offhand I can't think of one that
does.
Some operating systems provide a name for standard input in the file
system name space. /dev/fd/0 and /dev/stdin are the two I've seen. If
the OP is on such an OS, that would solve the problem.

<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dennis Reinhardt
2003-01-18 21:47:04 UTC
Permalink
The reason is that zipfile.read('myfile.html') returns a string,
containing
the contents of the file. However, Webbrowser requires a URL (in this case
the name of an html file).
You *could* implement a very simple http server. the server returns the
string when requested by browser. The following command (among others)
launches browser:
os.startfile("http://localhost")

You would need port 80 open locally or attach to some other port and
identify same in URL..
--

Dennis Reinhardt

http://www.dair.com
piter
2003-01-19 00:12:51 UTC
Permalink
Hi,

I already did it.
I created it during learning Java and since Java documentation as a zip file
was ~20MB but unpacked more than 110 MB i decided to write that simple http
server in order to preserve my precious disk space ;-)

To my suprise it worked quite fast (even with frames, gifs and all this
stuff)

-------- CODE STARTS --------
'''
# The zip file http server
# example URL:
# http://localhost:8000/d:/java/doc/j2sdk-131-docs.zip/docs/guide/index.html
# where:
# - http://localhost:8000 - points to host where this script is running
# - d:/java/doc/j2sdk-131-docs.zip - points to zip file
# - docs/guide/index.html - points to file to display
#
# you can also skip file to display:
# http://localhost:8000/d:/java/doc/j2sdk-131-docs.zip/
# and in this case you can browse entire zip file
#
# or you can browse your entire filesystem:
# http://localhost:8000/d:/
# but I am pretty sure there are much *BETTER* ways of doinig it...
#
'''

import base64, BaseHTTPServer, SimpleHTTPServer, os, os.path, re, socket,
sys, urllib, zipfile
PORT = 8000

class zipHTTPRequestHandler( SimpleHTTPServer.SimpleHTTPRequestHandler ):
def listDir( self, dir, namelist ):
dirLen = len( dir )
dirs, files = {}, {}
for name in namelist:
if name.startswith( dir ):
paths = name[dirLen:].split( '/' )
if len( paths ) > 1:
dirs[paths[0]] = 1
elif paths[0] != '':
files[paths[0]] = 1
dirs[ '..' ] = 1
dirs = dirs.keys()
dirs.sort()
files = files.keys()
files.sort()
return dirs, files

def sendOK( self ):
self.send_response( 200, 'OK' )
self.end_headers()

def printDir( self, path, dirs, files ):
self.sendOK()
self.wfile.write( '''<html>
<head>
<style TYPE="text/css">
<!--
a {text-decoration: none}
.a1:hover {background-color: darkRed; color: white}
.a2:hover {background-color: darkBlue; color: white}
h2 {padding: 4pt; background-color: darkBlue; color: white}
b.darkBlue {color: darkBlue}
-->
</style>
</head>
<body>
''' )
self.wfile.write( '<h2>Zip file browser</h2>\n' )
for file in ['index.html', 'index.htm']:
if file in files:
self.wfile.write( 'Found <a class=a1 href="%s">&nbsp %s
&nbsp</a> in this directory<br>\n' % (file, file) )
self.wfile.write( '<p>\nIndex of [<b
class=darkBlue>%s</b>]</p>\n<table>\n' % path )
for dir in dirs:
self.wfile.write( ' <tr><td><img src="/icon::dir.gif"
align=top>&nbsp<a class=a2 href="%s/">&nbsp %s &nbsp</a></td></tr>\n' %
(dir, dir) )
for file in files:
if file.endswith( '.zip' ):
icon = 'zip'
suffix = '/'
elif file.endswith( '.html' ) or file.endswith( '.htm' ):
icon = 'html'
suffix = ''
else:
icon = 'file'
suffix = ''
self.wfile.write( ' <tr><td><img src="/icon::%s.gif"
align=top>&nbsp<a class=a2 href="%s%s">&nbsp %s &nbsp</a></td></tr>\n' %
(icon, file, suffix, file) )
self.wfile.write( '</table>\n\n</body>\n</html>' )

def do_GET( self ):
self.path = urllib.unquote_plus( self.path )
if self.path in ['/icon::dir.gif', '/icon::file.gif',
'/icon::zip.gif', '/icon::html.gif']:
# OK this is quite easy...
self.sendOK()
self.wfile.write( self.icons[self.path] )
return
if sys.platform.startswith( 'win' ):
path = self.path[1:] # for windoze remove the leading slash
else:
path = self.path # else: (eunuch) everything is OK
r = re.match( '(.*\.zip)/(.*)', path )
if r:
# yes, yes, yes! we have zip file!
file = r.group( 1 )
path = r.group( 2 )
try:
zf = self.zFile[file]
except KeyError:
try:
zf = self.zFile[file] = zipfile.ZipFile( file, 'r' )
except:
self.send_error( 404, 'Cannot open zip file [%s]' %
file )
if path == '' or path.endswith( '/' ):
# request is directory in zip file
dirs, files = self.listDir( path, zf.namelist() )
self.printDir( '%s::%s' % (file, path), dirs, files )
else:
# request is normal file in zip file
try:
data = zf.read( path )
self.sendOK()
self.wfile.write( data )
except:
self.send_error( 404, 'Cannot open [%s::%s]' % (file,
path) )
else:
# ordinary file/dir
if os.path.isdir( path ):
if path[-1] != '/':
self.send_response( 301, 'Moved Permanently' )
self.send_header( 'Location', self.path + '/' )
self.end_headers()
return
try:
list = os.listdir( path )
except OSError, s:
self.send_error( 403, 'Forbidden: %s' % s )
return
dirs = []
files = []
for item in list:
if os.path.isdir( path + '/' + item ):
dirs += [ item ]
else:
files += [ item ]
if os.path.normpath( os.path.join(path, '..') ) != path:
dirs += [ '..' ]
dirs.sort()
files.sort()
self.printDir( path, dirs, files )
elif os.path.isfile( path ):
self.sendOK()
self.wfile.write( open(path).read() )
else:
self.send_error( 404, 'File [%s] not found' % path )

print __doc__
zipHTTPRequestHandler.zFile = {}
zipHTTPRequestHandler.icons = {}
zipLoading Image...'] = base64.decodestring( '''\
R0lGODlhEAAQAPUpAAAAABoaGikpKTk5OQA8WkVFRVhYWGlpaXR0dAtVhA5ekQhnmQdxoxBtoBF3
qBJ/sShsjSdokgeGtROAshaayxmv7jiixTy57VenxlawzV2x0HS71J2dnYaltKioqLi4uILD2JHJ
28G+vsfHx9nY2MHh7dPt9ujo6Pb29gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEBACkALAAAAAAQABAAAAbmwFQq
lQIUUqlUKpVKpVKpVCoVGJ0KhUEqlUqlUqlUarAgIDyjUwFQQABSqVRKUDJtFgSEZ3Q6nYCAVCoV
uFg2JROIQUB4RqdCKiWoSCyaDahkAjEICEMqFaA0HhK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PChsXAuNDAquTQgffRhvLXleXMp+LPs+7eq+jTtP7gv87Ozt3d3ercxurgyOji
2fDmwfLo2Orq6vPt4/vx6f39/QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5
BAEBAGEALAAAAAAQABAAAAf+gGFhYWEPDw8PDw8PDw9hYWFhYWFhD1dXV1dXQw86D2FhYWFhYQ9X
YGBgYF0RYDoPYWFhYQAXBhA5YGBgEVZgOgNhYRkvNz4pFwZGYDgREQ0DYRlFW1tLNSMVBENgXUI4
A2EUWF9TTjIdGAYgV19aQgMUJFheTzYeIhgJBEpdXIBHAwcuJ1A6PCwhGAkEQl5aRwMHKS4oNh4e
HxYFBEFaWEcDYRQsLSo2MQ8GBBJBWFg/A2EEHhocMS8MBAQvSlhUPwNhYQQGFA4NCAUvQElUUjQD
YWFhYQQEBA86QElSVFE0A2FhYWEPSkFBSUhHPz8zMwNhYWFhDwMDAwMDAwMGgAMDAwOBADs=
''' )

httpd = BaseHTTPServer.HTTPServer( ('', PORT), zipHTTPRequestHandler )

urlInfo = ( socket.gethostname(), PORT )
print 'Host [%s] started listening on port [%d]' % urlInfo
print 'Try e.g. http://%s:%d/pathToYourFile(s)' % urlInfo

httpd.serve_forever()
-------- CODE ENDS --------

Piter

U?ytkownik "Dennis Reinhardt" <DennisR at dair.com> napisa? w wiadomo?ci
Post by Kevin Altis
The reason is that zipfile.read('myfile.html') returns a string,
containing
the contents of the file. However, Webbrowser requires a URL (in this case
the name of an html file).
You *could* implement a very simple http server. the server returns the
string when requested by browser. The following command (among others)
os.startfile("http://localhost")
You would need port 80 open locally or attach to some other port and
identify same in URL..
--
Dennis Reinhardt
http://www.dair.com
Steve Holden
2003-01-18 20:50:33 UTC
Permalink
"Lance" <lbrannma at cablespeed.com> wrote in message
Hi All,
I have a zip file containing perhaps 800 html files. I want to use Python
to
open one of these files in a browser.
Suppose the filename is myfile.html.
I've tried webbrowser and zipfile. These don't work.
Yes they do. They just don't do what you want them to, that's all <wink>.
The reason is that zipfile.read('myfile.html') returns a string,
containing
the contents of the file. However, Webbrowser requires a URL (in this case
the name of an html file).
A good analysis of the problem.
I don't want to save the string from zipfile.read to disk.
Ah, so *this* is the *real* problem. You don't want to do what works, you
want to *define* "works" as not needing to write the zip component contents
to disk where the browser couls get at it.
Any suggestions will be appreciated.
Well, I'm not sure that's true, either. But, leaving that aside (for the
moment), I might ask how you would *like* it to work, given that the
webbrowser utility is driving whatever program you have installed as your
default browser. What would you have it do to let the browser know that it's
supposed to look inside a zip file for the content you want it to display?
It would be nice if browsers would be prepared to read their standard input,
so you could pipe the content into it, but offhand I can't think of one that
does.

There are other possibilities, of course, but they involve a good deal of
programming. For example, you woulc write a program with a graphical user
interface (wxWindows and Tkinter both have HTML widgets, IIRC - others I'm
less familiar with probably do too) that allows you to type in the name of a
zip file with the component name somehow also specified, and have it extract
the correct string from the zipfile and write it directly into the HTML
widget.

So, my suggestion would be that you get involved in the standards-making
process for the world-wide web and promote a "zip://" URL scheme that allows
reference to individual files inside a zip archive. Once you've defined the
"zip://" namespace so everyone can agree how it should work it will only be
a matter of waiting for five years or so, and browsers will be able to
handle it universally.

but-you-might-have-guessed-it's-not-an-entirely-serious-suggestion-ly
'rs - steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/
Bring your musical instrument to PyCon! http://www.python.org/pycon/
Lance
2003-01-18 18:54:27 UTC
Permalink
Hi All,

I have a zip file containing perhaps 800 html files. I want to use Python to
open one of these files in a browser.
Suppose the filename is myfile.html.

I've tried webbrowser and zipfile. These don't work.

The reason is that zipfile.read('myfile.html') returns a string, containing
the contents of the file. However, Webbrowser requires a URL (in this case
the name of an html file).

I don't want to save the string from zipfile.read to disk.

Any suggestions will be appreciated.

Thanks,
Lance

Continue reading on narkive:
Loading...