├── .gitignore
├── .vscode
└── settings.json
├── README.md
├── fileserver.py
├── illustrations
└── home.jpg
└── templates
└── home.html
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 | cover/
54 |
55 | # Translations
56 | *.mo
57 | *.pot
58 |
59 | # Django stuff:
60 | *.log
61 | local_settings.py
62 | db.sqlite3
63 | db.sqlite3-journal
64 |
65 | # Flask stuff:
66 | instance/
67 | .webassets-cache
68 |
69 | # Scrapy stuff:
70 | .scrapy
71 |
72 | # Sphinx documentation
73 | docs/_build/
74 |
75 | # PyBuilder
76 | .pybuilder/
77 | target/
78 |
79 | # Jupyter Notebook
80 | .ipynb_checkpoints
81 |
82 | # IPython
83 | profile_default/
84 | ipython_config.py
85 |
86 | # pyenv
87 | # For a library or package, you might want to ignore these files since the code is
88 | # intended to run in multiple environments; otherwise, check them in:
89 | # .python-version
90 |
91 | # pipenv
92 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
94 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
95 | # install all needed dependencies.
96 | #Pipfile.lock
97 |
98 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
99 | __pypackages__/
100 |
101 | # Celery stuff
102 | celerybeat-schedule
103 | celerybeat.pid
104 |
105 | # SageMath parsed files
106 | *.sage.py
107 |
108 | # Environments
109 | .env
110 | .venv
111 | env/
112 | venv/
113 | ENV/
114 | env.bak/
115 | venv.bak/
116 |
117 | # Spyder project settings
118 | .spyderproject
119 | .spyproject
120 |
121 | # Rope project settings
122 | .ropeproject
123 |
124 | # mkdocs documentation
125 | /site
126 |
127 | # mypy
128 | .mypy_cache/
129 | .dmypy.json
130 | dmypy.json
131 |
132 | # Pyre type checker
133 | .pyre/
134 |
135 | # pytype static type analyzer
136 | .pytype/
137 |
138 | # Cython debug symbols
139 | cython_debug/
140 |
141 | # static files generated from Django application using `collectstatic`
142 | media
143 | static
144 |
145 | # .vscode
146 | .vscode/*
147 | !.vscode/settings.json
148 | !.vscode/tasks.json
149 | !.vscode/launch.json
150 | !.vscode/extensions.json
151 | *.code-workspace
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "python.pythonPath": "C:\\Users\\JSGURU\\envs\\demo\\Scripts\\python.exe"
3 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Flask_File_Server
2 | A Flask File Server
3 |
4 | ## Install Dependencies
5 | ```sh
6 | > pip install flask
7 | ```
8 | ## Run a file server
9 | ```sh
10 | > python fileserver.py
11 | ```
12 | ## Change file storage path
13 | In the `fileserver.py`
14 | ```
15 | if __name__ == "__main__":
16 | dir_path = os.getcwd()
17 | storage_path = dir_path
18 | RunFileServer(storage_path, 8000)
19 | ```
20 | Replace `storage_path` to the absolute path of the folder you want to host files in.
21 | ## How the file server looks like.
22 |
--------------------------------------------------------------------------------
/fileserver.py:
--------------------------------------------------------------------------------
1 | """
2 | A Flask file server.
3 | """
4 | import logging
5 | import time
6 | import os
7 |
8 | import requests
9 | from flask import Flask, request, send_from_directory, render_template
10 |
11 | LOCALHOST = '127.0.0.1'
12 |
13 | logger = logging.getLogger(__name__)
14 |
15 |
16 | def RunFileServer(fileServerDir, fileServerPort):
17 | """
18 | Run a Flask file server on the given port.
19 | """
20 | app = Flask(__name__, instance_path=fileServerDir)
21 |
22 | @app.route('/', methods=['GET'])
23 | def Home(): # pylint: disable=unused-variable
24 |
25 | fileArr = []
26 | for root, dirs, files in os.walk(fileServerDir):
27 | for fileName in files:
28 | fileArr.append(root.replace(fileServerDir, "") + "\\" + fileName)
29 | return render_template("home.html", files=fileArr)
30 |
31 | @app.route('/fileserver-is-ready', methods=['GET'])
32 | def FileServerIsReady(): # pylint: disable=unused-variable
33 | """
34 | Used to test if file server has started.
35 | """
36 | return "The fileserver is ready."
37 |
38 | @app.route('/', methods=['GET'])
39 | def ServeFile(filename): # pylint: disable=unused-variable
40 | """
41 | Serves up a file from fileServerDir.
42 | """
43 | return send_from_directory(fileServerDir, filename.strip('/'))
44 |
45 | app.run(host=LOCALHOST, port=fileServerPort)
46 |
47 |
48 | def WaitForFileServerToStart(port):
49 | url = 'http://%s:%s/fileserver-is-ready' % (LOCALHOST, port)
50 | attempts = 0
51 | while True:
52 | try:
53 | attempts += 1
54 | requests.get(url, timeout=1)
55 | return True
56 | except requests.exceptions.ConnectionError:
57 | time.sleep(0.25)
58 | if attempts > 10:
59 | logger.warning("WaitForFileServerToStart: timeout")
60 | return
61 |
62 | if __name__ == "__main__":
63 | dir_path = os.getcwd()
64 | storage_path = dir_path
65 | RunFileServer(storage_path, 8000)
66 |
--------------------------------------------------------------------------------
/illustrations/home.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jsguru-git/Flask_File_Server/482cb500dd55341ac73105fbc55d30da08aebf57/illustrations/home.jpg
--------------------------------------------------------------------------------
/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | home
5 |
6 |
7 | File List
8 |
9 | {% if files %}
10 | {% for fileName in files %}
11 | - {{ fileName }}
12 | {% endfor %}
13 | {% endif %}
14 |
15 |
16 |
--------------------------------------------------------------------------------