26
26
class HTTPServer :
27
27
"""A basic socket-based HTTP server."""
28
28
29
- def __init__ (self , socket_source : Protocol ) -> None :
29
+ def __init__ (self , socket_source : Protocol , root_path : str ) -> None :
30
30
"""Create a server, and get it ready to run.
31
31
32
32
:param socket: An object that is a source of sockets. This could be a `socketpool`
33
33
in CircuitPython or the `socket` module in CPython.
34
+ :param str root_path: Root directory to serve files from
34
35
"""
35
36
self ._buffer = bytearray (1024 )
36
37
self ._timeout = 1
37
38
self .routes = _HTTPRoutes ()
38
39
self ._socket_source = socket_source
39
40
self ._sock = None
40
- self .root_path = "/"
41
+ self .root_path = root_path
41
42
42
43
def route (self , path : str , method : HTTPMethod = HTTPMethod .GET ) -> Callable :
43
44
"""
@@ -63,32 +64,28 @@ def route_decorator(func: Callable) -> Callable:
63
64
64
65
return route_decorator
65
66
66
- def serve_forever (self , host : str , port : int = 80 , root_path : str = "" ) -> None :
67
+ def serve_forever (self , host : str , port : int = 80 ) -> None :
67
68
"""Wait for HTTP requests at the given host and port. Does not return.
68
69
69
70
:param str host: host name or IP address
70
71
:param int port: port
71
- :param str root_path: root directory to serve files from
72
72
"""
73
- self .start (host , port , root_path )
73
+ self .start (host , port )
74
74
75
75
while True :
76
76
try :
77
77
self .poll ()
78
78
except OSError :
79
79
continue
80
80
81
- def start (self , host : str , port : int = 80 , root_path : str = "" ) -> None :
81
+ def start (self , host : str , port : int = 80 ) -> None :
82
82
"""
83
83
Start the HTTP server at the given host and port. Requires calling
84
84
poll() in a while loop to handle incoming requests.
85
85
86
86
:param str host: host name or IP address
87
87
:param int port: port
88
- :param str root_path: root directory to serve files from
89
88
"""
90
- self .root_path = root_path
91
-
92
89
self ._sock = self ._socket_source .socket (
93
90
self ._socket_source .AF_INET , self ._socket_source .SOCK_STREAM
94
91
)
0 commit comments