TCP Transport Example¶
Complexity: 🟢 Beginner | Lines: ~20 | Source Code: examples/short/tcp_transport.py
A plugin server using TCP sockets instead of Unix domain sockets - essential for Windows compatibility and network communication.
#!/usr/bin/env python3
"""
Server with TCP transport (20 lines).
Demonstrates using TCP instead of Unix sockets.
"""
import asyncio
from pyvider.rpcplugin import plugin_protocol, plugin_server
from provide.foundation import logger
async def main():
"""Run server with TCP transport."""
protocol = plugin_protocol()
handler = object()
# Use TCP transport on port 50051
server = plugin_server(
protocol=protocol,
handler=handler,
transport="tcp",
port=50051
)
logger.info("Starting server with TCP transport on port 50051...")
await server.serve()
if __name__ == "__main__":
asyncio.run(main())
Key Points¶
transport="tcp"specifies TCP socket transport instead of Unix domain socketsport=50051sets the TCP port (useport=0for automatic port assignment)- Windows compatibility - TCP works on all platforms (Unix sockets not available on Windows)
- Network communication - TCP allows plugin communication across network boundaries
- Automatic handshake - client detects TCP endpoint from handshake output
Related Examples¶
- Basic Server - Default Unix socket transport
- Transport Guide
What's Next?¶
Next Steps¶
- Add mTLS - Secure TCP connections with Security Configuration
- Network Discovery - Implement service discovery for network plugins
- Firewall Rules - Configure firewall for TCP port
- Load Balancing - Set up load balancing for multiple server instances
Learning Path¶
- Beginner: Try Basic Server with Unix sockets for comparison
- Intermediate: Learn Transport Selection strategies
- Advanced: Implement Multi-Transport Support