File size: 1,468 Bytes
fcb1da1 b4dd6fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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()
|