当前位置: 首页>>代码示例>>Python>>正文


Python socket.html方法代码示例

本文整理汇总了Python中socket.html方法的典型用法代码示例。如果您正苦于以下问题:Python socket.html方法的具体用法?Python socket.html怎么用?Python socket.html使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在socket的用法示例。


在下文中一共展示了socket.html方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: mock_dns

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def mock_dns():
    import socket

    _orig_getaddrinfo = socket.getaddrinfo

    def patched_getaddrinfo(host, *args, **kwargs):
        if host.endswith('.mock'):
            # nginx doesn't support multiple tls versions from the same container
            port = HOSTNAME_TO_PORT_MAPPING.get(host, 4443)

            # See socket.getaddrinfo, just updating the hostname here.
            # https://docs.python.org/3/library/socket.html#socket.getaddrinfo
            return [(2, 1, 6, '', ('127.0.0.1', port))]

        return _orig_getaddrinfo(host, *args, **kwargs)

    socket.getaddrinfo = patched_getaddrinfo
    yield
    socket.getaddrinfo = _orig_getaddrinfo 
开发者ID:DataDog,项目名称:integrations-core,代码行数:21,代码来源:conftest.py

示例2: _build_ssl_context

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def _build_ssl_context(
    disable_ssl_certificate_validation, ca_certs, cert_file=None, key_file=None,
    maximum_version=None, minimum_version=None,
):
    if not hasattr(ssl, "SSLContext"):
        raise RuntimeError("httplib2 requires Python 3.2+ for ssl.SSLContext")

    context = ssl.SSLContext(DEFAULT_TLS_VERSION)
    context.verify_mode = (
        ssl.CERT_NONE if disable_ssl_certificate_validation else ssl.CERT_REQUIRED
    )

    # SSLContext.maximum_version and SSLContext.minimum_version are python 3.7+.
    # source: https://docs.python.org/3/library/ssl.html#ssl.SSLContext.maximum_version
    if maximum_version is not None:
        if hasattr(context, "maximum_version"):
            context.maximum_version = getattr(ssl.TLSVersion, maximum_version)
        else:
            raise RuntimeError("setting tls_maximum_version requires Python 3.7 and OpenSSL 1.1 or newer")
    if minimum_version is not None:
        if hasattr(context, "minimum_version"):
            context.minimum_version = getattr(ssl.TLSVersion, minimum_version)
        else:
            raise RuntimeError("setting tls_minimum_version requires Python 3.7 and OpenSSL 1.1 or newer")

    # check_hostname requires python 3.4+
    # we will perform the equivalent in HTTPSConnectionWithTimeout.connect() by calling ssl.match_hostname
    # if check_hostname is not supported.
    if hasattr(context, "check_hostname"):
        context.check_hostname = not disable_ssl_certificate_validation

    context.load_verify_locations(ca_certs)

    if cert_file:
        context.load_cert_chain(cert_file, key_file)

    return context 
开发者ID:remg427,项目名称:misp42splunk,代码行数:39,代码来源:__init__.py

示例3: handle_data

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def handle_data(self):
		sock_data: bytes = b''
		try:
			# 16384 is 2^14 self.socket is a ssl wrapped socket.
			# Perhaps this value was chosen as the largest value that could be received [1] to avoid having to loop
			# until a new line is reached.
			# However, the Python docs [2] say:
			# "For best match with hardware and network realities, the value of bufsize should be a relatively
			# small power of 2, for example, 4096."
			# This should probably be changed in the future.
			# See also transport.py handle_server_data in class TCPTransport.
			# [1] https://stackoverflow.com/a/24870153/
			# [2] https://docs.python.org/3.7/library/socket.html#socket.socket.recv
			buffSize = 16384
			sock_data = self.socket.recv(buffSize)
		except:
			self.close()
			return
		if not sock_data: #Disconnect
			self.close()
			return
		data = self.buffer + sock_data
		if b'\n' not in data:
			self.buffer = data
			return
		self.buffer = b""
		while b'\n' in data:
			line, sep, data = data.partition(b'\n')
			try:
				self.parse(line)
			except ValueError:
				self.close()
				return
		self.buffer += data 
开发者ID:NVDARemote,项目名称:NVDARemote,代码行数:36,代码来源:server.py

示例4: connect_ex

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def connect_ex(self, dest_pair):
        """ https://docs.python.org/3/library/socket.html#socket.socket.connect_ex
        Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as "host not found" can still raise exceptions).
        """
        try:
            self.connect(dest_pair, catch_errors=True)
            return 0
        except OSError as e:
            # If the error is numeric (socket errors are numeric), then return number as
            # connect_ex expects. Otherwise raise the error again (socket timeout for example)
            if e.errno:
                return e.errno
            else:
                raise 
开发者ID:wkeeling,项目名称:selenium-wire,代码行数:16,代码来源:socks.py

示例5: connect_ex

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def connect_ex(self, dest_pair):
        """ https://docs.python.org/3/library/socket.html#socket.socket.connect_ex
        Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as "host not found" can still raise exceptions).
        """
        try:
            self.connect(dest_pair, catch_errors=True)
            return 0
        except OSError as e:
            # If the error is numeric (socket errors are numeric), then return number as 
            # connect_ex expects. Otherwise raise the error again (socket timeout for example)
            if e.errno:
                return e.errno
            else:
                raise 
开发者ID:pepsik-kiev,项目名称:HTTPAceProxy,代码行数:16,代码来源:socks.py

示例6: _start

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def _start(self):
        # memoryview act as an recv buffer
        # refer https://docs.python.org/3/library/stdtypes.html#memoryview
        buff = memoryview(bytearray(RECV_BUFFER_SIZE))
        while True:
            if not self.conn_rd:
                # sleep if there is no connections
                time.sleep(0.06)
                continue

            # blocks until there is socket(s) ready for .recv
            # notice: sockets which were closed by remote,
            #   are also regarded as read-ready by select()
            r, w, e = select.select(self.conn_rd, [], [], 0.5)

            for s in r:  # iter every read-ready or closed sockets
                try:
                    # here, we use .recv_into() instead of .recv()
                    #   recv data directly into the pre-allocated buffer
                    #   to avoid many unnecessary malloc()
                    # see https://docs.python.org/3/library/socket.html#socket.socket.recv_into
                    rec_len = s.recv_into(buff, RECV_BUFFER_SIZE)
                except:
                    # unable to read, in most cases, it's due to socket close
                    self._rd_shutdown(s)
                    continue

                if not rec_len:
                    # read zero size, closed or shutdowned socket
                    self._rd_shutdown(s)
                    continue

                try:
                    # send data, we use `buff[:rec_len]` slice because
                    #   only the front of buff is filled
                    self.map[s].send(buff[:rec_len])
                except:
                    # unable to send, close connection
                    self._rd_shutdown(s)
                    continue 
开发者ID:mxdg,项目名称:passbytcp,代码行数:42,代码来源:common_func.py

示例7: connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def connect(self, transport_timeout_s=None):
        """Create a socket connection to the device.

        Parameters
        ----------
        transport_timeout_s : float, None
            Set the timeout on the socket instance

        """
        timeout = self._default_transport_timeout_s if transport_timeout_s is None else transport_timeout_s
        self._connection = socket.create_connection((self._host, self._port), timeout=timeout)
        if timeout:
            # Put the socket in non-blocking mode
            # https://docs.python.org/3/library/socket.html#socket.socket.settimeout
            self._connection.setblocking(False) 
开发者ID:JeffLIrion,项目名称:adb_shell,代码行数:17,代码来源:tcp_transport.py

示例8: connect_ex

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def connect_ex(self, dest_pair):
        """ https://docs.python.org/3/library/socket.html#socket.socket.connect_ex
        Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as "host not found" can still raise exceptions).
        """
        try:
            self.connect(dest_pair, catch_errors=True)
            return 0
        except OSError as e:
            if e.errno:
                return e.errno
            else:
                raise 
开发者ID:Nemiroff,项目名称:script.elementum.nova,代码行数:14,代码来源:socks.py

示例9: _start

# 需要导入模块: import socket [as 别名]
# 或者: from socket import html [as 别名]
def _start(self):
        # memoryview act as an recv buffer
        # refer https://docs.python.org/3/library/stdtypes.html#memoryview
        buff = memoryview(bytearray(RECV_BUFFER_SIZE))
        while True:
            if not self.conn_rd:
                # sleep if there is no connections
                time.sleep(0.06)
                continue

            # blocks until there is socket(s) ready for .recv
            # notice: sockets which were closed by remote,
            #   are also regarded as read-ready by select()
            r, w, e = select.select(self.conn_rd, [], [], 0.5)

            for s in r:  # iter every read-ready or closed sockets
                try:
                    # here, we use .recv_into() instead of .recv()
                    #   recv data directly into the pre-allocated buffer
                    #   to avoid many unnecessary malloc()
                    # see https://docs.python.org/3/library/socket.html#socket.socket.recv_into
                    rec_len = s.recv_into(buff, RECV_BUFFER_SIZE)

                    # agre = "http"
                    # url = agre + '://' + heads['Host']
                    # heads = httphead(buff.tobytes().decode('utf-8'))
                    # logging.info("recv head:{}".format(heads))
                except Exception as e:
                    # unable to read, in most cases, it's due to socket close
                    self._rd_shutdown(s)
                    continue

                if not rec_len:
                    # read zero size, closed or shutdowned socket
                    self._rd_shutdown(s)
                    continue

                try:
                    # send data, we use `buff[:rec_len]` slice because
                    #   only the front of buff is filled
                    self.map[s].send(buff[:rec_len])
                except Exception as e:
                    # unable to send, close connection
                    self._rd_shutdown(s)
                    continue 
开发者ID:mxdg,项目名称:passbytcp,代码行数:47,代码来源:common_func.py


注:本文中的socket.html方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。