Skip to main content

Block socket creation by host/port.

Project description

Usage

The intended use is for making sure your unit tests don’t make network connections. Activate sockfilter with a predicate like:

# only allow http connections to localhost
def socket_address_allowed(address):
    return (address.host in ['localhost', '127.0.0.1'] and
            address.port == 80)

sockfilter.enable(socket_address_allowed)

Any subsequent attempts to open a connection at an address not satisfying the predicate will raise sockfilter.SockFilterError.

with_sockfiltering decorator

from sockfilter import with_sockfiltering

@with_sockfiltering(socket_address_allowed)
def test_foo():
    foo()

sockfiltering context manager

from sockfilter import sockfiltering

def test_foo():
    with sockfiltering(socket_address_allowed):
        foo()

enable and disable

import sockfilter
from unittest import TestCase

class FooTest(TestCase):

    def setUp(self):
        sockfilter.enable(socket_address_allowed)

    def tearDown(self):
        sockfilter.disable()

    def test_foo(self):
        foo()

Under the hood

Sockfilter works by monkey-patching Python’s socket library, inspired by HTTPretty.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sockfilter-1.0.tar.gz (1.8 kB view details)

Uploaded Source

File details

Details for the file sockfilter-1.0.tar.gz.

File metadata

  • Download URL: sockfilter-1.0.tar.gz
  • Upload date:
  • Size: 1.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sockfilter-1.0.tar.gz
Algorithm Hash digest
SHA256 ab795ff5363080224e3f321da7f22d2e82926e77692f67ea00a54add3110d81d
MD5 9985441df054697623ca5e20c95e05a3
BLAKE2b-256 4c28fbd294ed763ea831ef9e82280e212f7030b5cdca487b2c8af7e203a09e9a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page