python 在Python中禁用Internet。使用py.test钩子。 (禁用socket.socket。)GREAT用于单元测试。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在Python中禁用Internet。使用py.test钩子。 (禁用socket.socket。)GREAT用于单元测试。相关的知识,希望对你有一定的参考价值。

# Example usage of the py.test fixture in tests
import socket
import pytest

try:
    from urllib2 import urlopen
except ImportError:
    import urllib3
    urlopen = urllib.request.urlopen


def test_socket_disabled_by_default():
    # default behavior: socket.socket is unusable
    with pytest.raises(RuntimeError):
        urlopen(u'https://www.python.org/')


def test_explicitly_enable_socket(enable_socket):
    # socket is enabled by pytest fixture from conftest. disabled in finalizer
    assert socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Put this in the conftest.py at the top of your unit tests folder,
# so it's available to all unit tests
import pytest
import _socket_toggle


def pytest_runtest_setup():
    """ disable the interet. test-cases can explicitly re-enable """
    _socket_toggle.disable_socket()


@pytest.fixture(scope='function')
def enable_socket(request):
    """ re-enable socket.socket for duration of this test function """
    _socket_toggle.enable_socket()
    request.addfinalizer(_socket_toggle.disable_socket)
from __future__ import print_function
import socket
import sys

_module = sys.modules[__name__]

def disable_socket():
    """ disable socket.socket to disable the Internet. useful in testing.

    .. doctest::
        >>> # we'll try httplib a little later, but import it immediately, before tampering.
        >>> # hopefully this proves that the 'patch' doesn't have to happen before other imports.
        >>> import httplib
        >>> enable_socket() # should be able to call "enable" at any time, even when enabled...
        [!] socket.socket is UN-blocked, and the network can be accessed.
        >>> disable_socket()  # OK let's disable it.
        [!] socket.socket is now blocked. The network should be inaccessible.
        >>> socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Traceback (most recent call last):
        ...
        RuntimeError: A test tried to use socket.socket without explicitly un-blocking it.
        >>> httplib.HTTPConnection("scanme.nmap.org:80").request("GET", "/")
        Traceback (most recent call last):
        ...
        RuntimeError: A test tried to use socket.socket without explicitly un-blocking it.
        >>> enable_socket()
        [!] socket.socket is UN-blocked, and the network can be accessed.
        >>> enable_socket() # twice in a row should work.
        [!] socket.socket is UN-blocked, and the network can be accessed.
        >>> disable_socket()
        [!] socket.socket is now blocked. The network should be inaccessible.
        >>> socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Traceback (most recent call last):
        ...
        RuntimeError: A test tried to use socket.socket without explicitly un-blocking it.
        >>> enable_socket()
        [!] socket.socket is UN-blocked, and the network can be accessed.
    """
    setattr(_module, u'_socket_disabled', True)

    def guarded(*args, **kwargs):
        if getattr(_module, u'_socket_disabled', False):
            raise RuntimeError(
                u"A test tried to use socket.socket without explicitly un-blocking it.")
        else:
            # SocketType is a valid, public alias of socket.socket,
            # we use it here to avoid namespace collisions
            return socket.SocketType(*args, **kwargs)

    socket.socket = guarded

    print(u'[!] socket.socket is now blocked. The network should be inaccessible.')


def enable_socket():
    """ re-enable socket.socket to enable the Internet. useful in testing.
    """
    setattr(_module, u'_socket_disabled', False)
    print(u'[!] socket.socket is UN-blocked, and the network can be accessed.')

以上是关于python 在Python中禁用Internet。使用py.test钩子。 (禁用socket.socket。)GREAT用于单元测试。的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 中禁用断言

在 python 3 中禁用异常链接

在嵌入式 Python 中禁用内置模块导入

在没有 Internet 访问权限的情况下安装 Python 模块

Python继承 - 如何禁用函数

如何使用 javascript 或 php 禁用 Internet Explorer 缓存