A simple Python API for self-hosting stateless servers
Create a new server from a dictionary of endpoints. This is the primary method for turning Python functions into HTTP endpoints with automatic process management.
name | str | Unique name for the server |
endpoints | dict[str, Callable] | Dictionary mapping URL paths to Python functions |
dependencies | list[str] | Optional list of pip packages to install |
port | int | Optional specific port (default: auto-assign) |
expiration_seconds | int | Auto-cleanup after N seconds (default: 86400, -1 = never) |
force | bool | Replace existing server with same name (default: False) |
Server | Server object with url, status, logs, and control methods |
import syft_serve as ss
def predict(data: dict):
return {"prediction": "positive", "confidence": 0.95}
# Create a server
server = ss.create(
name="ml_api",
endpoints={"/predict": predict},
dependencies=["numpy", "scikit-learn"]
)
print(server.url) # http://localhost:8000
List all currently running servers. Returns a list of Server objects that you can inspect and control.
No parameters |
list[Server] | List of all active Server objects |
# List all servers
servers = ss.list()
for server in servers:
print(f"{server.name}: {server.url} - {server.status}")
Get a specific server by name. Useful for retrieving a server created in a previous session or different notebook.
name | str | Name of the server to retrieve |
Server | Server object if found |
# Get a specific server
server = ss.get("ml_api")
# Access server properties
print(f"URL: {server.url}")
print(f"Status: {server.status}")
print(f"Uptime: {server.uptime}")
Server objects have rich display representations in Jupyter notebooks that show status, logs, and control options at a glance.
When you evaluate a server object in a Jupyter cell, you get an interactive display:
Status: | ✅ Running |
URL: | http://localhost:8000 |
Endpoints: |
|
Uptime: | 2h 15m |
Expires In: | ⏰ 21h 45m |
PID: | 12345 |
server.stdout.tail(20)
, server.stderr.tail(20)
, server.env
, server.terminate()
# In Jupyter, just evaluate the server object
server
# Or get multiple servers
servers = ss.list()
servers # Shows a list with each server's display
Get the most recent lines from the server's standard output logs. Useful for debugging and monitoring server activity.
n | int | Number of lines to return (default: 10) |
str | Last n lines from stdout |
# Get recent logs
logs = server.stdout.tail(20)
print(logs)
# Check for errors
errors = server.stderr.tail(20)
Stream logs in real-time as they are generated. This creates a continuous output that follows the log file, similar to `tail -f`.
No parameters |
Generator | Yields log lines as they appear |
# Follow logs in real-time
for line in server.stdout.follow():
print(line)
if "error" in line.lower():
break
Get the most recent lines from the server's standard error logs. Useful for debugging errors and tracking warnings.
n | int | Number of lines to return (default: 10) |
str | Last n lines from stderr |
# Check for errors
errors = server.stderr.tail(20)
if errors:
print("Recent errors:", errors)
Stream error logs in real-time as they are generated. Perfect for monitoring errors and warnings as they occur.
No parameters |
Generator | Yields error log lines as they appear |
# Monitor errors in real-time
for error in server.stderr.follow():
print(f"ERROR: {error}")
# Send alert or take action
Access the server's environment information including Python version, installed packages, and environment variables.
python | str | Python version used by the server |
packages | list[str] | List of installed packages with versions |
variables | dict | Environment variables |
# Check server environment
print(f"Python: {server.env.python}")
print(f"Packages: {len(server.env.packages)}")
# Find specific package
for pkg in server.env.packages:
if "numpy" in pkg:
print(pkg)
Gracefully terminate a server. This stops the server process, cleans up resources, and removes it from the active servers list.
timeout | float | Maximum seconds to wait for graceful shutdown (default: 5.0) |
None | Server is terminated and cleaned up |
# Terminate a specific server
server = ss.get("ml_api")
server.terminate()
# Or with custom timeout
server.terminate(timeout=10.0)
Terminate all running servers at once. This is useful for cleanup in notebooks or when shutting down a development environment.
force | bool | Use force termination if graceful shutdown fails (default: True) |
dict | Summary of termination results |
# Clean up all servers
results = ss.terminate_all()
print(f"Terminated {results['tracked_terminated']} servers")
if results['orphaned_discovered'] > 0:
print(f"Also found and terminated {results['orphaned_terminated']} orphaned processes")
Servers can be configured to automatically terminate after a specified time period. This prevents resource leaks and is perfect for temporary endpoints.
expiration_seconds | int | Seconds until auto-termination (default: 86400 = 24 hours, -1 = never expire) |
# Create a temporary server (5 minutes)
temp_server = ss.create(
name="temp_endpoint",
endpoints={"/process": process_data},
expiration_seconds=300
)
# Create a permanent server
permanent_server = ss.create(
name="always_on",
endpoints={"/health": lambda: "OK"},
expiration_seconds=-1
)
# Check time remaining
print(f"Expires in: {temp_server.expiration_info}")