Linux command
daphne 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Run a Django Channels application
daphne [myproject.asgi]:application
Run on specific host and port
daphne -b [0.0.0.0] -p [8000] [myproject.asgi]:application
Run with Unix socket
daphne -u [/tmp/daphne.sock] [myproject.asgi]:application
Run with multiple worker threads
daphne --threads [4] [myproject.asgi]:application
Run with SSL
daphne -e ssl:443:privateKey=[key.pem]:certKey=[cert.pem] [myproject.asgi]:application
Enable verbose logging
daphne -v 2 [myproject.asgi]:application
说明
Daphne is an HTTP, HTTP2, and WebSocket protocol server for ASGI (Asynchronous Server Gateway Interface) applications, particularly Django Channels. It enables Django applications to handle WebSocket connections and other asynchronous protocols. Unlike WSGI servers (Gunicorn, uWSGI) that handle synchronous HTTP requests, Daphne supports long-lived connections required for real-time features: WebSockets, Server-Sent Events, and HTTP long polling. Daphne is built on Twisted's networking engine. It's typically used with Django Channels for chat applications, live notifications, and other real-time features. In production, it's often run behind a reverse proxy like nginx.
参数
- -b, --bind _address_
- IP address to bind (default: 127.0.0.1).
- -p, --port _port_
- Port to listen on (default: 8000).
- -u, --unix-socket _path_
- Unix socket path (alternative to TCP).
- --fd _fd_
- File descriptor to listen on.
- -e, --endpoint _spec_
- Twisted endpoint specification.
- --threads _n_
- Number of worker threads.
- -v, --verbosity _level_
- Logging verbosity (0-2).
- --access-log _path_
- Access log file path.
- --websocket_timeout _seconds_
- Maximum time to allow a WebSocket to be connected (-1 for infinite).
- --websocket_connect_timeout _seconds_
- Maximum time to allow a connection to handshake (-1 for infinite).
- --ping-interval _seconds_
- Seconds a WebSocket must be idle before a keepalive ping is sent.
- --ping-timeout _seconds_
- Seconds before a WebSocket is closed if no response to a keepalive ping.
- --application-close-timeout _seconds_
- Seconds an ASGI application has to exit after client disconnect before it is killed (default: 10).
- --proxy-headers
- Enable X-Forwarded-For handling.
- --proxy-headers-host _header_
- Header for host detection behind proxy.
- --root-path _path_
- Root path prefix for the ASGI scope (for reverse proxy setups).
FAQ
What is the daphne command used for?
Daphne is an HTTP, HTTP2, and WebSocket protocol server for ASGI (Asynchronous Server Gateway Interface) applications, particularly Django Channels. It enables Django applications to handle WebSocket connections and other asynchronous protocols. Unlike WSGI servers (Gunicorn, uWSGI) that handle synchronous HTTP requests, Daphne supports long-lived connections required for real-time features: WebSockets, Server-Sent Events, and HTTP long polling. Daphne is built on Twisted's networking engine. It's typically used with Django Channels for chat applications, live notifications, and other real-time features. In production, it's often run behind a reverse proxy like nginx.
How do I run a basic daphne example?
Run `daphne [myproject.asgi]:application` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -b, --bind _address_ do in daphne?
IP address to bind (default: 127.0.0.1).