webdav / app.py
sigyllly's picture
Update app.py
b4dd6fc verified
raw
history blame
1.47 kB
import os
from wsgidav.server.server_thread import WsgiDAVApp
from wsgidav.dav_provider import DAVProvider
from wsgidav.request import Request
from wsgidav.response import Response
# Create a folder named 'files' and add a 'sample.txt' file if not already present
directory = 'files'
file_name = os.path.join(directory, 'sample.txt')
# Create the directory and file if they do not exist
if not os.path.exists(directory):
os.makedirs(directory)
if not os.path.exists(file_name):
with open(file_name, 'w') as f:
f.write("This is a sample text file.")
# WebDAV Provider Class
class MyDAVProvider(DAVProvider):
def __init__(self, base_path):
super().__init__(base_path)
# WebDAV App Configuration
def start_webdav_server():
# The base folder that the WebDAV server will expose
base_path = os.path.abspath(directory)
# Set up the WebDAV provider (which provides file management)
provider = MyDAVProvider(base_path)
# Server settings (no authentication required)
config = {
"host": "0.0.0.0", # Listen on all interfaces
"port": 7860, # Port number
"provider": provider, # File system provider
"authenticator": None, # Disable authentication
}
# Create the WSGI WebDAV app
app = WsgiDAVApp(config)
print(f"Starting WebDAV server on http://0.0.0.0:7860/")
# Start the WebDAV server
app.run()
if __name__ == "__main__":
start_webdav_server()