Skip to main content

PyHTTPIntercept

Provides a HTTP Server which can be used to intercept and modify API requests/responses for local clients.

This is useful for client testing where generating different response types from the API is not always ideal.

The HTTP server provides the following functionality:

  • Hosting of sites
  • Redirecting requests with the following methods:
    • HTTP 3xx statuses
    • transparently to the client
  • Intercepting and modifying of requests. Man-in-the-middle, useful for client testing against a production API.
  • Proxying of requests

When a request comes in methods are checked & executed in the following order:

  1. Redirect
  2. Hosting
  3. Intercept - Intercept will only be reached if request is not picked up by hosting.
  4. Proxy - Proxy configuration will only be reached if request is not picked up by Hosting or Intercept.

Hosting requests

Sites can be hosted as with any webserver. The server supports static sites/resources only.

Hosting configuration

An example configuration file:

{
  "/": {
    "doc_root": "default_sites/",
    "active": true,
    "description": "Root Site"
  },
  "/example": {
    "doc_root": "default_sites/example/",
    "active": true,
    "description": "Example Site"
  }
}

A configured site configurations key will be set to the expected url path.

Site configuration parameters:

  • Object key: String - Path where site will be hosted.
  • doc_root: String - The full path to the configured site. A relative path can also be configured and is explained below.
  • active: Boolean - True if the site is to be served.
  • description: String - [optional] A description for your site.

Relative paths

TODO


Redirecting requests

Sending HTTP 3xx statuses

These are your standard redirects.

Transparently to the client

These are useful for clients that do not support redirects.

An example use would be redirecting a client with hard coded endpoints to a lab environemnt for testing without having to generate & install specific builds for the lab.

Redirect configuration

Parameters:

  • Object key: String - Domain to redirect.
  • host: String - [optional] The full domain to redirect to.
  • paths: Object - [optional] An object containing the paths being redirected for this domain.
  • active: Boolean - True to enable redirect.
  • description: String - [optional] A description for your redirect.

Note: at least one of host or paths must be specified!

Path object:

  • Object key: String - Path to redirect.
  • host: String - [optional] The full domain to redirect to.
  • path: String - [optional] The full path to redirect to. If omitted then the path will be set to domain root.
  • status: Number - [optional] The HTTP 3xx status to send. Specifying this parameter tells the server to use a HTTP 3xx redirect rather than redirecting transparently.
  • active: Boolean - True to enable redirect.
  • description: String - [optional] A description for your redirect.

Note: at least one of path or host (from either site/path config) must be specified!

If a host is specifed in the key it will only by honoured when intercepting or proxying, for anything else the keys will be ignored.

Redirecting paths within the same site:

{
  "example.com": {
    "paths": {
      "/example_redirect": {
        "path": "/temp_path",
        "active": true,
      }
    },
    "active": true,
  }
}

Redirecting paths within the same site using a HTTP 3xx redirect:

{
  "example.com": {
    "paths": {
      "/example_redirect": {
        "path": "/temp_path",
        "status": 301,
        "active": true,
      }
    },
    "active": true,
  }
}

Redirecting paths from one site to another:

{
  "example.com": {
    "host": "example2.com",
    "paths": {
      "/example_redirect": {
        "path": "/",
        "active": true,
      }
    },
    "active": true,
  }
}

This can also be done on a path by path basis:

{
  "example.com": {
    "paths": {
      "host": "example2.com",
      "/example_redirect": {
        "path": "/",
        "active": true,
      },
      "/example_redirect2": {
        "host": "example3.com",
        "path": "/",
        "active": true,
      }
    },
    "active": true,
  }
}

If a host is configured for a path it takes precedence over the site redirect host.

Redirecting one domain to another:

{
  "example.com": {
    "host": "example2.com",
    "active": true,
  }
}

This applies to all paths for the domain.


Intercepting requests

Intercept configuration

Parameters:

  • Object key: String - Domain to Intercept.
  • active: Boolean - True to enable proxy.
  • description: String - [optional] A description for your proxy.

An example configuration file:

{
  "example.com": {
    "active": true,
    "description": "Intercept & modify"
  }
}

Proxying requests

Parameters:

  • Object key: String - Domain to proxy.
  • active: Boolean - True to enable proxy.
  • description: String - [optional] A description for your proxy.

Proxy can be configured to either proxy all requests:

{
  "*": {
    "active": true,
    "description": "Proxy All"
  }
}

or specific domains only:

{
  "example.com": {
    "active": true,
    "description": "Proxy example.com"
  }
}

Wildcards

The '*' character can be used as a wildcard.

Domain wildcards

*.example.com will handle requests for all subdomains, but not example.com. *example.com will handle requests for all subdomains, including example.com.

In the following example the first configuration example.com will only proxy requests for example.com. While the second configuration \*.example.com will proxy all subdomains but not example.com.

{
  "example.com": {
    "active": true,
    "description": "Proxy example.com"
  },
  "*.example.com": {
    "active": true,
    "description": "Proxy subdomains of example.com"
  }
}

This snippet can be simplified to:

{
  "*example.com": {
    "active": true,
    "description": "Proxy example.com and all subdomains"
  }
}

Path wildcards (Only available for redirects)

/testing/* will redirect all requests for path /testing including sub paths i.e /testing/path_a

In the following example example.com/example_redirect and all sub paths will be redirected to example.com/temp_path.

{
  "example.com": {
    "paths": {
      "/example_redirect/*": {
        "path": "/temp_path",
        "active": true,
      }
    },
    "active": true,
  }
}

Metadata

Release files for pyhttpintercept 0.34.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyhttpintercept 0.34.1
File Size Uploaded
pyhttpintercept-0.34.1.tar.gz 903.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyhttpintercept 0.34.1
File Interpreter ABI Platform
pyhttpintercept-0.34.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 1.9 MB

Release files / pyhttpintercept-0.34.1.tar.gz

Download URL pyhttpintercept-0.34.1.tar.gz
Size 903.7 kB
Tags Source
SHA-256 checksum
How to use checksums
307e09de9f8f52208ccd70248155ae97f1a3cb7be15c2708a11442b1cc79a025
BLAKE2b-256 checksum
How to use checksums
68332abfd9d34e3797df6ba51aaaba0a30e6eb95144b90340511e27d9b6e5c10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

Release files / pyhttpintercept-0.34.1-py2.py3-none-any.whl

Download URL pyhttpintercept-0.34.1-py2.py3-none-any.whl
Size 961.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f0d1c7059a3eca7f5b4ab1eeb1dae9ca3069e3f6dd52a4e39017fbf2a7f0991e
BLAKE2b-256 checksum
How to use checksums
8a2201d7fc7e0d30ff86207eed7f01bd44529ee09826dd5f1ae0e92ffdee3896
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15

Release history Release notifications | RSS feed

This release

0.34.1 This release

2 release files

0.34.0

2 release files

0.33.2

2 release files

0.33.1

2 release files

0.33.0

2 release files

0.32.2

2 release files

0.32.1

2 release files

0.32.0

2 release files

0.31.1

2 release files

0.31.0

2 release files

0.30.0

2 release files

0.29.0

2 release files

0.28.1

2 release files

0.27.0

2 release files

0.25.2

2 release files

0.25.1

2 release files

0.25.0

2 release files

0.24.5

2 release files

0.24.4

2 release files

0.24.3

2 release files

0.24.2

2 release files

0.24.1

2 release files

0.24.0

2 release files

0.23.2

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.22.1

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.20.1

2 release files

0.20.0

2 release files

0.19.0

2 release files

0.18.1

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.16.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.4

2 release files

0.13.2

2 release files

0.13.0

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page