Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Flask REST API
#1
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__':
    app.run(debug=True)
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?
Reply
#2
if __name__ == '__main__':
    app.run(debug=True, host=host, port=port)
where host is the ip of your interface, eg. 192.168.0.2 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  REST API using flask - limit connection? korenron 1 1,265 Feb-05-2023, 06:48 PM
Last Post: noisefloor
  Flask Rest API - fire and forget MorganSamage 2 1,588 Feb-04-2023, 11:09 AM
Last Post: MorganSamage
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 4,007 Aug-12-2020, 07:24 PM
Last Post: Alpy
  Simple flask rest api problem cancerboi 4 2,825 Jan-29-2020, 03:10 PM
Last Post: brighteningeyes
  Python Rest API Public/Secret Keys Authentication Nonce dn237 1 2,915 Oct-31-2019, 02:07 AM
Last Post: dn237
  How to Host a REST API that executes a PYTHON script on the WEB SERVER rahul4data 4 3,468 Jan-25-2019, 02:54 PM
Last Post: DeaD_EyE
  Flask rest api How to retrieve json request raysefo 4 6,110 Jan-20-2019, 06:46 PM
Last Post: raysefo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020