Python Forum

Full Version: Python Flask REST API
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I'm trying to create a simple REST API application that would show the contents of some folder on the server.

import os
from flask import Flask, jsonify

app = Flask(__name__)
files_in_dirs = []

@app.route('/api/listdir', methods=['GET'])
def list_dir():
    path = '/home/user/media'
    for dirname, dirnames, filenames in os.walk(path):
        # print path to all filenames
        for filename in filenames:
            files_in_dirs.append(os.path.join(dirname, filename))

    return jsonify({'Files on the server: ': files_in_dirs})

if __name__ == '__main__':
I run my server through the console, then go to the browser, write the address http://localhost:5000/api/listdir and everything works well. But how to make it so that any user on the Internet can see the contents of this folder and not only the localhost? What address should I enter in browser from other computer? Is this possible with Python and Flask?
if __name__ == '__main__':, host=host, port=port)
where host is the ip of your interface, eg. and port is the port number (default is 5000)

check ifconfig (linux) for the ip address to use (ipconfig for windows)

You should also make sure that the port is not firewalled. If you want to use a port number < 1024 than you need to be root.