Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3....
File: socket.py
[500] Fix | Delete
def detach(self):
[501] Fix | Delete
"""detach() -> file descriptor
[502] Fix | Delete
[503] Fix | Delete
Close the socket object without closing the underlying file descriptor.
[504] Fix | Delete
The object cannot be used after this call, but the file descriptor
[505] Fix | Delete
can be reused for other purposes. The file descriptor is returned.
[506] Fix | Delete
"""
[507] Fix | Delete
self._closed = True
[508] Fix | Delete
return super().detach()
[509] Fix | Delete
[510] Fix | Delete
@property
[511] Fix | Delete
def family(self):
[512] Fix | Delete
"""Read-only access to the address family for this socket.
[513] Fix | Delete
"""
[514] Fix | Delete
return _intenum_converter(super().family, AddressFamily)
[515] Fix | Delete
[516] Fix | Delete
@property
[517] Fix | Delete
def type(self):
[518] Fix | Delete
"""Read-only access to the socket type.
[519] Fix | Delete
"""
[520] Fix | Delete
return _intenum_converter(super().type, SocketKind)
[521] Fix | Delete
[522] Fix | Delete
if os.name == 'nt':
[523] Fix | Delete
def get_inheritable(self):
[524] Fix | Delete
return os.get_handle_inheritable(self.fileno())
[525] Fix | Delete
def set_inheritable(self, inheritable):
[526] Fix | Delete
os.set_handle_inheritable(self.fileno(), inheritable)
[527] Fix | Delete
else:
[528] Fix | Delete
def get_inheritable(self):
[529] Fix | Delete
return os.get_inheritable(self.fileno())
[530] Fix | Delete
def set_inheritable(self, inheritable):
[531] Fix | Delete
os.set_inheritable(self.fileno(), inheritable)
[532] Fix | Delete
get_inheritable.__doc__ = "Get the inheritable flag of the socket"
[533] Fix | Delete
set_inheritable.__doc__ = "Set the inheritable flag of the socket"
[534] Fix | Delete
[535] Fix | Delete
def fromfd(fd, family, type, proto=0):
[536] Fix | Delete
""" fromfd(fd, family, type[, proto]) -> socket object
[537] Fix | Delete
[538] Fix | Delete
Create a socket object from a duplicate of the given file
[539] Fix | Delete
descriptor. The remaining arguments are the same as for socket().
[540] Fix | Delete
"""
[541] Fix | Delete
nfd = dup(fd)
[542] Fix | Delete
return socket(family, type, proto, nfd)
[543] Fix | Delete
[544] Fix | Delete
if hasattr(_socket.socket, "share"):
[545] Fix | Delete
def fromshare(info):
[546] Fix | Delete
""" fromshare(info) -> socket object
[547] Fix | Delete
[548] Fix | Delete
Create a socket object from the bytes object returned by
[549] Fix | Delete
socket.share(pid).
[550] Fix | Delete
"""
[551] Fix | Delete
return socket(0, 0, 0, info)
[552] Fix | Delete
__all__.append("fromshare")
[553] Fix | Delete
[554] Fix | Delete
if hasattr(_socket, "socketpair"):
[555] Fix | Delete
[556] Fix | Delete
def socketpair(family=None, type=SOCK_STREAM, proto=0):
[557] Fix | Delete
"""socketpair([family[, type[, proto]]]) -> (socket object, socket object)
[558] Fix | Delete
[559] Fix | Delete
Create a pair of socket objects from the sockets returned by the platform
[560] Fix | Delete
socketpair() function.
[561] Fix | Delete
The arguments are the same as for socket() except the default family is
[562] Fix | Delete
AF_UNIX if defined on the platform; otherwise, the default is AF_INET.
[563] Fix | Delete
"""
[564] Fix | Delete
if family is None:
[565] Fix | Delete
try:
[566] Fix | Delete
family = AF_UNIX
[567] Fix | Delete
except NameError:
[568] Fix | Delete
family = AF_INET
[569] Fix | Delete
a, b = _socket.socketpair(family, type, proto)
[570] Fix | Delete
a = socket(family, type, proto, a.detach())
[571] Fix | Delete
b = socket(family, type, proto, b.detach())
[572] Fix | Delete
return a, b
[573] Fix | Delete
[574] Fix | Delete
else:
[575] Fix | Delete
[576] Fix | Delete
# Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
[577] Fix | Delete
def socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
[578] Fix | Delete
if family == AF_INET:
[579] Fix | Delete
host = _LOCALHOST
[580] Fix | Delete
elif family == AF_INET6:
[581] Fix | Delete
host = _LOCALHOST_V6
[582] Fix | Delete
else:
[583] Fix | Delete
raise ValueError("Only AF_INET and AF_INET6 socket address families "
[584] Fix | Delete
"are supported")
[585] Fix | Delete
if type != SOCK_STREAM:
[586] Fix | Delete
raise ValueError("Only SOCK_STREAM socket type is supported")
[587] Fix | Delete
if proto != 0:
[588] Fix | Delete
raise ValueError("Only protocol zero is supported")
[589] Fix | Delete
[590] Fix | Delete
# We create a connected TCP socket. Note the trick with
[591] Fix | Delete
# setblocking(False) that prevents us from having to create a thread.
[592] Fix | Delete
lsock = socket(family, type, proto)
[593] Fix | Delete
try:
[594] Fix | Delete
lsock.bind((host, 0))
[595] Fix | Delete
lsock.listen()
[596] Fix | Delete
# On IPv6, ignore flow_info and scope_id
[597] Fix | Delete
addr, port = lsock.getsockname()[:2]
[598] Fix | Delete
csock = socket(family, type, proto)
[599] Fix | Delete
try:
[600] Fix | Delete
csock.setblocking(False)
[601] Fix | Delete
try:
[602] Fix | Delete
csock.connect((addr, port))
[603] Fix | Delete
except (BlockingIOError, InterruptedError):
[604] Fix | Delete
pass
[605] Fix | Delete
csock.setblocking(True)
[606] Fix | Delete
ssock, _ = lsock.accept()
[607] Fix | Delete
except:
[608] Fix | Delete
csock.close()
[609] Fix | Delete
raise
[610] Fix | Delete
finally:
[611] Fix | Delete
lsock.close()
[612] Fix | Delete
return (ssock, csock)
[613] Fix | Delete
__all__.append("socketpair")
[614] Fix | Delete
[615] Fix | Delete
socketpair.__doc__ = """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
[616] Fix | Delete
Create a pair of socket objects from the sockets returned by the platform
[617] Fix | Delete
socketpair() function.
[618] Fix | Delete
The arguments are the same as for socket() except the default family is AF_UNIX
[619] Fix | Delete
if defined on the platform; otherwise, the default is AF_INET.
[620] Fix | Delete
"""
[621] Fix | Delete
[622] Fix | Delete
_blocking_errnos = { EAGAIN, EWOULDBLOCK }
[623] Fix | Delete
[624] Fix | Delete
class SocketIO(io.RawIOBase):
[625] Fix | Delete
[626] Fix | Delete
"""Raw I/O implementation for stream sockets.
[627] Fix | Delete
[628] Fix | Delete
This class supports the makefile() method on sockets. It provides
[629] Fix | Delete
the raw I/O interface on top of a socket object.
[630] Fix | Delete
"""
[631] Fix | Delete
[632] Fix | Delete
# One might wonder why not let FileIO do the job instead. There are two
[633] Fix | Delete
# main reasons why FileIO is not adapted:
[634] Fix | Delete
# - it wouldn't work under Windows (where you can't used read() and
[635] Fix | Delete
# write() on a socket handle)
[636] Fix | Delete
# - it wouldn't work with socket timeouts (FileIO would ignore the
[637] Fix | Delete
# timeout and consider the socket non-blocking)
[638] Fix | Delete
[639] Fix | Delete
# XXX More docs
[640] Fix | Delete
[641] Fix | Delete
def __init__(self, sock, mode):
[642] Fix | Delete
if mode not in ("r", "w", "rw", "rb", "wb", "rwb"):
[643] Fix | Delete
raise ValueError("invalid mode: %r" % mode)
[644] Fix | Delete
io.RawIOBase.__init__(self)
[645] Fix | Delete
self._sock = sock
[646] Fix | Delete
if "b" not in mode:
[647] Fix | Delete
mode += "b"
[648] Fix | Delete
self._mode = mode
[649] Fix | Delete
self._reading = "r" in mode
[650] Fix | Delete
self._writing = "w" in mode
[651] Fix | Delete
self._timeout_occurred = False
[652] Fix | Delete
[653] Fix | Delete
def readinto(self, b):
[654] Fix | Delete
"""Read up to len(b) bytes into the writable buffer *b* and return
[655] Fix | Delete
the number of bytes read. If the socket is non-blocking and no bytes
[656] Fix | Delete
are available, None is returned.
[657] Fix | Delete
[658] Fix | Delete
If *b* is non-empty, a 0 return value indicates that the connection
[659] Fix | Delete
was shutdown at the other end.
[660] Fix | Delete
"""
[661] Fix | Delete
self._checkClosed()
[662] Fix | Delete
self._checkReadable()
[663] Fix | Delete
if self._timeout_occurred:
[664] Fix | Delete
raise OSError("cannot read from timed out object")
[665] Fix | Delete
while True:
[666] Fix | Delete
try:
[667] Fix | Delete
return self._sock.recv_into(b)
[668] Fix | Delete
except timeout:
[669] Fix | Delete
self._timeout_occurred = True
[670] Fix | Delete
raise
[671] Fix | Delete
except error as e:
[672] Fix | Delete
if e.args[0] in _blocking_errnos:
[673] Fix | Delete
return None
[674] Fix | Delete
raise
[675] Fix | Delete
[676] Fix | Delete
def write(self, b):
[677] Fix | Delete
"""Write the given bytes or bytearray object *b* to the socket
[678] Fix | Delete
and return the number of bytes written. This can be less than
[679] Fix | Delete
len(b) if not all data could be written. If the socket is
[680] Fix | Delete
non-blocking and no bytes could be written None is returned.
[681] Fix | Delete
"""
[682] Fix | Delete
self._checkClosed()
[683] Fix | Delete
self._checkWritable()
[684] Fix | Delete
try:
[685] Fix | Delete
return self._sock.send(b)
[686] Fix | Delete
except error as e:
[687] Fix | Delete
# XXX what about EINTR?
[688] Fix | Delete
if e.args[0] in _blocking_errnos:
[689] Fix | Delete
return None
[690] Fix | Delete
raise
[691] Fix | Delete
[692] Fix | Delete
def readable(self):
[693] Fix | Delete
"""True if the SocketIO is open for reading.
[694] Fix | Delete
"""
[695] Fix | Delete
if self.closed:
[696] Fix | Delete
raise ValueError("I/O operation on closed socket.")
[697] Fix | Delete
return self._reading
[698] Fix | Delete
[699] Fix | Delete
def writable(self):
[700] Fix | Delete
"""True if the SocketIO is open for writing.
[701] Fix | Delete
"""
[702] Fix | Delete
if self.closed:
[703] Fix | Delete
raise ValueError("I/O operation on closed socket.")
[704] Fix | Delete
return self._writing
[705] Fix | Delete
[706] Fix | Delete
def seekable(self):
[707] Fix | Delete
"""True if the SocketIO is open for seeking.
[708] Fix | Delete
"""
[709] Fix | Delete
if self.closed:
[710] Fix | Delete
raise ValueError("I/O operation on closed socket.")
[711] Fix | Delete
return super().seekable()
[712] Fix | Delete
[713] Fix | Delete
def fileno(self):
[714] Fix | Delete
"""Return the file descriptor of the underlying socket.
[715] Fix | Delete
"""
[716] Fix | Delete
self._checkClosed()
[717] Fix | Delete
return self._sock.fileno()
[718] Fix | Delete
[719] Fix | Delete
@property
[720] Fix | Delete
def name(self):
[721] Fix | Delete
if not self.closed:
[722] Fix | Delete
return self.fileno()
[723] Fix | Delete
else:
[724] Fix | Delete
return -1
[725] Fix | Delete
[726] Fix | Delete
@property
[727] Fix | Delete
def mode(self):
[728] Fix | Delete
return self._mode
[729] Fix | Delete
[730] Fix | Delete
def close(self):
[731] Fix | Delete
"""Close the SocketIO object. This doesn't close the underlying
[732] Fix | Delete
socket, except if all references to it have disappeared.
[733] Fix | Delete
"""
[734] Fix | Delete
if self.closed:
[735] Fix | Delete
return
[736] Fix | Delete
io.RawIOBase.close(self)
[737] Fix | Delete
self._sock._decref_socketios()
[738] Fix | Delete
self._sock = None
[739] Fix | Delete
[740] Fix | Delete
[741] Fix | Delete
def getfqdn(name=''):
[742] Fix | Delete
"""Get fully qualified domain name from name.
[743] Fix | Delete
[744] Fix | Delete
An empty argument is interpreted as meaning the local host.
[745] Fix | Delete
[746] Fix | Delete
First the hostname returned by gethostbyaddr() is checked, then
[747] Fix | Delete
possibly existing aliases. In case no FQDN is available, hostname
[748] Fix | Delete
from gethostname() is returned.
[749] Fix | Delete
"""
[750] Fix | Delete
name = name.strip()
[751] Fix | Delete
if not name or name == '0.0.0.0':
[752] Fix | Delete
name = gethostname()
[753] Fix | Delete
try:
[754] Fix | Delete
hostname, aliases, ipaddrs = gethostbyaddr(name)
[755] Fix | Delete
except error:
[756] Fix | Delete
pass
[757] Fix | Delete
else:
[758] Fix | Delete
aliases.insert(0, hostname)
[759] Fix | Delete
for name in aliases:
[760] Fix | Delete
if '.' in name:
[761] Fix | Delete
break
[762] Fix | Delete
else:
[763] Fix | Delete
name = hostname
[764] Fix | Delete
return name
[765] Fix | Delete
[766] Fix | Delete
[767] Fix | Delete
_GLOBAL_DEFAULT_TIMEOUT = object()
[768] Fix | Delete
[769] Fix | Delete
def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
[770] Fix | Delete
source_address=None):
[771] Fix | Delete
"""Connect to *address* and return the socket object.
[772] Fix | Delete
[773] Fix | Delete
Convenience function. Connect to *address* (a 2-tuple ``(host,
[774] Fix | Delete
port)``) and return the socket object. Passing the optional
[775] Fix | Delete
*timeout* parameter will set the timeout on the socket instance
[776] Fix | Delete
before attempting to connect. If no *timeout* is supplied, the
[777] Fix | Delete
global default timeout setting returned by :func:`getdefaulttimeout`
[778] Fix | Delete
is used. If *source_address* is set it must be a tuple of (host, port)
[779] Fix | Delete
for the socket to bind as a source address before making the connection.
[780] Fix | Delete
A host of '' or port 0 tells the OS to use the default.
[781] Fix | Delete
"""
[782] Fix | Delete
[783] Fix | Delete
host, port = address
[784] Fix | Delete
err = None
[785] Fix | Delete
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
[786] Fix | Delete
af, socktype, proto, canonname, sa = res
[787] Fix | Delete
sock = None
[788] Fix | Delete
try:
[789] Fix | Delete
sock = socket(af, socktype, proto)
[790] Fix | Delete
if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
[791] Fix | Delete
sock.settimeout(timeout)
[792] Fix | Delete
if source_address:
[793] Fix | Delete
sock.bind(source_address)
[794] Fix | Delete
sock.connect(sa)
[795] Fix | Delete
# Break explicitly a reference cycle
[796] Fix | Delete
err = None
[797] Fix | Delete
return sock
[798] Fix | Delete
[799] Fix | Delete
except error as _:
[800] Fix | Delete
err = _
[801] Fix | Delete
if sock is not None:
[802] Fix | Delete
sock.close()
[803] Fix | Delete
[804] Fix | Delete
if err is not None:
[805] Fix | Delete
try:
[806] Fix | Delete
raise err
[807] Fix | Delete
finally:
[808] Fix | Delete
# Break explicitly a reference cycle
[809] Fix | Delete
err = None
[810] Fix | Delete
else:
[811] Fix | Delete
raise error("getaddrinfo returns an empty list")
[812] Fix | Delete
[813] Fix | Delete
[814] Fix | Delete
def has_dualstack_ipv6():
[815] Fix | Delete
"""Return True if the platform supports creating a SOCK_STREAM socket
[816] Fix | Delete
which can handle both AF_INET and AF_INET6 (IPv4 / IPv6) connections.
[817] Fix | Delete
"""
[818] Fix | Delete
if not has_ipv6 \
[819] Fix | Delete
or not hasattr(_socket, 'IPPROTO_IPV6') \
[820] Fix | Delete
or not hasattr(_socket, 'IPV6_V6ONLY'):
[821] Fix | Delete
return False
[822] Fix | Delete
try:
[823] Fix | Delete
with socket(AF_INET6, SOCK_STREAM) as sock:
[824] Fix | Delete
sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
[825] Fix | Delete
return True
[826] Fix | Delete
except error:
[827] Fix | Delete
return False
[828] Fix | Delete
[829] Fix | Delete
[830] Fix | Delete
def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
[831] Fix | Delete
dualstack_ipv6=False):
[832] Fix | Delete
"""Convenience function which creates a SOCK_STREAM type socket
[833] Fix | Delete
bound to *address* (a 2-tuple (host, port)) and return the socket
[834] Fix | Delete
object.
[835] Fix | Delete
[836] Fix | Delete
*family* should be either AF_INET or AF_INET6.
[837] Fix | Delete
*backlog* is the queue size passed to socket.listen().
[838] Fix | Delete
*reuse_port* dictates whether to use the SO_REUSEPORT socket option.
[839] Fix | Delete
*dualstack_ipv6*: if true and the platform supports it, it will
[840] Fix | Delete
create an AF_INET6 socket able to accept both IPv4 or IPv6
[841] Fix | Delete
connections. When false it will explicitly disable this option on
[842] Fix | Delete
platforms that enable it by default (e.g. Linux).
[843] Fix | Delete
[844] Fix | Delete
>>> with create_server(('', 8000)) as server:
[845] Fix | Delete
... while True:
[846] Fix | Delete
... conn, addr = server.accept()
[847] Fix | Delete
... # handle new connection
[848] Fix | Delete
"""
[849] Fix | Delete
if reuse_port and not hasattr(_socket, "SO_REUSEPORT"):
[850] Fix | Delete
raise ValueError("SO_REUSEPORT not supported on this platform")
[851] Fix | Delete
if dualstack_ipv6:
[852] Fix | Delete
if not has_dualstack_ipv6():
[853] Fix | Delete
raise ValueError("dualstack_ipv6 not supported on this platform")
[854] Fix | Delete
if family != AF_INET6:
[855] Fix | Delete
raise ValueError("dualstack_ipv6 requires AF_INET6 family")
[856] Fix | Delete
sock = socket(family, SOCK_STREAM)
[857] Fix | Delete
try:
[858] Fix | Delete
# Note about Windows. We don't set SO_REUSEADDR because:
[859] Fix | Delete
# 1) It's unnecessary: bind() will succeed even in case of a
[860] Fix | Delete
# previous closed socket on the same address and still in
[861] Fix | Delete
# TIME_WAIT state.
[862] Fix | Delete
# 2) If set, another socket is free to bind() on the same
[863] Fix | Delete
# address, effectively preventing this one from accepting
[864] Fix | Delete
# connections. Also, it may set the process in a state where
[865] Fix | Delete
# it'll no longer respond to any signals or graceful kills.
[866] Fix | Delete
# See: msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx
[867] Fix | Delete
if os.name not in ('nt', 'cygwin') and \
[868] Fix | Delete
hasattr(_socket, 'SO_REUSEADDR'):
[869] Fix | Delete
try:
[870] Fix | Delete
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
[871] Fix | Delete
except error:
[872] Fix | Delete
# Fail later on bind(), for platforms which may not
[873] Fix | Delete
# support this option.
[874] Fix | Delete
pass
[875] Fix | Delete
if reuse_port:
[876] Fix | Delete
sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
[877] Fix | Delete
if has_ipv6 and family == AF_INET6:
[878] Fix | Delete
if dualstack_ipv6:
[879] Fix | Delete
sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
[880] Fix | Delete
elif hasattr(_socket, "IPV6_V6ONLY") and \
[881] Fix | Delete
hasattr(_socket, "IPPROTO_IPV6"):
[882] Fix | Delete
sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 1)
[883] Fix | Delete
try:
[884] Fix | Delete
sock.bind(address)
[885] Fix | Delete
except error as err:
[886] Fix | Delete
msg = '%s (while attempting to bind on address %r)' % \
[887] Fix | Delete
(err.strerror, address)
[888] Fix | Delete
raise error(err.errno, msg) from None
[889] Fix | Delete
if backlog is None:
[890] Fix | Delete
sock.listen()
[891] Fix | Delete
else:
[892] Fix | Delete
sock.listen(backlog)
[893] Fix | Delete
return sock
[894] Fix | Delete
except error:
[895] Fix | Delete
sock.close()
[896] Fix | Delete
raise
[897] Fix | Delete
[898] Fix | Delete
[899] Fix | Delete
def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
[900] Fix | Delete
"""Resolve host and port into list of address info entries.
[901] Fix | Delete
[902] Fix | Delete
Translate the host/port argument into a sequence of 5-tuples that contain
[903] Fix | Delete
all the necessary arguments for creating a socket connected to that service.
[904] Fix | Delete
host is a domain name, a string representation of an IPv4/v6 address or
[905] Fix | Delete
None. port is a string service name such as 'http', a numeric port number or
[906] Fix | Delete
None. By passing None as the value of host and port, you can pass NULL to
[907] Fix | Delete
the underlying C API.
[908] Fix | Delete
[909] Fix | Delete
The family, type and proto arguments can be optionally specified in order to
[910] Fix | Delete
narrow the list of addresses returned. Passing zero as a value for each of
[911] Fix | Delete
these arguments selects the full range of results.
[912] Fix | Delete
"""
[913] Fix | Delete
# We override this function since we want to translate the numeric family
[914] Fix | Delete
# and socket type values to enum constants.
[915] Fix | Delete
addrlist = []
[916] Fix | Delete
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
[917] Fix | Delete
af, socktype, proto, canonname, sa = res
[918] Fix | Delete
addrlist.append((_intenum_converter(af, AddressFamily),
[919] Fix | Delete
_intenum_converter(socktype, SocketKind),
[920] Fix | Delete
proto, canonname, sa))
[921] Fix | Delete
return addrlist
[922] Fix | Delete
[923] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function