Configuration¶
Warning
To delcare addr and port - you have to do it from the startup code:
import slimHTTP
http = slimHTTP.server(slimHTTP.HTTP, addr='127.0.0.1', port=8080)
http.run()
Trying to set it in the runtime configuration will fail, as the server has already setup the socket.bind((addr, port))
Note
All following config options are runtime friendly, they can be changed whenever during normal operation without needing to reload the server. The format for the configuration is a valid python dict:
{
'key-one' : 'value',
'key-two' : 'value'
}
Where the key is any of the below options, and the value is whatever corresponds to that particular key or option.
Example configuration¶
import slimHTTP
http = slimHTTP.server(slimHTTP.HTTP)
@http.configuration
def config(instance):
return {
'web_root' : './vhosts/default',
'index' : 'index.html',
'vhosts' : {
'hvornum.se' : {
'web_root' : './vhosts/hvornum.se',
'index' : 'index.html'
},
'slimhttp.hvornum.se' : {
'module' : './vhosts/internal_tests/vhost.py'
}
}
}
http.run()
Global configuration options¶
web_root¶
{'web_root' : './path'}
index¶
{'index' : ['index.html', 'main.py']}
Vhost specific configuration¶
vhosts¶
{ 'vhosts' : { 'slimhttp.hvornum.se' : { // config options for slimhttp.hvornum.se } } }
module¶
Note
{ 'vhosts' : { 'slimhttp.hvornum.se' : { 'module' : './vhosts/hvornum.se/vhost_slimhttp.py' } } }
Warning
if not 'MyMemStorage' in __builtins__: __builtins__['MyMemStorage'] = {}
if not 'counter' in MyMemStorage: MyMemStorage['counter'] = 0
print(f"The module ran with counter value {MyMemStorage['counter']}. Incremeting value!"")
MyMemStorage['counter'] += 1
Or you could use pickle.dumps or a database to store the data you need between sessions. Although they will be a bit slower considering they’re not working within the application memory space.
on_request¶
if the function on_request is defined (using `hasattr(‘on_request’, <module>)`), slimHTTP will automatically call it upon each request to that vhost.
Warning
if @app.route(‘/…’, vhost=’example.com’) is defined, that will take precedence over the on_request if on_request returns data. Otherwise the @app.route will be a fallback.
@app.route¶
import slimHTTP
http = slimHTTP.instances[':80']
@http.route('/', vhost='example.com')
def route_handler(request).
print(request)
Note
Note that the instance depends on the addr and port used, a “listening on every interface on port 80” would be :80 in this case.
proxy¶
HTTP_REQUEST
to a HTTP_PROXY_REQUEST
.Warning
The HTTP_REQUEST
object has two pitfalls. One, if the proxy is slow to respond all concurrent HTTP requests to slimHTTP will become slow, since we’re single threaded, it means that the proxy response has to be parsed in full before other requests can come in. The second pitfall being Issue #11.
{
'vhosts' : {
'internal.hvornum.se' : {
'proxy' : '192.168.10.10:80',
'ssl' : False
}
}
}
Note
The ‘ssl’ : False’ is optional and the default behavior.