Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to view files in a browser with python http server

I am creating a python httpserver for a folder on remote machine using command :

python -m SimpleHTTPServer 9999

But I am unable to view a file in the browser using this. As soon as click on the link the file gets downloaded. Is there a way to create a server so that i can view the files in my browser only.

like image 405
Nishant Lakhara Avatar asked Jun 02 '16 07:06

Nishant Lakhara


People also ask

How do I access the HTTP server in python?

Accessing the Python HTTP Server Locally To access the server, open a browsing window and enter http://localhost:PORT_NUMBER into the URL field. If a port number is not specified in the previous step, the server will be found at http://localhost:8000 . From here, users can open or download any of the hosted files.

What can you do with python HTTP server?

Python comes with a built-in module known as SimpleHTTPServer, which in other words is a simple HTTP server that gives you standard GET and HEAD request handlers. This module can turn any directory of your system into a web server.

Does python have HTTP server?

You can run python http server on any port, default port is 8000. Try to use port number greater than 1024 to avoid conflicts. Then open your favourite browser and type localhost:9000 .

How do I host a file in python?

Go to the directory whose file you want to share by using cd (change directory) command. Go to the directory with the file you want to share using cd on *nix or MacOS systems or CD for Windows. Start your HTTP server with either python -m SimpleHTTPServer or python3 -m http. server.


1 Answers

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)
like image 57
Håken Lid Avatar answered Sep 30 '22 18:09

Håken Lid