### waitress-serve command line examples Source: https://docs.pylonsproject.org/projects/waitress/en/latest/usage.html Command line runner examples: listen on both IPv4 and IPv6 on port 8041, or listen on only IPv4 on port 8041. ```bash # Listen on both IPv4 and IPv6 on port 8041 waitress-serve --listen=*:8041 myapp:wsgifunc # Listen on only IPv4 on port 8041 waitress-serve --port=8041 myapp:wsgifunc ``` -------------------------------- ### --listen option example Source: https://docs.pylonsproject.org/projects/waitress/en/latest/runner.html Example of the --listen option used multiple times to listen on multiple sockets. A wildcard hostname binds to both IPv4 and IPv6 depending on their enabled state. ```text --listen=127.0.0.1:8080 --listen=[::1]:8080 --listen=*:8080 ``` -------------------------------- ### waitress-serve --listen examples Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/runner.rst.txt Examples of the --listen option for waitress-serve. It can be used multiple times to listen on multiple sockets, and a wildcard hostname binds to both IPv4/IPv6 depending on their enabled state. ```shell --listen=127.0.0.1:8080 --listen=[::1]:8080 --listen=*:8080 ``` -------------------------------- ### Waitress: Use url_prefix Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt Start Waitress with a URL prefix '/foo' so downstream applications that respect SCRIPT_NAME generate URLs with this prefix. ```python from waitress import serve serve(wsgiapp, listen='0.0.0.0:8080', url_prefix='/foo') ``` -------------------------------- ### listen argument examples Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/arguments.rst.txt Shows how to specify the listen argument as a quoted, space-delimited list of host:port combinations. A wildcard for the hostname binds to both IPv4/IPv6 depending on their enabled state. IPv6 addresses must be surrounded by brackets. ```python listen="127.0.0.1:8080 [::1]:8080" listen="*:8080 *:6543" ``` -------------------------------- ### waitress-serve command with hyphens Source: https://docs.pylonsproject.org/projects/waitress/en/latest/runner.html Equivalent command-line invocation using hyphens for the same arguments as the Python example above. ```bash waitress-serve --port=8041 --url-scheme=https myapp:wsgifunc ``` -------------------------------- ### Wrap app with TransLogger in Python Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Equivalent to the PasteDeploy pipeline: wrap your app in a TransLogger instance and serve it with Waitress. Disables the automatic console handler setup because logging handlers are already configured. ```python from mypackage import wsgiapp from waitress import serve from paste.translogger import TransLogger serve(TransLogger(wsgiapp, setup_console_handler=False)) ``` -------------------------------- ### waitress.serve(app, listen='0.0.0.0:8080', unix_socket=None, unix_socket_perms='600', threads=4, url_scheme='http', url_prefix='', ident='waitress', backlog=1024, recv_bytes=8192, send_bytes=1, outbuf_overflow=104856, outbuf_high_watermark=16777216, inbuf_overflow=52488, connection_limit=1000, cleanup_interval=30, channel_timeout=120, log_socket_errors=True, max_request_header_size=262144, max_request_body_size=1073741824, expose_tracebacks=False) Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/api.rst.txt The serve function is the main entry point for running a WSGI application with Waitress. It starts the server and blocks until the server is shut down. The function accepts an application object and various configuration parameters to control the server's behavior. ```APIDOC ## waitress.serve(app, listen='0.0.0.0:8080', unix_socket=None, unix_socket_perms='600', threads=4, url_scheme='http', url_prefix='', ident='waitress', backlog=1024, recv_bytes=8192, send_bytes=1, outbuf_overflow=104856, outbuf_high_watermark=16777216, inbuf_overflow=52488, connection_limit=1000, cleanup_interval=30, channel_timeout=120, log_socket_errors=True, max_request_header_size=262144, max_request_body_size=1073741824, expose_tracebacks=False) ### Description Starts a Waitress WSGI server to serve the given WSGI application. This function blocks until the server is shut down. ### Parameters - **app** (WSGI application) - Required - The WSGI application to serve. - **listen** (str) - Optional - The host:port to listen on. Default: '0.0.0.0:8080'. - **unix_socket** (str) - Optional - Path to a Unix socket to listen on. Default: None. - **unix_socket_perms** (str) - Optional - Permissions for the Unix socket. Default: '600'. - **threads** (int) - Optional - Number of threads to use. Default: 4. - **url_scheme** (str) - Optional - The URL scheme to use. Default: 'http'. - **url_prefix** (str) - Optional - A URL prefix to use. Default: ''. - **ident** (str) - Optional - The server identification string. Default: 'waitress'. - **backlog** (int) - Optional - The backlog for the listening socket. Default: 1024. - **recv_bytes** (int) - Optional - The number of bytes to receive at a time. Default: 8192. - **send_bytes** (int) - Optional - The number of bytes to send at a time. Default: 1. - **outbuf_overflow** (int) - Optional - The output buffer overflow size. Default: 104856. - **outbuf_high_watermark** (int) - Optional - The output buffer high watermark. Default: 16777216. - **inbuf_overflow** (int) - Optional - The input buffer overflow size. Default: 52488. - **connection_limit** (int) - Optional - The maximum number of simultaneous connections. Default: 1000. - **cleanup_interval** (int) - Optional - The interval for cleanup tasks. Default: 30. - **channel_timeout** (int) - Optional - The timeout for channels. Default: 120. - **log_socket_errors** (bool) - Optional - Whether to log socket errors. Default: True. - **max_request_header_size** (int) - Optional - The maximum size of request headers. Default: 262144. - **max_request_body_size** (int) - Optional - The maximum size of the request body. Default: 1073741824. - **expose_tracebacks** (bool) - Optional - Whether to expose tracebacks in error responses. Default: False. ### Returns None. The function blocks until the server is shut down. ``` -------------------------------- ### Start waitress with two pre-bound sockets using waitress.serve(sockets=...) Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/socket-activation.rst.txt Creates two INET stream sockets, binds them to 127.0.0.1 on ports 8080 and 9090, and passes them to waitress.serve() via the 'sockets' parameter. Sockets are closed after serve() returns. This is the pattern for manual socket activation; init systems provide bound sockets that are passed directly to serve(). ```python import socket import waitress def app(environ, start_response): content_length = environ.get('CONTENT_LENGTH', None) if content_length is not None: content_length = int(content_length) body = environ['wsgi.input'].read(content_length) content_length = str(len(body)) start_response( '200 OK', [('Content-Length', content_length), ('Content-Type', 'text/plain')] ) return [body] if __name__ == '__main__': sockets = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM), socket.socket(socket.AF_INET, socket.SOCK_STREAM)] sockets[0].bind(('127.0.0.1', 8080)) sockets[1].bind(('127.0.0.1', 9090)) waitress.serve(app, sockets=sockets) for socket in sockets: socket.close() ``` -------------------------------- ### Equivalent waitress.serve and waitress-serve invocations Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/runner.rst.txt Shows the equivalent ways to start a WSGI application with waitress: the Python function call and two waitress-serve command-line invocations. The first uses the waitress.serve function with underscore arguments, while the command-line versions use hyphens and either a positional MODULE:OBJECT or the --app flag. ```python import myapp waitress.serve(myapp.wsgifunc, port=8041, url_scheme='https') ``` ```shell waitress-serve --port=8041 --url-scheme=https myapp:wsgifunc ``` ```shell waitress-serve --port=8041 --url-scheme=https --app=myapp:wsgifunc ``` -------------------------------- ### WSGI application using environ['wsgi.file_wrapper'] Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/filewrapper.rst.txt Example WSGI application that returns a file wrapper for serving a file. The wrapper is accessed via environ['wsgi.file_wrapper'] and must be returned as the app_iter. The file-like object must support read() with optional size hint and return bytes; block_size is optional (default 32768). ```python import os here = os.path.dirname(os.path.abspath(__file__)) def myapp(environ, start_response): f = open(os.path.join(here, 'myphoto.jpg'), 'rb') headers = [('Content-Type', 'image/jpeg')] start_response( '200 OK', headers ) return environ['wsgi.file_wrapper'](f, 32768) ``` -------------------------------- ### Serve with multiple listen sockets in Waitress 1.0.0 Source: https://docs.pylonsproject.org/projects/waitress/en/latest/index.html Waitress 1.0.0 supports listening on multiple sockets, including IPv4 and IPv6, by passing a space-delineated list to the listen parameter. The example binds to IPv4 (0.0.0.0:8080), IPv6 ([::]:9090), and all interfaces (*:6543). ```python from waitress import serve serve(wsgiapp, listen='0.0.0.0:8080 [::]:9090 *:6543') ``` -------------------------------- ### Console output with TransLogger filter Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Example console output when requesting a page with the TransLogger filter in place. Shows the wsgi logger propagating messages to the root logger and the console. ```text 00:50:53,694 INFO [wsgiapp] Returning: Hello World! (content-type: text/plain) 00:50:53,695 INFO [wsgi] 192.168.1.111 - - [11/Aug/2011:20:09:33 -0700] "GET /hello HTTP/1.1" 404 - "-" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" ``` -------------------------------- ### Configure TransLogger with PasteDeploy pipeline Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Configure a WSGI pipeline with TransLogger and your app using PasteDeploy .ini syntax. The first block shows the existing app and server configuration; the second adds the translogger filter and pipeline, disabling the console handler setup. ```ini [app:wsgiapp] use = egg:mypackage#wsgiapp [server:main] use = egg:waitress#main host = 127.0.0.1 port = 8080 ``` ```ini [filter:translogger] use = egg:Paste#translogger setup_console_handler = False [pipeline:main] pipeline = translogger wsgiapp ``` -------------------------------- ### Waitress: Use https url_scheme Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt Start Waitress with the https url scheme, so all generated URLs use https. Useful when the proxy accepts HTTPS and the backend does not need to detect the scheme per request. ```python from waitress import serve serve(wsgiapp, listen='0.0.0.0:8080', url_scheme='https') ``` -------------------------------- ### Base PasteDeploy Configuration for WSGI App and Waitress Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/logging.rst.txt Base PasteDeploy configuration with a WSGI application and Waitress server. This is the starting point for adding a TransLogger filter to log web traffic. ```ini [app:wsgiapp] use = egg:mypackage#wsgiapp [server:main] use = egg:waitress#main host = 127.0.0.1 port = 8080 ``` -------------------------------- ### Serve a WSGI app with url_prefix Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Sets the `url_prefix` to '/foo' when serving a WSGI application. This causes the WSGI `SCRIPT_NAME` to be '/foo' (minus trailing slashes) and strips the prefix from `PATH_INFO` for requests that start with it. ```python from waitress import serve serve(wsgiapp, listen='0.0.0.0:8080', url_prefix='/foo') ``` -------------------------------- ### Configure Waitress logger level with Python logging API Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Sets the logging level for the 'waitress' logger to INFO using the Python logging module. This must be done before the server starts to take effect. ```python import logging logger = logging.getLogger('waitress') logger.setLevel(logging.INFO) ``` -------------------------------- ### serve() Source: https://docs.pylonsproject.org/projects/waitress/en/latest/api.html Documentation for the serve() function, the main entry point for running a WSGI application with Waitress. ```APIDOC ## serve(app, listen='0.0.0.0:8080', unix_socket=None, unix_socket_perms='600', threads=4, url_scheme='http', url_prefix='', ident='waitress', backlog=1024, recv_bytes=8192, send_bytes=1, outbuf_overflow=104856, outbuf_high_watermark=16777216, inbuf_overflow=52488, connection_limit=1000, cleanup_interval=30, channel_timeout=120, log_socket_errors=True, max_request_header_size=262144, max_request_body_size=1073741824, expose_tracebacks=False) ### Description Serves the given WSGI application using the Waitress WSGI server. This is the primary entry point for running a WSGI application with Waitress. ### Method Function call ### Endpoint serve() ### Parameters - **app** (WSGI application) - Required - The WSGI application to serve. - **listen** (string) - Optional - The host:port to listen on. Default: '0.0.0.0:8080'. - **unix_socket** (string) - Optional - Path to a Unix socket to listen on. Default: None. - **unix_socket_perms** (string) - Optional - Permissions for the Unix socket. Default: '600'. - **threads** (int) - Optional - Number of threads to use. Default: 4. - **url_scheme** (string) - Optional - The URL scheme to use. Default: 'http'. - **url_prefix** (string) - Optional - URL prefix for the application. Default: ''. - **ident** (string) - Optional - Server identity string. Default: 'waitress'. - **backlog** (int) - Optional - Backlog size for the listening socket. Default: 1024. - **recv_bytes** (int) - Optional - Number of bytes to receive at a time. Default: 8192. - **send_bytes** (int) - Optional - Number of bytes to send at a time. Default: 1. - **outbuf_overflow** (int) - Optional - Overflow size for output buffer. Default: 104856. - **outbuf_high_watermark** (int) - Optional - High watermark for output buffer. Default: 16777216. - **inbuf_overflow** (int) - Optional - Overflow size for input buffer. Default: 52488. - **connection_limit** (int) - Optional - Maximum number of simultaneous connections. Default: 1000. - **cleanup_interval** (int) - Optional - Interval for cleanup tasks. Default: 30. - **channel_timeout** (int) - Optional - Timeout for channels. Default: 120. - **log_socket_errors** (bool) - Optional - Whether to log socket errors. Default: True. - **max_request_header_size** (int) - Optional - Maximum size of request headers. Default: 262144. - **max_request_body_size** (int) - Optional - Maximum size of request body. Default: 1073741824. - **expose_tracebacks** (bool) - Optional - Whether to expose tracebacks in error responses. Default: False. ### Request Example No request body is applicable for this function call. ### Response #### Success Response No return value is documented. The function starts the server and blocks. #### Response Example No response body is applicable. ``` -------------------------------- ### Using --app flag to specify application Source: https://docs.pylonsproject.org/projects/waitress/en/latest/runner.html Shows how to use the --app flag to explicitly specify the application object. ```bash waitress-serve --port=8041 --url-scheme=https --app=myapp:wsgifunc ``` -------------------------------- ### PasteDeploy configuration with listen Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Entry point for PasteDeploy (egg:waitress#main) that lets you use Waitress's WSGI gateway from a configuration file. ```ini [server:main] use = egg:waitress#main listen = 127.0.0.1:8080 ``` -------------------------------- ### waitress-serve usage syntax Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/runner.rst.txt Usage line for the waitress-serve command. Shows the general syntax with optional options and a positional MODULE:OBJECT argument. ```shell waitress-serve [OPTS] [MODULE:OBJECT] ``` -------------------------------- ### PasteDeploy configuration with host and port Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Using host and port is also supported in PasteDeploy configuration. ```ini [server:main] host = 127.0.0.1 port = 8080 ``` -------------------------------- ### Python equivalent of waitress-serve command Source: https://docs.pylonsproject.org/projects/waitress/en/latest/runner.html Shows the Python code using waitress.serve with underscores, equivalent to the waitress-serve command with hyphens. ```python import myapp waitress.serve(myapp.wsgifunc, port=8041, url_scheme='https') ``` -------------------------------- ### waitress.serve Arguments Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/arguments.rst.txt Configuration arguments for the waitress.serve function, usable directly in Python code or PasteDeploy configuration. ```APIDOC ## waitress.serve Arguments ### Description These are the arguments that can be passed to the `waitress.serve` function or used in PasteDeploy configuration. They control the server's listening behavior, socket options, threading, and proxy trust settings. ### Arguments #### host - **Type**: string - **Default**: `0.0.0.0` - **Description**: Hostname or IP address on which to listen. Means "all IP addresses on this host". - **Warning**: May not be used with `listen`. #### port - **Type**: integer - **Default**: `8080` - **Description**: TCP port on which to listen. - **Warning**: May not be used with `listen`. #### listen - **Type**: string (quoted, space-delimited list of `host:port` combinations) - **Default**: None - **Description**: Tell waitress to listen on combinations of `host:port` arguments. Wildcard for hostname binds to both IPv4/IPv6 depending on enablement. IPv6 addresses must be surrounded by brackets. - **Example**: `listen="127.0.0.1:8080 [::1]:8080"` or `listen="*:8080 *:6543"` - **Version added**: 1.0 #### server_name - **Type**: string - **Default**: `waitress.invalid` - **Description**: Value placed in the WSGI environment as `SERVER_NAME`. Used only when the client sends an HTTP/1.0 request without a `Host` header and no proxy headers are present. In most situations you will not need to set this. - **Version added**: 2.0 #### ipv4 - **Type**: boolean - **Default**: None - **Description**: Enable or disable IPv4. #### ipv6 - **Type**: boolean - **Default**: None - **Description**: Enable or disable IPv6. #### unix_socket - **Type**: string (path) - **Default**: `None` - **Description**: Path of Unix socket. If specified, a Unix domain socket is made instead of the usual inet domain socket. Not available on Windows. #### unix_socket_perms - **Type**: string (octal permissions) - **Default**: `'600'` - **Description**: Octal permissions to use for the Unix domain socket. Only used if `unix_socket` is not `None`. #### sockets - **Type**: list of sockets - **Default**: `[]` - **Description**: A list of sockets (Internet or UNIX) that must be bound. Internet and UNIX sockets cannot be mixed. If the list is not empty, waitress creates one server for each socket. - **Version added**: 1.1.1 - **Warning**: May not be used with `listen`, `host`, `port`, or `unix_socket`. #### threads - **Type**: integer - **Default**: `4` - **Description**: The number of threads used to process application logic. #### trusted_proxy - **Type**: string (IP address or `*`) - **Default**: `None` - **Description**: IP address of a remote peer allowed to override various WSGI environment variables using proxy headers. For unix sockets, set to `localhost`. The wildcard `*` trusts all remote peers, which is a security risk if connections come from untrusted locations. #### trusted_proxy_count - **Type**: integer - **Default**: `1` - **Description**: How many proxies we trust when chained. For example, `X-Forwarded-For: 192.0.2.1, "[2001:db8::1]"` means there were potentially two proxies involved. If only one proxy is valid, the initial IP is not trusted and ignored. - **Version added**: 1.2.0 #### trusted_proxy_headers - **Type**: set of strings - **Default**: `x-forwarded-proto` (if `trusted_proxy` is set) - **Description**: Which proxy headers to trust. Can be "forwarded" or one or more of "x-forwarded-host", "x-forwarded-for", "x-forwarded-proto", "x-forwarded-port", "x-forwarded-by". Used when `trusted_proxy` is set to modify the WSGI environment. - **Version added**: 1.2.0 - **Warnings**: - If `trusted_proxy` is set, the default is `x-forwarded-proto` to match older versions. Users should explicitly opt-in as future versions will use an empty default. - It is an error to set this value without setting `trusted_proxy`. #### log_untrusted_proxy_headers - **Type**: boolean - **Default**: None - **Description**: Should waitress log warning messages about proxy headers that are being sent from upstream that are not trusted by `trusted_proxy_headers` but are being cleared due to `clear_untrusted_proxy_headers`? ``` -------------------------------- ### Clone Waitress repository Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/index.rst.txt Use this command to clone the Waitress repository from GitHub to check out the trunk version. ```text git clone git@github.com:Pylons/waitress.git ``` -------------------------------- ### Configure trusted_proxy_headers for X-Forwarded-* Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Set trusted_proxy_headers to include the X-Forwarded-* headers that Waitress should trust. ```text trusted_proxy_headers = "x-forwarded-for x-forwarded-host x-forwarded-proto x-forwarded-port" ``` -------------------------------- ### Serve via UNIX domain socket Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Serves your application through a UNIX domain socket to serve a downstream HTTP server/proxy such as nginx, lighttpd, and so on. This configuration won't work on Windows. ```python from waitress import serve serve(wsgiapp, unix_socket='/path/to/unix.sock') ``` -------------------------------- ### Configure accesslog formatter and FileHandler in INI Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/logging.rst.txt These two INI configuration blocks are used together to set up a custom 'accesslog' formatter and wire it into a FileHandler for the 'wsgi' logger. The first block defines the formatter with a pass-through format, and the second block creates the handler that uses it. The 'wsgi' logger must have 'propagate' set to 0 to direct records only to this handler. ```ini [formatters] keys = generic, accesslog [formatter_accesslog] format = %(message)s ``` ```ini [handler_accesslog] class = FileHandler args = ('%(here)s/access.log','a') level = INFO formatter = accesslog ``` -------------------------------- ### waitress-serve --call to invoke a WSGI factory Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/runner.rst.txt Uses the --call flag to load the myapp.mymodule module and call app.wsgi_factory to obtain a WSGI application function. This is for frameworks like web.py that have factory methods returning usable WSGI functions. ```shell waitress-serve --call myapp.mymodule.app.wsgi_factory ``` -------------------------------- ### Use accesslog formatter in FileHandler Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Wire the accesslog formatter into the FileHandler by replacing the formatter setting with accesslog. ```ini [handler_accesslog] class = FileHandler args = ('%(here)s/access.log','a') level = INFO formatter = accesslog ``` -------------------------------- ### Nginx: Forward X-Forwarded-* headers Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt Set the X-Forwarded-* headers in nginx so Waitress can build the correct WSGI environment from the original request. ```nginx proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Port $server_port; ``` -------------------------------- ### Serve on all IP addresses (IPv4 and IPv6) Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Runs Waitress on port 8080 on all available IP addresses, both IPv4 and IPv6. Press Ctrl-C (or Ctrl-Break on Windows) to exit. ```python from waitress import serve serve(wsgiapp, listen='*:8080') ``` -------------------------------- ### PasteDeploy configuration for UNIX domain socket Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt The PasteDeploy syntax for UNIX domain sockets is analogous. ```ini [server:main] use = egg:waitress#main unix_socket = /path/to/unix.sock ``` -------------------------------- ### Return wsgi.file_wrapper from a WSGI app Source: https://docs.pylonsproject.org/projects/waitress/en/latest/filewrapper.html WSGI application that returns a file wrapper via environ['wsgi.file_wrapper'] to serve a JPEG file. The wrapper constructor takes (filelike_object, block_size) as positional arguments; the result must be returned as the app_iter. The file-like object must support read() returning bytes and ideally seek(), tell(), and close(). ```python import os here = os.path.dirname(os.path.abspath(__file__)) def myapp(environ, start_response): f = open(os.path.join(here, 'myphoto.jpg'), 'rb') headers = [('Content-Type', 'image/jpeg')] start_response( '200 OK', headers ) return environ['wsgi.file_wrapper'](f, 32768) ``` -------------------------------- ### Configure FileHandler for wsgi logger Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Add a FileHandler named accesslog and configure the wsgi logger to use it. Setting propagate to 0 disables propagation to the root logger, so records go only to the accesslog handler. ```ini # Begin logging configuration [loggers] keys = root, wsgiapp, wsgi [handlers] keys = console, accesslog [logger_wsgi] level = INFO handlers = accesslog qualname = wsgi propagate = 0 [handler_accesslog] class = FileHandler args = ('%(here)s/access.log','a') level = INFO formatter = generic ``` -------------------------------- ### Heroku Procfile configuration Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Waitress can be used to serve WSGI apps on Heroku. Include waitress in requirements.txt and update the Procfile as shown. The proxy config trusts forwarding headers set by the Heroku load balancer and allows setting WEB_CONCURRENCY. Note that requests should not take longer than 30 seconds. ```bash web: waitress-serve \ --listen "*:$PORT" \ --trusted-proxy '*' \ --trusted-proxy-headers 'x-forwarded-for x-forwarded-proto x-forwarded-port' \ --log-untrusted-proxy-headers \ --clear-untrusted-proxy-headers \ --threads ${WEB_CONCURRENCY:-4} \ myapp:wsgifunc ``` -------------------------------- ### Waitress: Trusted proxy headers configuration Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt Set trusted_proxy_headers to the X-Forwarded-* headers so Waitress can update the WSGI environment accordingly. ```ini trusted_proxy_headers = "x-forwarded-for x-forwarded-host x-forwarded-proto x-forwarded-port" ``` -------------------------------- ### Configure TransLogger to Log to File with FileHandler Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/logging.rst.txt PasteDeploy logging configuration that adds a FileHandler named 'accesslog' and configures the 'wsgi' logger to use it. This directs TransLogger output to an access.log file instead of the console. ```ini # Begin logging configuration [loggers] keys = root, wsgiapp, wsgi [handlers] keys = console, accesslog [logger_wsgi] level = INFO handlers = accesslog qualname = wsgi propagate = 0 [handler_accesslog] class = FileHandler args = ('%(here)s/access.log','a') level = INFO formatter = generic ``` -------------------------------- ### WSGI environment variables set by Waitress Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Lists the WSGI environment variables that Waitress sets based on the trusted proxy headers. ```text HTTP_HOST SERVER_NAME SERVER_PORT REMOTE_ADDR REMOTE_PORT (if available) wsgi.url_scheme ``` -------------------------------- ### Serve on IPv4 only Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Runs Waitress on port 8080 on all available IPv4 addresses, but not IPv6. ```python from waitress import serve serve(wsgiapp, host='0.0.0.0', port=8080) ``` -------------------------------- ### Nginx proxy headers for Waitress Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Configure Nginx to forward X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Port headers to Waitress. ```nginx proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Port $server_port; ``` -------------------------------- ### Add accesslog formatter Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Add a pass-through formatter named accesslog that outputs only the log message, since TransLogger already provides all needed information. ```ini [formatters] keys = generic, accesslog [formatter_accesslog] format = %(message)s ``` -------------------------------- ### Serve with default host and port Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt By default Waitress binds to any IPv4 address on port 8080. You can omit the host and port arguments and just call serve with the WSGI app as a single argument. ```python from waitress import serve serve(wsgiapp) ``` -------------------------------- ### Configure Waitress logger level in PasteDeploy INI file Source: https://docs.pylonsproject.org/projects/waitress/en/latest/logging.html Sets the logging level for the 'waitress' logger to INFO in a PasteDeploy configuration file using the standard Python logging INI format. ```ini [logger_waitress] level = INFO ``` -------------------------------- ### Boolean flag and its negative equivalent for waitress-serve Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/runner.rst.txt Boolean flags for waitress-serve. Using --expose-tracebacks is equivalent to passing expose_tracebacks=True to waitress.serve, while --no-expose-tracebacks is equivalent to expose_tracebacks=False. All flags have a negative equivalent prefixed with no-. ```shell --expose-tracebacks ``` ```shell --no-expose-tracebacks ``` -------------------------------- ### waitress-serve command line runner Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/usage.rst.txt Command line runner called waitress-serve, which can be used in development and in situations where PasteDeploy is not necessary. ```bash # Listen on both IPv4 and IPv6 on port 8041 waitress-serve --listen=*:8041 myapp:wsgifunc # Listen on only IPv4 on port 8041 waitress-serve --port=8041 myapp:wsgifunc ``` -------------------------------- ### Waitress: Use Forwarded header Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt Configure Waitress to use the RFC7239 Forwarded header instead of X-Forwarded-* (mutually exclusive). ```ini trusted_proxy_headers = "forwarded" ``` -------------------------------- ### Negative flag --no-expose-tracebacks Source: https://docs.pylonsproject.org/projects/waitress/en/latest/runner.html The negative flag sets expose_tracebacks=False, equivalent to passing that argument to waitress.serve. ```bash --no-expose-tracebacks ``` -------------------------------- ### Nginx: Set proxy Host header Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt When using nginx as a reverse proxy, add these lines in a location section to pass the client's Host header to Waitress, ensuring correct URL generation. ```nginx proxy_set_header Host $host; ``` -------------------------------- ### Boolean flag --expose-tracebacks Source: https://docs.pylonsproject.org/projects/waitress/en/latest/runner.html The flag sets expose_tracebacks=True, equivalent to passing that argument to waitress.serve. ```bash --expose-tracebacks ``` -------------------------------- ### Configure trusted_proxy_headers for Forwarded header Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Set trusted_proxy_headers to "forwarded" to use the Forwarded (RFC7239) header instead of X-Forwarded-*. ```text trusted_proxy_headers = "forwarded" ``` -------------------------------- ### Apache RequestHeader set X-Forwarded-Proto Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Add this directive to Apache to set the X-Forwarded-Proto header to https. ```apache RequestHeader set X-Forwarded-Proto https ``` -------------------------------- ### Apache mod_proxy forwarded headers Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html Lists the headers that Apache's mod_proxy automatically forwards: X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Server. ```text X-Forwarded-For X-Forwarded-Host X-Forwarded-Server ``` -------------------------------- ### Apache: mod_proxy forwarded headers Source: https://docs.pylonsproject.org/projects/waitress/en/latest/_sources/reverse-proxy.rst.txt Lists the proxy headers automatically forwarded by Apache's mod_proxy; you must also add the RequestHeader directive to set X-Forwarded-Proto. ```apache X-Forwarded-For X-Forwarded-Host X-Forwarded-Server ``` ```apache RequestHeader set X-Forwarded-Proto https ``` -------------------------------- ### nginx proxy_set_header Host directive Source: https://docs.pylonsproject.org/projects/waitress/en/latest/reverse-proxy.html A configuration line for an nginx location section to forward the client's Host header to Waitress. Use this when configuring nginx as a reverse proxy. ```nginx proxy_set_header Host $host; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.