http_server¶
Simple HTTP server with authorization support for testing.
Serves files from the current (or given) folder over HTTP, requiring authorization - bearer or basic - for write operations (PUT/DELETE), while reads are served without authorization.
HandlerException
¶
Bases: Exception
Exception raised for HTTP handler errors with status code and message.
Handlers raise it - typically out of _check_authorization() - and the do_PUT()/do_DELETE() methods convert it into an HTTP error response.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
The HTTP status code. |
|
message |
The error message string. |
|
explain |
The explanation string. |
Source code in py_misc_utils/http_server.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
HTTPRequestHandler
¶
Bases: SimpleHTTPRequestHandler
HTTP request handler with authorization for write operations.
Extends SimpleHTTPRequestHandler to add bearer and basic authentication checking for PUT and DELETE operations. OPTIONS is supported for CORS preflight requests.
Source code in py_misc_utils/http_server.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
do_OPTIONS()
¶
Handle OPTIONS requests with CORS headers.
Responds with 200 OK and lists allowed methods - no authorization check is performed, letting clients probe the server capabilities.
Source code in py_misc_utils/http_server.py
173 174 175 176 177 178 179 180 181 182 | |
do_DELETE()
¶
Handle DELETE requests with authorization check.
Deletes the file at the requested path if authorized.
Example
import argparse
import base64
import functools
import http.server
import tempfile
import threading
import requests
from py_misc_utils import http_server as hs
# Serve a scratch folder, with authorization required for writes.
handler = hs.ClassWrapper(hs.HTTPRequestHandler,
_args=argparse.Namespace(auth='s3cret'))
td = tempfile.mkdtemp()
server = http.server.ThreadingHTTPServer(
('127.0.0.1', 0), functools.partial(handler, directory=td))
threading.Thread(target=server.serve_forever, daemon=True).start()
url = 'http://127.0.0.1:{}'.format(server.server_address[1])
# Writes need the authorization header - others are rejected.
token = base64.b64encode(b's3cret').decode()
resp = requests.put(url + '/hello.txt', data=b'hi there',
headers={'Authorization': 'Bearer ' + token})
assert resp.status_code == 201
# Reads are served without authorization.
assert requests.get(url + '/hello.txt').content == b'hi there'
server.shutdown()
server.server_close()
Source code in py_misc_utils/http_server.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
do_PUT()
¶
Handle PUT requests with authorization check.
Creates or overwrites the file at the requested path if authorized.
See do_DELETE() for a full client/server example.
Source code in py_misc_utils/http_server.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
ClassWrapper
¶
Wraps a class to inject constructor keyword arguments as attributes.
When called, creates a new instance with the wrapped class and updates its dict with the stored kwargs before calling init. Also intercepts setattr to store attribute assignments in _kwargs - so that later attribute assignments on the wrapper are seen by instances created afterwards.
PRIVATE_ATTRIBUTES: Set of attribute names that are not stored in _kwargs.
Example
from py_misc_utils import http_server as hs
class Greeting:
def __init__(self, name):
self.name = name
wrapped = hs.ClassWrapper(Greeting)
obj = wrapped('Ada')
assert obj.name == 'Ada'
# Attributes assigned on the wrapper are injected into later instances.
wrapped.timeout = 30
obj2 = wrapped('Grace')
assert obj2.timeout == 30 and obj2.name == 'Grace'
Source code in py_misc_utils/http_server.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |