Skip to main content

Block socket creation by host/port.

Project description


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 ( in ['localhost', ''] and
            address.port == 80)


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

def test_foo():

sockfiltering context manager

from sockfilter import sockfiltering

def test_foo():
    with sockfiltering(socket_address_allowed):

enable and disable

import sockfilter
from unittest import TestCase

class FooTest(TestCase):

    def setUp(self):

    def tearDown(self):

    def test_foo(self):

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.4.tar.gz (5.5 kB view hashes)

Uploaded Source

Supported by

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