Python socket.html() Examples
The following are 10
code examples of socket.html().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: conftest.py From integrations-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #2
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
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
Example #3
Source File: server.py From NVDARemote with GNU General Public License v2.0 | 5 votes |
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
Example #4
Source File: socks.py From selenium-wire with MIT License | 5 votes |
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
Example #5
Source File: socks.py From HTTPAceProxy with GNU General Public License v3.0 | 5 votes |
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
Example #6
Source File: socks.py From script.elementum.burst with Do What The F*ck You Want To Public License | 5 votes |
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
Example #7
Source File: common_func.py From passbytcp with MIT License | 5 votes |
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
Example #8
Source File: tcp_transport.py From adb_shell with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: socks.py From script.elementum.nova with Do What The F*ck You Want To Public License | 5 votes |
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
Example #10
Source File: common_func.py From passbytcp with MIT License | 4 votes |
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