0
# Server Management
1
2
Development server and production server adapter support for various WSGI servers including built-in development server, and adapters for production servers like Gunicorn, uWSGI, and others.
3
4
## Capabilities
5
6
### Development Server
7
8
Built-in development server for testing and development.
9
10
```python { .api }
11
def run(app=None, server='wsgiref', host='127.0.0.1', port=8080, debug=False, reloader=False, interval=1, quiet=False, plugins=None, **kwargs):
12
"""
13
Start a development server.
14
15
Parameters:
16
- app: Bottle or WSGI application (uses default app if None)
17
- server: str, server adapter name ('wsgiref', 'cherrypy', 'waitress', etc.)
18
- host: str, server host address
19
- port: int, server port number
20
- debug: bool, enable debug mode with detailed error pages
21
- reloader: bool, enable auto-reload on file changes
22
- interval: int, file check interval for reloader (seconds)
23
- quiet: bool, suppress startup output
24
- plugins: list, plugins to install before starting
25
- **kwargs: additional server-specific options
26
"""
27
```
28
29
Usage:
30
31
```python
32
from bottle import Bottle, run
33
34
# Simple development server
35
app = Bottle()
36
37
@app.route('/')
38
def index():
39
return 'Hello World!'
40
41
# Basic server startup
42
if __name__ == '__main__':
43
run(app, host='localhost', port=8080)
44
45
# Development mode with auto-reload
46
if __name__ == '__main__':
47
run(app, host='localhost', port=8080, debug=True, reloader=True)
48
49
# Custom server adapter
50
if __name__ == '__main__':
51
run(app, server='waitress', host='0.0.0.0', port=8080)
52
```
53
54
### Server Adapters
55
56
Base server adapter class and built-in adapters for various WSGI servers.
57
58
```python { .api }
59
class ServerAdapter:
60
def __init__(self, host='127.0.0.1', port=8080, **options):
61
"""
62
Base server adapter.
63
64
Parameters:
65
- host: str, server host
66
- port: int, server port
67
- **options: server-specific options
68
"""
69
70
def run(self, handler):
71
"""
72
Start server with WSGI handler.
73
74
Parameters:
75
- handler: WSGI application callable
76
"""
77
```
78
79
### Built-in Server Adapters
80
81
#### WSGIRef Server (Default)
82
83
```python { .api }
84
class WSGIRefServer(ServerAdapter):
85
"""Built-in Python WSGI reference server (single-threaded, development only)."""
86
87
def run(self, handler):
88
"""Start WSGIRef server."""
89
```
90
91
#### Waitress Server
92
93
```python { .api }
94
class WaitressServer(ServerAdapter):
95
"""Waitress WSGI server (pure Python, production-ready)."""
96
97
def run(self, handler):
98
"""Start Waitress server."""
99
```
100
101
Usage:
102
103
```python
104
# Install: pip install waitress
105
run(app, server='waitress', host='0.0.0.0', port=8080)
106
```
107
108
#### CherryPy Server
109
110
```python { .api }
111
class CherryPyServer(ServerAdapter):
112
"""CherryPy WSGI server (deprecated, use Cheroot instead)."""
113
114
class CherootServer(ServerAdapter):
115
"""Cheroot WSGI server (CherryPy's server extracted)."""
116
117
def run(self, handler):
118
"""Start Cheroot server."""
119
```
120
121
Usage:
122
123
```python
124
# Install: pip install cheroot
125
run(app, server='cheroot', host='0.0.0.0', port=8080, threads=10)
126
```
127
128
#### Gunicorn Server
129
130
```python { .api }
131
class GunicornServer(ServerAdapter):
132
"""Gunicorn WSGI server (Unix only, production-ready)."""
133
134
def run(self, handler):
135
"""Start Gunicorn server."""
136
```
137
138
Usage:
139
140
```python
141
# Install: pip install gunicorn
142
run(app, server='gunicorn', host='0.0.0.0', port=8080,
143
workers=4, worker_class='sync')
144
```
145
146
#### Eventlet Server
147
148
```python { .api }
149
class EventletServer(ServerAdapter):
150
"""Eventlet WSGI server (async, green threads)."""
151
152
def run(self, handler):
153
"""Start Eventlet server."""
154
```
155
156
Usage:
157
158
```python
159
# Install: pip install eventlet
160
run(app, server='eventlet', host='0.0.0.0', port=8080,
161
workers=1000)
162
```
163
164
#### Gevent Server
165
166
```python { .api }
167
class GeventServer(ServerAdapter):
168
"""Gevent WSGI server (async, green threads)."""
169
170
def run(self, handler):
171
"""Start Gevent server."""
172
```
173
174
Usage:
175
176
```python
177
# Install: pip install gevent
178
run(app, server='gevent', host='0.0.0.0', port=8080)
179
```
180
181
#### Other Server Adapters
182
183
```python { .api }
184
class PasteServer(ServerAdapter):
185
"""Paste HTTP server."""
186
187
class MeinheldServer(ServerAdapter):
188
"""Meinheld WSGI server (C extension, high performance)."""
189
190
class TornadoServer(ServerAdapter):
191
"""Tornado WSGI server."""
192
193
class TwistedServer(ServerAdapter):
194
"""Twisted WSGI server."""
195
196
class BjoernServer(ServerAdapter):
197
"""Bjoern WSGI server (C extension, very fast)."""
198
199
class AiohttpServer(ServerAdapter):
200
"""Aiohttp WSGI server (asyncio-based)."""
201
202
class AutoServer(ServerAdapter):
203
"""Automatically select best available server adapter."""
204
```
205
206
### Server Configuration
207
208
Configure server-specific options for different adapters.
209
210
```python
211
# Waitress configuration
212
run(app, server='waitress',
213
host='0.0.0.0', port=8080,
214
threads=6, # Number of threads
215
connection_limit=100, # Max connections
216
cleanup_interval=30, # Cleanup interval
217
channel_timeout=120 # Channel timeout
218
)
219
220
# Gunicorn configuration
221
run(app, server='gunicorn',
222
host='0.0.0.0', port=8080,
223
workers=4, # Number of worker processes
224
worker_class='sync', # Worker class
225
worker_connections=1000, # Max connections per worker
226
max_requests=1000, # Restart workers after N requests
227
timeout=30, # Worker timeout
228
keepalive=2 # Keep-alive timeout
229
)
230
231
# Gevent configuration
232
run(app, server='gevent',
233
host='0.0.0.0', port=8080,
234
spawn=1000, # Max concurrent connections
235
backlog=2048 # Listen backlog
236
)
237
```
238
239
### Application Loading
240
241
Load applications from module strings for deployment.
242
243
```python { .api }
244
def load(target, **namespace):
245
"""
246
Load module, class, or function from string.
247
248
Parameters:
249
- target: str, import target (e.g., 'myapp:app' or 'mymodule.myapp')
250
- **namespace: additional namespace items
251
252
Returns:
253
any: loaded object
254
"""
255
256
def load_app(target):
257
"""
258
Load WSGI application from string.
259
260
Parameters:
261
- target: str, application target (e.g., 'myapp:application')
262
263
Returns:
264
WSGI application: loaded application
265
"""
266
```
267
268
Usage:
269
270
```python
271
# Load app from module
272
app = load_app('myproject.wsgi:application')
273
run(app)
274
275
# Load with custom namespace
276
config = {'debug': True}
277
app = load('myproject.app:create_app', config=config)
278
```
279
280
### Production Deployment
281
282
Deployment patterns for production environments.
283
284
#### WSGI File
285
286
Create WSGI file for deployment:
287
288
```python
289
# wsgi.py
290
from bottle import Bottle, route
291
292
app = Bottle()
293
294
@app.route('/')
295
def index():
296
return 'Hello Production!'
297
298
# For Gunicorn, uWSGI, etc.
299
application = app
300
301
if __name__ == '__main__':
302
app.run(host='0.0.0.0', port=8080)
303
```
304
305
#### Gunicorn Deployment
306
307
```bash
308
# Install Gunicorn
309
pip install gunicorn
310
311
# Run with Gunicorn
312
gunicorn -w 4 -b 0.0.0.0:8080 wsgi:application
313
314
# With configuration file
315
gunicorn -c gunicorn.conf.py wsgi:application
316
```
317
318
#### uWSGI Deployment
319
320
```bash
321
# Install uWSGI
322
pip install uwsgi
323
324
# Run with uWSGI
325
uwsgi --http :8080 --wsgi-file wsgi.py --callable application
326
327
# With ini file
328
uwsgi --ini uwsgi.ini
329
```
330
331
### Development Features
332
333
Features for development and debugging.
334
335
#### Auto-Reload
336
337
Monitor files for changes and restart server automatically:
338
339
```python
340
run(app, reloader=True, interval=1) # Check every second
341
```
342
343
#### Debug Mode
344
345
Enable detailed error pages and exception propagation:
346
347
```python
348
import bottle
349
350
bottle.debug(True) # Global debug mode
351
run(app, debug=True) # Server debug mode
352
```
353
354
#### File Monitoring
355
356
```python { .api }
357
class FileCheckerThread(threading.Thread):
358
"""Thread that monitors files for changes in reloader mode."""
359
360
def __init__(self, lockfile, interval):
361
"""
362
Create file checker thread.
363
364
Parameters:
365
- lockfile: str, lock file path
366
- interval: int, check interval in seconds
367
"""
368
369
def run(self):
370
"""Monitor files for changes."""
371
```
372
373
### Server Utilities
374
375
Utility functions for server management.
376
377
```python { .api }
378
def main():
379
"""Main CLI entry point for bottle command."""
380
381
class WSGIFileWrapper:
382
"""WSGI file wrapper for efficient file serving."""
383
384
def __init__(self, fp, buffer_size=8192):
385
"""
386
Create file wrapper.
387
388
Parameters:
389
- fp: file-like object
390
- buffer_size: int, read buffer size
391
"""
392
393
def __iter__(self):
394
"""Iterate over file chunks."""
395
396
def close(self):
397
"""Close file."""
398
```
399
400
## Command Line Interface
401
402
Bottle provides a command-line interface for running applications:
403
404
```bash
405
# Run bottle application
406
python -m bottle myapp:app
407
408
# Specify host and port
409
python -m bottle --bind 0.0.0.0:8080 myapp:app
410
411
# Choose server adapter
412
python -m bottle --server waitress myapp:app
413
414
# Enable debug and reload
415
python -m bottle --debug --reload myapp:app
416
417
# Load configuration
418
python -m bottle --conf config.ini myapp:app
419
420
# Install plugins
421
python -m bottle --plugin cors --plugin auth myapp:app
422
```