To make the browser open files inline instead of downloading them, you have to serve the files with the appropriate http Content
headers.
What makes the content load inline in the browser tab, instead of as a download, is the header Content-Disposition: inline
.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
To add these headers, you you can subclass the default SimpleHTTPRequestHandler
with a custom one.
This is how it can be done using python 3. You have to modify the imports and maybe some other parts if you have to use python 2.
Put it in a executable script file which you can call myserver.py
and run it like so: ./myserver.py 9999
#!/usr/bin/env python3
from http.server import SimpleHTTPRequestHandler, test
import argparse
class InlineHandler(SimpleHTTPRequestHandler):
def end_headers(self):
mimetype = self.guess_type(self.path)
is_file = not self.path.endswith('/')
# This part adds extra headers for some file types.
if is_file and mimetype in ['text/plain', 'application/octet-stream']:
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Disposition', 'inline')
super().end_headers()
# The following is based on the standard library implementation
# https://github.com/python/cpython/blob/3.6/Lib/http/server.py#L1195
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
parser.add_argument('port', action='store',
default=8000, type=int,
nargs='?',
help='Specify alternate port [default: 8000]')
args = parser.parse_args()
test(InlineHandler, port=args.port, bind=args.bind)
Content-Type
header of the HTTP response? – PhoenicianContent-Disposition
response header toinline
. You have to write your own HttpRequestHandler to do that. – Zimmer