Python 2 Server Guide
Build a PulseRPC server in Python 2.7 using the standard library.
Python 2 support policy
Python 2 is actively maintained for legacy systems, not frozen, and not expected to reach feature parity with the Python 3 runtime. Python 2 reached end of life in 2020, but legacy deployments persist, so this runtime continues to receive bug fixes and targeted features. The canonical RUNTIME_IMPLEMENTATION_GUIDE.md checklist applies to Python 2 except for the capabilities explicitly waived below.
Intentionally waived capabilities
- Contract verification (diff / auditors / checksum) — Python 2 has no diff
engine, no auditors, and no checksum helper. The following Python 3 symbols
are intentionally absent from
pulserpc.__all__:extract_checksum,diff_idl,EntityType,ChangeType,Direction,Severity,ContractDelta,VerificationResult,ContractAuditor,NoOpAuditor,LoggingAuditor, andFailFastAuditor. A client therefore cannot perform the fetch-pulserpc-idl-and-diff compatibility check described in R31/D15; this remains a documented carve-out. - Structured validation classes —
ValidationResultandValidationErrorare Python 3 only. Contract-level validation failures instead raiseRPCErrorwith code-32602(request) or-32603(response) and a plain-dictdatapayload ({"error": ..., "invalid_fields": [...]}). - Batch requests — the Python 2
Server.callaccepts one request object. Batch arrays are rejected with-32600(“Request must be an object”); there is no batch cap because there is no batch dispatch to bound (R18 carve-out). - Generated stubs and HTTP adapter — Python 2 generates only
idl.jsonand the runtime package. Noserver.py,client.py,test_server.pyortest_client.pyis generated. The 1 MiB body cap and 405 handling therefore live in your adapter, not in generated code (see below). - Type resolution — the Python 2 helpers use exact dict keys; the cached, namespace-aware resolution index introduced for Python 3 (R21) is not present.
tests/test_api_surface.py pins the 19 exported symbols and fails on drift.
An API difference that is not listed here is a bug, not an intentional waiver.
Shared wire-contract rules (implemented here too)
These rules are identical to Python 3 and the other runtimes:
- D1/R25 —
intis signed 64-bit.validate_intaccepts integral values in[-9223372036854775808, 9223372036854775807]; fractional, NaN/Inf, bool and out-of-range values are rejected. Python 2longvalues are range-checked explicitly. - D2/R24 —
idis a string, number or null. Booleans, objects and arrays are rejected with-32600and a responseidofnull. - D4/R15 — internal errors never leak details. An unhandled handler
exception returns
{"code": -32603, "message": "Internal error"}with nodata. The exception and traceback are logged server-side through the module logger,logging.getLogger(__name__)—pulserpc.serverfor the runtime imported under its normal package name — or passed toon_error. DeclaredRPCErrormessages/data and-32602validation messages are preserved. - D5 — resource limits. The request body cap is
pulserpc.server.DEFAULT_MAX_BODY_SIZE(1 MiB) for your adapter to enforce;HttpTransportcaps response bodies atMAX_RESPONSE_BODY_SIZE(10 MiB) and raisesValueErrorbeyond it. - D6/R19 — 30 s HTTP timeout (
pulserpc.transport.DEFAULT_TIMEOUT). - D13/R29/R30 — custom headers and trailing-slash normalization.
HttpTransport(url, headers={...})merges headers overContent-Type, and every trailing/is stripped from the base URL: the runtime appliesurl.rstrip("/"), soHttpTransport("http://host/api///")is normalized tohttp://host/api.
Prerequisites
- Python 2.7
- PulseRPC CLI installed (Installation Guide)
1. Generate Code
Generate Python 2 targeted code:
pulserpc -plugin python-client-server -python-version 2 -dir ./output checkout.pulse
This creates:
output/checkout/idl.json- IDL metadata (in the namespace directory)output/pulserpc/- Runtime library (RPCError, validation, types)
Note: Python 2 generates only metadata + runtime. No stub classes are generated (Python 2 uses plain dicts and strings for structs and enums).
2. Runtime Architecture
The Python 2 runtime provides:
- Server - Main server class (from
pulserpc) - Contract - IDL validation (from
pulserpc) - RPCError - For returning structured errors (from
pulserpc)
Handlers are plain classes (no generated base classes in Python 2). All handler methods receive ctx as the first positional argument for transport-level metadata.
The Server constructor accepts an optional on_error callback for custom error handling:
def handle_error(e):
# Log to your monitoring system, metrics, etc.
print("Handler error:", e)
server = Server(contract, on_error=handle_error)
When on_error is set, it is invoked with unhandled handler exceptions instead of logging them. If on_error is not set (default None), unhandled exceptions are logged (with traceback) through the pulserpc.server logger — server.py uses logging.getLogger(__name__), which resolves to that name for the normally imported runtime — and the client receives only -32603 "Internal error" with no data.
Transport Context (ctx)
All handler methods receive a ctx parameter as the first positional argument. This contains transport-level metadata (headers, auth tokens, etc.) passed automatically by the runtime.
Important: Python 2 runtime passes ctx as a first positional argument, so it must be the first parameter after self in your method signature. See pkg/runtime/runtimes/python2/pulserpc/server.py for implementation details.
class CatalogServiceImpl(object):
def listProducts(self, ctx):
# ctx may be None if no transport context is provided
# ctx is a dict with transport metadata (e.g., headers)
return products_db
def getProduct(self, ctx, productId):
# ctx is passed as first positional argument
return None
3. Implement the Server
Create my_server.py:
#!/usr/bin/env python
import os
import sys
import json
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import random
import time
from pulserpc import Server, Contract, RPCError
products_db = [
{
"productId": "prod001",
"name": "Wireless Mouse",
"description": "Ergonomic mouse",
"price": 29.99,
"stock": 50,
"imageUrl": "https://example.com/mouse.jpg",
},
{
"productId": "prod002",
"name": "Mechanical Keyboard",
"description": "RGB keyboard",
"price": 89.99,
"stock": 25,
"imageUrl": "https://example.com/keyboard.jpg",
},
]
carts_db = {}
orders_db = {}
class CatalogServiceImpl(object):
def listProducts(self, ctx):
return products_db
def getProduct(self, ctx, productId):
for p in products_db:
if p["productId"] == productId:
return p
return None
class CartServiceImpl(object):
def addToCart(self, ctx, request):
cart_id = request.get("cartId") or "cart_%d" % random.randint(1000, 9999)
if cart_id not in carts_db:
carts_db[cart_id] = {"cartId": cart_id, "items": [], "subtotal": 0.0}
cart = carts_db[cart_id]
product = None
for p in products_db:
if p["productId"] == request.get("productId"):
product = p
break
if not product:
raise RPCError(-32602, "Product '%s' not found" % request.get("productId"))
for item in cart["items"]:
if item["productId"] == request.get("productId"):
item["quantity"] += request.get("quantity", 0)
item["price"] = product["price"]
break
else:
cart["items"].append({
"productId": request.get("productId"),
"quantity": request.get("quantity", 0),
"price": product["price"],
})
cart["subtotal"] = sum(item["price"] * item["quantity"] for item in cart["items"])
return cart
def getCart(self, ctx, cartId):
return carts_db.get(cartId)
def clearCart(self, ctx, cartId):
if cartId in carts_db:
carts_db[cartId]["items"] = []
carts_db[cartId]["subtotal"] = 0.0
return True
return False
class OrderServiceImpl(object):
def createOrder(self, ctx, request):
if request.get("cartId") not in carts_db:
raise RPCError(1001, "CartNotFound: Cart does not exist")
cart = carts_db[request.get("cartId")]
if not cart["items"]:
raise RPCError(1002, "CartEmpty: Cannot create order from empty cart")
addr = request.get("shippingAddress") or {}
if not addr.get("street") or not addr.get("city") or not addr.get("zipCode"):
raise RPCError(1005, "InvalidAddress: Shipping address validation failed")
for item in cart["items"]:
product = None
for p in products_db:
if p["productId"] == item["productId"]:
product = p
break
if product and product["stock"] < item["quantity"]:
raise RPCError(1004, "OutOfStock: Insufficient inventory")
if random.random() < 0.1:
raise RPCError(1003, "PaymentFailed: Card declined by issuer")
order_id = "order_%d" % random.randint(10000, 99999)
order = {
"orderId": order_id,
"cart": cart,
"shippingAddress": request.get("shippingAddress"),
"paymentMethod": request.get("paymentMethod"),
"status": "pending",
"total": cart["subtotal"],
"createdAt": int(time.time()),
}
orders_db[order_id] = order
carts_db[request.get("cartId")]["items"] = []
carts_db[request.get("cartId")]["subtotal"] = 0.0
return {"orderId": order_id, "message": "Order created successfully"}
def getOrder(self, ctx, orderId):
return orders_db.get(orderId)
port = int(os.environ.get("SERVER_PORT", "8080"))
# Find idl.json - it's placed in the namespace directory
idl_path = os.path.join(os.path.dirname(__file__), "checkout", "idl.json")
with open(idl_path) as f:
idl_data = json.load(f)
contract = Contract(idl_data)
server = Server(contract)
server.add_handler("CatalogService", CatalogServiceImpl())
server.add_handler("CartService", CartServiceImpl())
server.add_handler("OrderService", OrderServiceImpl())
class PulseRPCHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers.getheader('Content-Length', 0))
if content_length <= 0:
self.send_error(400, "No content")
return
request_body = self.rfile.read(content_length)
request_data = json.loads(request_body)
# Build context dict from request headers
ctx = {
'headers': dict(self.headers),
'remote_addr': self.client_address[0] if hasattr(self, 'client_address') else None,
}
# Example: extract auth token from Authorization header
auth_header = self.headers.getheader('Authorization')
if auth_header:
ctx['auth'] = auth_header
# Pass ctx to server.call() - it will be forwarded to handler methods
response = server.call(request_data, ctx)
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
if response:
self.wfile.write(json.dumps(response))
def log_message(self, format, *args):
pass
if __name__ == "__main__":
print "Starting PulseRPC server on port %d..." % port
httpd = HTTPServer(("0.0.0.0", port), PulseRPCHandler)
print "Server running at http://localhost:%d" % port
httpd.serve_forever()4. Integrate with BaseHTTPServer
The server integrates with Python 2’s BaseHTTPServer. Build a ctx dict from request headers and pass it to server.call():
import json
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from pulserpc import RPCError
from pulserpc.server import DEFAULT_MAX_BODY_SIZE
class PulseRPCHandler(BaseHTTPRequestHandler):
MAX_BODY_SIZE = DEFAULT_MAX_BODY_SIZE # 1 MiB (R16)
def do_POST(self):
# Reject an oversized body before reading it into memory.
try:
content_length = int(self.headers.getheader('Content-Length', 0))
except (TypeError, ValueError):
content_length = -1
if content_length <= 0:
self._send_error(None, -32700, "Parse error")
return
if content_length > self.MAX_BODY_SIZE:
self._send_error(None, -32600, "Request body too large")
return
request_body = self.rfile.read(content_length)
try:
request_data = json.loads(request_body)
except ValueError:
self._send_error(None, -32700, "Parse error")
return
# Build context dict from request headers
ctx = {
'headers': dict(self.headers),
'remote_addr': self.client_address[0],
}
# Example: extract auth token
auth_header = self.headers.getheader('Authorization')
if auth_header:
ctx['auth'] = auth_header
# Pass ctx to server.call() - forwarded to all handler methods
response = server.call(request_data, ctx)
# Send response
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
if response:
self.wfile.write(json.dumps(response))
def _send_error(self, request_id, code, message):
body = json.dumps({
'jsonrpc': '2.0',
'error': {'code': code, 'message': message},
'id': request_id,
})
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
# Only POST is a valid JSON-RPC transport (D3/R22). Every other method
# must return HTTP 405 with a JSON-RPC -32600 body.
def do_GET(self):
self._method_not_allowed()
def do_PUT(self):
self._method_not_allowed()
def do_DELETE(self):
self._method_not_allowed()
def _method_not_allowed(self):
body = json.dumps({
'jsonrpc': '2.0',
'error': {
'code': -32600,
'message': 'Invalid Request - only POST allowed',
},
'id': None,
})
self.send_response(405)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
Handler methods receive ctx as the first positional argument:
class OrderServiceImpl(object):
def createOrder(self, ctx, request):
# ctx contains transport metadata (headers, auth, remote_addr)
if ctx and ctx.get('auth'):
print("Authenticated request:", ctx['auth'])
# ... rest of handler logic
Start the server:
python my_server.py
Server runs on http://localhost:8080
Next Steps
- Python Reference - Type mappings, patterns, best practices
- IDL Syntax - Full IDL language reference