localhost-server 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- localhost_server-0.1.0/LICENSE +21 -0
- localhost_server-0.1.0/PKG-INFO +73 -0
- localhost_server-0.1.0/README.md +55 -0
- localhost_server-0.1.0/pyproject.toml +29 -0
- localhost_server-0.1.0/setup.cfg +4 -0
- localhost_server-0.1.0/src/localhost/__init__.py +3 -0
- localhost_server-0.1.0/src/localhost/server.py +188 -0
- localhost_server-0.1.0/src/localhost_server.egg-info/PKG-INFO +73 -0
- localhost_server-0.1.0/src/localhost_server.egg-info/SOURCES.txt +10 -0
- localhost_server-0.1.0/src/localhost_server.egg-info/dependency_links.txt +1 -0
- localhost_server-0.1.0/src/localhost_server.egg-info/requires.txt +4 -0
- localhost_server-0.1.0/src/localhost_server.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: localhost-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Static file hosting library for local development
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/username/localhost
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: build; extra == "dev"
|
|
16
|
+
Requires-Dist: twine; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# localhost
|
|
20
|
+
|
|
21
|
+
Static file hosting library for local development.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install localhost
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from localhost import serve
|
|
33
|
+
|
|
34
|
+
serve("./website/", port=4488)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from localhost import serve, Options
|
|
41
|
+
|
|
42
|
+
server = serve(
|
|
43
|
+
"./website/",
|
|
44
|
+
port=4488,
|
|
45
|
+
options=Options(
|
|
46
|
+
index_file="demo.html",
|
|
47
|
+
enable_js=False,
|
|
48
|
+
auto_reload=True,
|
|
49
|
+
log_level="info",
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
- `serve(folder, port, options)` - Start hosting
|
|
57
|
+
- `stop()` - Stop the server
|
|
58
|
+
- `Options` - Configuration dataclass
|
|
59
|
+
|
|
60
|
+
## Error Handling
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from localhost import serve, ErrFolderNotFound, ErrIndexNotFound, ErrPortInUse
|
|
64
|
+
|
|
65
|
+
try:
|
|
66
|
+
server = serve("./website/", port=4488)
|
|
67
|
+
except ErrFolderNotFound:
|
|
68
|
+
print("Folder does not exist")
|
|
69
|
+
except ErrIndexNotFound:
|
|
70
|
+
print("No index file found")
|
|
71
|
+
except ErrPortInUse:
|
|
72
|
+
print("Port is already in use")
|
|
73
|
+
```
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# localhost
|
|
2
|
+
|
|
3
|
+
Static file hosting library for local development.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install localhost
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from localhost import serve
|
|
15
|
+
|
|
16
|
+
serve("./website/", port=4488)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from localhost import serve, Options
|
|
23
|
+
|
|
24
|
+
server = serve(
|
|
25
|
+
"./website/",
|
|
26
|
+
port=4488,
|
|
27
|
+
options=Options(
|
|
28
|
+
index_file="demo.html",
|
|
29
|
+
enable_js=False,
|
|
30
|
+
auto_reload=True,
|
|
31
|
+
log_level="info",
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## API
|
|
37
|
+
|
|
38
|
+
- `serve(folder, port, options)` - Start hosting
|
|
39
|
+
- `stop()` - Stop the server
|
|
40
|
+
- `Options` - Configuration dataclass
|
|
41
|
+
|
|
42
|
+
## Error Handling
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from localhost import serve, ErrFolderNotFound, ErrIndexNotFound, ErrPortInUse
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
server = serve("./website/", port=4488)
|
|
49
|
+
except ErrFolderNotFound:
|
|
50
|
+
print("Folder does not exist")
|
|
51
|
+
except ErrIndexNotFound:
|
|
52
|
+
print("No index file found")
|
|
53
|
+
except ErrPortInUse:
|
|
54
|
+
print("Port is already in use")
|
|
55
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "localhost-server"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Static file hosting library for local development"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "Your Name", email = "you@example.com"}
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.10"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/username/localhost"
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
dev = ["build", "twine"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
where = ["src"]
|
|
29
|
+
include = ["localhost*"]
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Optional
|
|
4
|
+
import http.server
|
|
5
|
+
import socketserver
|
|
6
|
+
import os
|
|
7
|
+
import signal
|
|
8
|
+
import sys
|
|
9
|
+
import threading
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LogLevel(Enum):
|
|
13
|
+
NONE = 0
|
|
14
|
+
INFO = 1
|
|
15
|
+
DEBUG = 2
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class LocalhostError(Exception):
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ErrFolderNotFound(LocalhostError):
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ErrIndexNotFound(LocalhostError):
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ErrPortInUse(LocalhostError):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class Options:
|
|
36
|
+
index_file: str = "index.html"
|
|
37
|
+
enable_js: bool = True
|
|
38
|
+
auto_reload: bool = False
|
|
39
|
+
log_prefix: str = "[localhost]"
|
|
40
|
+
log_level: str = "info"
|
|
41
|
+
cert_file: Optional[str] = None
|
|
42
|
+
key_file: Optional[str] = None
|
|
43
|
+
|
|
44
|
+
def __post_init__(self):
|
|
45
|
+
if isinstance(self.log_level, str):
|
|
46
|
+
self.log_level = {
|
|
47
|
+
"none": LogLevel.NONE,
|
|
48
|
+
"info": LogLevel.INFO,
|
|
49
|
+
"debug": LogLevel.DEBUG,
|
|
50
|
+
}.get(self.log_level.lower(), LogLevel.INFO)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
_server_instance = None
|
|
54
|
+
_server_shutdown = threading.Event()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _log(prefix: str, message: str, level, min_level):
|
|
58
|
+
if level.value >= min_level.value:
|
|
59
|
+
print(f"{prefix} {message}")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class FileWatcher:
|
|
63
|
+
def __init__(self, folder: str, callback, log_prefix: str, log_level):
|
|
64
|
+
self.folder = folder
|
|
65
|
+
self.callback = callback
|
|
66
|
+
self.log_prefix = log_prefix
|
|
67
|
+
self.log_level = log_level
|
|
68
|
+
self._running = False
|
|
69
|
+
self._thread = None
|
|
70
|
+
self._mtimes = {}
|
|
71
|
+
|
|
72
|
+
def _get_mtime(self, filepath):
|
|
73
|
+
try:
|
|
74
|
+
return os.path.getmtime(filepath)
|
|
75
|
+
except OSError:
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
def _scan_files(self):
|
|
79
|
+
for root, dirs, files in os.walk(self.folder):
|
|
80
|
+
for f in files:
|
|
81
|
+
if f.endswith(('.html', '.css', '.js')):
|
|
82
|
+
path = os.path.join(root, f)
|
|
83
|
+
self._mtimes[path] = self._get_mtime(path)
|
|
84
|
+
|
|
85
|
+
def _check_changes(self):
|
|
86
|
+
self._scan_files()
|
|
87
|
+
while self._running:
|
|
88
|
+
for path, old_mtime in list(self._mtimes.items()):
|
|
89
|
+
new_mtime = self._get_mtime(path)
|
|
90
|
+
if new_mtime is not None and new_mtime != old_mtime:
|
|
91
|
+
self._mtimes[path] = new_mtime
|
|
92
|
+
rel_path = os.path.relpath(path, self.folder)
|
|
93
|
+
_log(self.log_prefix, f"File changed: {rel_path}", self.log_level, LogLevel.INFO)
|
|
94
|
+
self.callback()
|
|
95
|
+
break
|
|
96
|
+
threading.Event().wait(0.5)
|
|
97
|
+
|
|
98
|
+
def start(self):
|
|
99
|
+
self._running = True
|
|
100
|
+
self._thread = threading.Thread(target=self._check_changes, daemon=True)
|
|
101
|
+
self._thread.start()
|
|
102
|
+
|
|
103
|
+
def stop(self):
|
|
104
|
+
self._running = False
|
|
105
|
+
if self._thread:
|
|
106
|
+
self._thread.join(timeout=1)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def serve(
|
|
110
|
+
folder: str = "./",
|
|
111
|
+
port: int = 8080,
|
|
112
|
+
options: Optional[Options] = None,
|
|
113
|
+
):
|
|
114
|
+
global _server_instance
|
|
115
|
+
|
|
116
|
+
if options is None:
|
|
117
|
+
options = Options()
|
|
118
|
+
|
|
119
|
+
if not os.path.isdir(folder):
|
|
120
|
+
raise ErrFolderNotFound(f"Folder not found: {folder}")
|
|
121
|
+
|
|
122
|
+
index_path = os.path.join(folder, options.index_file)
|
|
123
|
+
if not os.path.isfile(index_path):
|
|
124
|
+
raise ErrIndexNotFound(f"No index file found in {folder}")
|
|
125
|
+
|
|
126
|
+
class Handler(http.server.SimpleHTTPRequestHandler):
|
|
127
|
+
def __init__(self, *args, **kwargs):
|
|
128
|
+
super().__init__(*args, directory=folder, **kwargs)
|
|
129
|
+
|
|
130
|
+
def log_message(self, format, *args):
|
|
131
|
+
pass
|
|
132
|
+
|
|
133
|
+
watcher = None
|
|
134
|
+
httpd = None
|
|
135
|
+
|
|
136
|
+
def signal_handler(sig, frame):
|
|
137
|
+
_server_shutdown.set()
|
|
138
|
+
if httpd:
|
|
139
|
+
httpd.shutdown()
|
|
140
|
+
|
|
141
|
+
try:
|
|
142
|
+
if options.auto_reload:
|
|
143
|
+
watcher = FileWatcher(folder, lambda: None, options.log_prefix, options.log_level)
|
|
144
|
+
watcher.start()
|
|
145
|
+
|
|
146
|
+
old_sigint = signal.signal(signal.SIGINT, signal_handler)
|
|
147
|
+
old_sigterm = signal.signal(signal.SIGTERM, signal_handler)
|
|
148
|
+
|
|
149
|
+
socketserver.TCPServer.allow_reuse_address = True
|
|
150
|
+
socketserver.TCPServer.daemon_threads = True
|
|
151
|
+
httpd = socketserver.TCPServer(("", port), Handler)
|
|
152
|
+
_server_instance = httpd
|
|
153
|
+
|
|
154
|
+
scheme = "https" if options.cert_file else "http"
|
|
155
|
+
_log(options.log_prefix, f"Started hosting on port {port}.", options.log_level, LogLevel.INFO)
|
|
156
|
+
_log(options.log_prefix, f"Serving files from \"{folder}\".", options.log_level, LogLevel.INFO)
|
|
157
|
+
_log(options.log_prefix, f"Go to {scheme}://localhost:{port} in your browser.", options.log_level, LogLevel.INFO)
|
|
158
|
+
_log(options.log_prefix, "Press Ctrl+C to stop.", options.log_level, LogLevel.INFO)
|
|
159
|
+
|
|
160
|
+
if options.auto_reload:
|
|
161
|
+
_log(options.log_prefix, "Auto-reload enabled. Watching for file changes ...", options.log_level, LogLevel.INFO)
|
|
162
|
+
|
|
163
|
+
serve_thread = threading.Thread(target=httpd.serve_forever, kwargs={"poll_interval": 0.1}, daemon=True)
|
|
164
|
+
serve_thread.start()
|
|
165
|
+
|
|
166
|
+
while not _server_shutdown.is_set():
|
|
167
|
+
threading.Event().wait(0.1)
|
|
168
|
+
|
|
169
|
+
serve_thread.join(timeout=1)
|
|
170
|
+
except OSError as e:
|
|
171
|
+
if e.errno == 98 or e.errno == 48:
|
|
172
|
+
raise ErrPortInUse(f"Port {port} is already in use") from e
|
|
173
|
+
raise
|
|
174
|
+
finally:
|
|
175
|
+
signal.signal(signal.SIGINT, old_sigint)
|
|
176
|
+
signal.signal(signal.SIGTERM, old_sigterm)
|
|
177
|
+
if watcher:
|
|
178
|
+
watcher.stop()
|
|
179
|
+
_server_instance = None
|
|
180
|
+
_server_shutdown.clear()
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def stop():
|
|
184
|
+
global _server_instance
|
|
185
|
+
_server_shutdown.set()
|
|
186
|
+
if _server_instance:
|
|
187
|
+
_server_instance.shutdown()
|
|
188
|
+
_server_instance = None
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: localhost-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Static file hosting library for local development
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/username/localhost
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: build; extra == "dev"
|
|
16
|
+
Requires-Dist: twine; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# localhost
|
|
20
|
+
|
|
21
|
+
Static file hosting library for local development.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install localhost
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from localhost import serve
|
|
33
|
+
|
|
34
|
+
serve("./website/", port=4488)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Options
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from localhost import serve, Options
|
|
41
|
+
|
|
42
|
+
server = serve(
|
|
43
|
+
"./website/",
|
|
44
|
+
port=4488,
|
|
45
|
+
options=Options(
|
|
46
|
+
index_file="demo.html",
|
|
47
|
+
enable_js=False,
|
|
48
|
+
auto_reload=True,
|
|
49
|
+
log_level="info",
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
- `serve(folder, port, options)` - Start hosting
|
|
57
|
+
- `stop()` - Stop the server
|
|
58
|
+
- `Options` - Configuration dataclass
|
|
59
|
+
|
|
60
|
+
## Error Handling
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from localhost import serve, ErrFolderNotFound, ErrIndexNotFound, ErrPortInUse
|
|
64
|
+
|
|
65
|
+
try:
|
|
66
|
+
server = serve("./website/", port=4488)
|
|
67
|
+
except ErrFolderNotFound:
|
|
68
|
+
print("Folder does not exist")
|
|
69
|
+
except ErrIndexNotFound:
|
|
70
|
+
print("No index file found")
|
|
71
|
+
except ErrPortInUse:
|
|
72
|
+
print("Port is already in use")
|
|
73
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/localhost/__init__.py
|
|
5
|
+
src/localhost/server.py
|
|
6
|
+
src/localhost_server.egg-info/PKG-INFO
|
|
7
|
+
src/localhost_server.egg-info/SOURCES.txt
|
|
8
|
+
src/localhost_server.egg-info/dependency_links.txt
|
|
9
|
+
src/localhost_server.egg-info/requires.txt
|
|
10
|
+
src/localhost_server.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
localhost
|