python-mocket: Mock Socket Traffic for Python Tests

Summary
Mocket lets Python developers intercept socket and SSL traffic so tests can mock HTTP and other protocol clients without making real network requests. It supports synchronous and asynchronous clients, strict network blocking, and custom socket-level mocks.
At a glance
- Language
- Python
- License
- BSD-3-Clause
- Stars
- 311
- Forks
- 51
- Added to OSRepos
- August 3, 2026
- Last analyzed
- October 3, 2026
Use at your own risk
OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.
Overview
Mocket is a Python testing framework that patches socket and SSL behavior to simulate network responses. It helps developers test clients that communicate over sockets without depending on live services, covering HTTP and HTTPS as well as lower-level protocols.
Use it when ordinary HTTP-only mocking is too narrow, or when tests should fail if they attempt unmocked network access. The project also offers asyncio support and an HTTPretty compatibility layer.
Key Features
- Register HTTP and HTTPS responses for client requests, including headers, bodies, and simulated socket errors.
- Match requests using custom callbacks that can inspect paths and parsed query parameters.
- Enable strict mode to raise an exception on unmocked network access, with optional host and host-port allowances.
- Support asyncio clients, including aiohttp, with a dedicated TCP connector option.
- Build custom socket-protocol mocks using its low-level framework, with HTTP and basic Redis mocks as examples.
- Record real socket traffic to JSON files for reuse in tests.
- Integrate with pook through an optional mock engine and provide an HTTPretty compatibility layer.
Use Cases
- Test an HTTP client library without reaching external services, useful for application and library test suites.
- Verify behavior for connection failures and other error paths by registering socket exceptions.
- Prevent accidental network access in tests, especially when a suite should run fully offline.
- Exercise asyncio-based HTTP clients with predictable mocked responses.
- Prototype tests for a custom database or protocol client that communicates over sockets.
Project Facts
- Language: Python
- License: BSD-3-Clause
- Stars: 311
- Forks: 51
- Topics: aiohttp, asyncio, coverage, framework, http, httpretty, https, micropython, mock, mocket, mocking, python, socket, socket-communication, socket-programming, ssl, tdd, tdd-utilities, testing, urequests
- Archived: false
Getting Started
Install from PyPI:
pip install mocket
See the README for setup examples, integrations, and configuration details.
Alternatives
- httptoolkit: HTTP Toolkit captures and mocks HTTP(S) traffic through a cross-platform debugging tool, while Mocket provides in-process socket mocks for Python tests.
Considerations
Mocket relies on patching Python's socket and SSL modules, so interactions with client libraries that initialize SSL components at import time may require importing them while Mocket is active or using the provided connector option. Its HTTP mock is not identical to HTTPretty: it uses callback-based matching rather than regex URL entries and represents errors with exceptions rather than response-body functions. The README describes Python 2.7 compatibility as ending at version 3.9.4; check the package documentation for version-specific interpreter support.
Found this useful?
Share it with someone who would like python-mocket.
Comparisons
Source repository
Open the original repository on GitHub.
24 counted GitHub visits
Related repositories
Similar repositories that may be relevant next.

omnigent: Orchestrate AI Coding Agents Across Harnesses
October 5, 2026
Omnigent provides a shared orchestration layer for AI coding agents, with policies, sandboxing, and team collaboration. It suits developers who want to combine agent runtimes and access sessions across devices without tying workflows to one harness.

agentevals: Evaluate AI Agents from OpenTelemetry Traces
October 4, 2026
agentevals scores AI agent behavior from existing OpenTelemetry traces, without rerunning agents or making extra model calls. It suits teams building instrumented agents that need local evaluation, golden-set checks, or CI quality gates.

web-design: Create Consistent Web Pages with a Claude Code Skill
October 3, 2026
web-design is a Claude Code skill that turns product briefs, reference URLs, or screenshots into an editable design specification before generating web code. It is suited to developers and designers who want a repeatable, spec-led workflow for building consistent pages.

oomwoo: Build a DIY Robot Vacuum
October 2, 2026
OOMWOO is a planned, hackable robot vacuum built around Raspberry Pi, ROS2 and 2D LiDAR. It is aimed at makers who want to build and customize a locally controlled vacuum, but its hardware and build instructions are still in development.