Zope Publisher

8.0 · active · verified Thu Apr 16

The Zope publisher publishes Python objects on the web. It is a core component of the Zope application server and related frameworks, responsible for dispatching HTTP requests to Python objects and generating responses. As of version 8.0, it supports Python 3.9+ and maintains its role in the Zope ecosystem as a low-level building block for web applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create and interact with `HTTPRequest` and `HTTPResponse` objects, which are the fundamental data structures `zope.publisher` operates on. It shows how to simulate a WSGI environment, access request properties like URL, method, and headers, and set response status, headers, and body. This is crucial for understanding how data flows through the Zope publishing process.

from io import BytesIO
from zope.publisher.http import HTTPRequest, HTTPResponse

# Simulate a WSGI-like environment for the request
request_environ = {
    'REQUEST_METHOD': 'GET',
    'PATH_INFO': '/my/path',
    'QUERY_STRING': 'param1=value1&param2=value2',
    'SERVER_NAME': 'localhost',
    'SERVER_PORT': '8080',
    'wsgi.input': BytesIO(b''), # For GET, body is usually empty
    'wsgi.url_scheme': 'http',
    'HTTP_HOST': 'localhost:8080',
    'CONTENT_TYPE': 'text/plain; charset=utf-8',
}

# Create a request object
# The first argument (input_stream) can be a BytesIO or similar for POST bodies
request = HTTPRequest(BytesIO(b''), request_environ)

# Create a response object
response = HTTPResponse()

# --- Interact with the Request ---
print(f"Request URL: {request.getURL()}")
print(f"Request Method: {request.method}")
print(f"Path Info: {request.getURL('PATH_INFO')}")
print(f"Query parameters (combined form): {request.form}")
print(f"Request header 'Host': {request.get('HTTP_HOST')}")

# --- Interact with the Response ---
response.setStatus(200)
response.setHeader('Content-Type', 'text/html; charset=utf-8')
response.setBody('<h1>Hello from Zope Publisher!</h1>')

print(f"\nResponse Status: {response.getStatus()}")
print(f"Response Header 'Content-Type': {response.getHeader('Content-Type')}")
print(f"Response Body: {response.consumeBody().decode('utf-8')}")

view raw JSON →