{"id":9427,"library":"zope-publisher","title":"Zope Publisher","description":"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.","status":"active","version":"8.0","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/zope.publisher","tags":["web","zope","publisher","http","wsgi","framework-component"],"install":[{"cmd":"pip install zope.publisher","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core Zope component, widely used for defining interfaces and adapting objects.","package":"zope.interface","optional":false},{"reason":"Used for hierarchical object traversal, a fundamental aspect of Zope's URL dispatch.","package":"zope.traversing","optional":false},{"reason":"Provides security concepts for Zope, often integrated with publishing decisions.","package":"zope.security","optional":false}],"imports":[{"note":"HTTPRequest and HTTPResponse are located in the `http` submodule.","wrong":"from zope.publisher import HTTPRequest","symbol":"HTTPRequest","correct":"from zope.publisher.http import HTTPRequest"},{"note":"HTTPRequest and HTTPResponse are located in the `http` submodule.","wrong":"from zope.publisher import HTTPResponse","symbol":"HTTPResponse","correct":"from zope.publisher.http import HTTPResponse"},{"note":"Zope components are heavily interface-driven, often found in the `interfaces` submodule.","symbol":"IPublisher","correct":"from zope.publisher.interfaces import IPublisher"}],"quickstart":{"code":"from io import BytesIO\nfrom zope.publisher.http import HTTPRequest, HTTPResponse\n\n# Simulate a WSGI-like environment for the request\nrequest_environ = {\n    'REQUEST_METHOD': 'GET',\n    'PATH_INFO': '/my/path',\n    'QUERY_STRING': 'param1=value1&param2=value2',\n    'SERVER_NAME': 'localhost',\n    'SERVER_PORT': '8080',\n    'wsgi.input': BytesIO(b''), # For GET, body is usually empty\n    'wsgi.url_scheme': 'http',\n    'HTTP_HOST': 'localhost:8080',\n    'CONTENT_TYPE': 'text/plain; charset=utf-8',\n}\n\n# Create a request object\n# The first argument (input_stream) can be a BytesIO or similar for POST bodies\nrequest = HTTPRequest(BytesIO(b''), request_environ)\n\n# Create a response object\nresponse = HTTPResponse()\n\n# --- Interact with the Request ---\nprint(f\"Request URL: {request.getURL()}\")\nprint(f\"Request Method: {request.method}\")\nprint(f\"Path Info: {request.getURL('PATH_INFO')}\")\nprint(f\"Query parameters (combined form): {request.form}\")\nprint(f\"Request header 'Host': {request.get('HTTP_HOST')}\")\n\n# --- Interact with the Response ---\nresponse.setStatus(200)\nresponse.setHeader('Content-Type', 'text/html; charset=utf-8')\nresponse.setBody('<h1>Hello from Zope Publisher!</h1>')\n\nprint(f\"\\nResponse Status: {response.getStatus()}\")\nprint(f\"Response Header 'Content-Type': {response.getHeader('Content-Type')}\")\nprint(f\"Response Body: {response.consumeBody().decode('utf-8')}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Migrate your application to Python 3.9+ and ensure all dependencies are Python 3 compatible. Review `__unicode__`, `str`, `bytes` usage, and update import paths if necessary.","message":"Python 2 support was dropped as of `zope.publisher` 5.0. All subsequent versions, including 8.0, are Python 3.x only. Code written for Python 2 will not run on recent versions.","severity":"breaking","affected_versions":">=5.0"},{"fix":"If you are building a full web application, consider using a higher-level framework built on Zope (e.g., Zope, Grok, Plone) or a different Python web framework. Use `zope.publisher` directly only if you understand ZCA and need to customize the core publishing mechanics.","message":"`zope.publisher` is a low-level component designed for the Zope Component Architecture (ZCA). It is not a standalone web framework like Flask or FastAPI. Direct usage for simple web apps is often overly complex and requires manual setup of interfaces, adapters, and traversers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always install `zope.publisher` and other `zope.*` packages from a consistent source (e.g., PyPI with a `requirements.txt`) and ensure compatible versions. Check the `install_requires` in `setup.cfg` or `pyproject.toml` on GitHub for explicit dependency version ranges.","message":"Zope packages are tightly coupled. Incorrect versions of other `zope.*` dependencies can lead to runtime errors or unexpected behavior. This is especially true for `zope.interface`, `zope.traversing`, and `zope.security`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure your request object is an instance of `zope.publisher.http.HTTPRequest` or `zope.publisher.browser.BrowserRequest` (for browser contexts), or explicitly implements `zope.publisher.interfaces.IRequest` and its sub-interfaces via `zope.interface.implementer`.","cause":"The object you are passing as a request to a Zope publisher function (or related component) does not implement the `IRequest` interface, or no suitable adapter is registered for it.","error":"TypeError: Can't adapt <your_object> to zope.publisher.interfaces.IRequest"},{"fix":"The `HTTPRequest` and `HTTPResponse` classes are located in the `zope.publisher.http` submodule. Correct your import statement to `from zope.publisher.http import HTTPRequest`.","cause":"You are trying to import `HTTPRequest` (or `HTTPResponse`) directly from the top-level `zope.publisher` package.","error":"ImportError: cannot import name 'HTTPRequest' from 'zope.publisher'"},{"fix":"For POST requests, ensure the `request_environ['wsgi.input']` is a file-like object containing the raw request body bytes, and `request_environ['CONTENT_TYPE']` is set appropriately (e.g., `application/x-www-form-urlencoded` or `multipart/form-data`) so `HTTPRequest` can parse it.","cause":"When handling POST requests, `request.form` combines query string parameters and POST body data. If `wsgi.input` (the stream for the request body) was not correctly provided or the `CONTENT_TYPE` was incorrect, POST data might not be parsed.","error":"AttributeError: 'HTTPRequest' object has no attribute 'form' or 'POST' data is missing."}]}