本文整理匯總了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
示例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
示例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
示例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
示例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
示例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
示例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)
示例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
示例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