Файловый менеджер - Редактировать - /opt/imh-python/lib/python3.9/site-packages/twisted/internet/__pycache__/tcp.cpython-39.pyc
Ðазад
a k�h�� � @ s& d Z ddlZddlZddlZddlZddlmZmZmZm Z ddl mZmZ ddl Z ddlZddlmZmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZ dd lmZ z$dd lm Z!m"Z#m$Z% ddlm&Z& W nB e'�y eZ&G dd � d �Z#G dd� d�Z!G dd� d�Z%Y n0 edk�rve(� Z)ddl*m+Z,m-Z.m/Z0m1Z2m3Z4m5Z6m7Z8 e(� Z9e(� Z:e8Z;ddl*m<Z= ddl>m?Z@ n�ddl*m)Z) ddl*m0Z0 ddl*m8Z8 ddl*m.Z. ddl*m,Z, ddl*m2Z2 ddl*m6Z6 ddl*m4Z4 ddl*m9Z9 ddl*m:Z: dd l*m;Z; dd!l*m=Z= dd"lm@Z@ dd#l*mAZA dd$lBmCZCmDZDmEZEmFZFmGZGmHZH dd%lImJZJ dd&lKmLZL dd'lMmNZN dd(lmOZOmPZPmQZQ dd)lRmSZS eTed*d�ZUd+d,� ZVd-d.� ZWd/d0� ZXG d1d2� d2�ZYG d3d4� d4�ZZee&ee�G d5d6� d6e#eCj[eYeZ��Z\G d7d8� d8�Z]G d9d:� d:e]e!e\�Z^ej_eUB Z`d;d<� ZaG d=d>� d>�ZbG d?d@� d@ebe^�ZcG dAdB� dBe%e\�ZdG dCdD� dDe�ZeG dEdF� dFejL�Zfeee�e jgdGdH�G dIdJ� dJ���Zheee�G dKdL� dL��Ziedk�r�ei� ZjnehdMdN� �Zje4e6e9e:e=fZke jgdGdH�G dOdP� dP��ZldQdR� Zmee�G dSdT� dTeEjneY��ZoG dUdV� dVeEjp�ZqdS )Wzt Various asynchronous TCP/IP classes. End users shouldn't use this module directly - use the reactor APIs instead. � N)�Callable�ClassVar�List�Optional)� Interface�implementer)�IHalfCloseableProtocol�IListeningPort� ISystemHandle� ITCPTransport)�ILogObserver�LogEvent�Logger)� deprecate�versions)� lazyByteSlice)�platformType)�ClientMixin�ConnectionMixin�ServerMixin)� ITLSTransportc @ s e Zd ZdZdS )�_TLSConnectionMixinFN)�__name__� __module__�__qualname__ZTLS� r r ��/root/rpmbuild/BUILDROOT/imh-python39-modules-3.9.7-92.el8.x86_64/opt/imh-python/lib/python3.9/site-packages/twisted/internet/tcp.pyr 0 s r c @ s e Zd ZdS )�_TLSClientMixinN�r r r r r r r r 3 s r c @ s e Zd ZdS )�_TLSServerMixinNr r r r r r 6 s r �win32)�WSAEALREADY�WSAEINPROGRESS� WSAEINVAL� WSAEISCONN� WSAEMFILE� WSAENOBUFS�WSAEWOULDBLOCK)� WSAECONNRESET)�formatError)�EPERM)�EINVAL)�EWOULDBLOCK)�EINPROGRESS)�EALREADY)�EISCONN)�ENOBUFS)�EMFILE)�ENFILE)�ENOMEM)�EAGAIN)�ECONNABORTED)�strerror)� errorcode)�abstract�address�base�error�fdesc�main)�CannotListenError)�Protocol)� deferLater)�failure�log�reflect)�untilConcludes�AI_NUMERICSERVc C sN t | �dkr>t�| tjtjB �d }t|gt| dd� � �S | dd� S dS )ai Return a 2-tuple of socket IP and port for IPv4 and a 4-tuple of socket IP, port, flowInfo, and scopeID for IPv6. For IPv6, it returns the interface portion (the part after the %) as a part of the IPv6 address, which Python 3.7+ does not include. @param addr: A 2-tuple for IPv4 information or a 4-tuple for IPv6 information. � r � N� )�len�socket�getnameinfo�NI_NUMERICHOST�NI_NUMERICSERV�tuple�list)�addr�hostr r r �_getrealnamen s �rR c C s t | �� �S �z See L{_getrealname}. )rR �getpeername��sktr r r �_getpeername� s rW c C s t | �� �S rS )rR �getsocknamerU r r r �_getsockname� s rY c @ s e Zd ZdZdZdd� ZdS )� _SocketCloserz� @ivar _shouldShutdown: Set to C{True} if C{shutdown} should be called before calling C{close} on the underlying socket. @type _shouldShutdown: C{bool} Tc C sv | j }z8|r| jr<|�d� n| j �t jt jt�ddd�� W n tyP Y n0 z|� � W n typ Y n0 d S )NrH �iirG r ) rJ �_shouldShutdown�shutdown� setsockopt� SOL_SOCKET� SO_LINGER�struct�pack�OSError�close)�selfZorderlyrV r r r �_closeSocket� s �z_SocketCloser._closeSocketN)r r r �__doc__r\ rf r r r r rZ � s rZ c @ s e Zd ZdZdZdd� ZdS )�_AbortingMixinz� Common implementation of C{abortConnection}. @ivar _aborting: Set to C{True} when C{abortConnection} is called. @type _aborting: C{bool} Fc C sZ | j s| jrdS d| _| �� | �� dd� | _dd� | _| j�d| jt � t�� �� dS )zf Aborts the connection immediately, dropping any buffered data. @since: 11.1 NTc _ s d S �Nr ��args�kwargsr r r �<lambda>� � z0_AbortingMixin.abortConnection.<locals>.<lambda>c _ s d S ri r rj r r r rm � rn r ) �disconnected� _aborting�stopReading�stopWriting�doRead�doWrite�reactor� callLater�connectionLostrA �Failurer; �ConnectionAborted�re r r r �abortConnection� s �z_AbortingMixin.abortConnectionN)r r r rg rp r{ r r r r rh � s rh c @ s~ e Zd ZdZddd�Zdd� Zdd� Zd d � Zdd� Zd d� Z dd� Z dd� ZdZdd� Z dd� Zdd� Zdd� Zdd� ZdS )� Connectiona Superclass of all socket-based FileDescriptors. This is an abstract superclass of all objects which represent a TCP/IP connection based socket. @ivar logstr: prefix used when logging events related to this connection. @type logstr: C{str} Nc C s4 t jj| |d� || _| j�d� |j| _|| _d S )N�ru r )r8 �FileDescriptor�__init__rJ �setblocking�fileno�protocol)re rV r� ru r r r r � s zConnection.__init__c C s | j S )z&Return the socket for this connection.)rJ rz r r r � getHandle� s zConnection.getHandlec C sp z| j �| j�}W nR tyd } z:|jd tkr>W Y d}~dS tjW Y d}~S W Y d}~n d}~0 0 | �|�S )a\ Calls self.protocol.dataReceived with all available data. This reads up to self.bufferSize bytes of data from its socket, then calls self.dataReceived(data) to process it. If the connection is not lost through an error in the physical recv(), this function will return the result of the dataReceived call. r N) rJ �recvZ bufferSizerc rk r, r= �CONNECTION_LOST� _dataReceived)re �data�ser r r rs � s (zConnection.doReadc C sV |s t jS | j�|�}|d urR| jj}d}tj|t�dddd�|d�}t�||� |S )NzPReturning a value other than None from %(fqpn)s is deprecated since %(version)s.ZTwisted� r )�format) r= �CONNECTION_DONEr� ZdataReceivedr ZgetDeprecationWarningStringr �VersionZwarnAboutFunction)re r� �rvalZoffenderZ warningFormatZ warningStringr r r r� � s ��zConnection._dataReceivedc C sz t |d| j�}zt| jj|�W S tyt } z>|jd ttfv rNW Y d}~dS t j W Y d}~S W Y d}~n d}~0 0 dS )a Write as much as possible of the given data to this TCP connection. This sends up to C{self.SEND_LIMIT} bytes from C{data}. If the connection is lost, an exception is returned. Otherwise, the number of bytes successfully written is returned. r N)r Z SEND_LIMITrD rJ �sendrc rk r, r0 r= r� )re r� ZlimitedDatar� r r r � writeSomeData s zConnection.writeSomeDatac C sr z| j �d� W n ty" Y n0 t| jd �}|rnz|�� W n, tyl t�� }t � � | �|� Y n0 d S �NrG )rJ r] rc r r� ZwriteConnectionLost� BaseExceptionrA rx rB �errrw )re �p�fr r r �_closeWriteConnection s z Connection._closeWriteConnectionc C sV t | jd �}|rHz|�� W qR tyD t�� | �t�� � Y qR0 n | �|� d S ri ) r r� �readConnectionLostr� rB r� rw rA rx )re �reasonr� r r r r� , s zConnection.readConnectionLostc C sP t | d�sdS tj�| |� | �|�tj� � | j}| `| ` | ` |�|� dS )z-See abstract.FileDescriptor.connectionLost().rJ N)�hasattrr8 r~ rw rf �checkr; ry r� rJ r� )re r� r� r r r rw 7 s zConnection.connectionLostZ Uninitializedc C s | j S )z<Return the prefix to log with when I own the logging thread.)�logstrrz r r r � logPrefixJ s zConnection.logPrefixc C s t | j�tjtj��S ri )�boolrJ � getsockopt�IPPROTO_TCP�TCP_NODELAYrz r r r � getTcpNoDelayN s zConnection.getTcpNoDelayc C s | j �t jt j|� d S ri )rJ r^ r� r� �re �enabledr r r � setTcpNoDelayQ s zConnection.setTcpNoDelayc C s t | j�tjtj��S ri )r� rJ r� r_ �SO_KEEPALIVErz r r r �getTcpKeepAliveT s zConnection.getTcpKeepAlivec C s | j �t jt j|� d S ri )rJ r^ r_ r� r� r r r �setTcpKeepAliveW s zConnection.setTcpKeepAlive)N)r r r rg r r� rs r� r� r� r� rw r� r� r� r� r� r� r r r r r| � s r| c @ sL e Zd ZdZejZejZdd� Z dd� Z dd� Zdd � Zd d� Z dd � ZdS )�_BaseBaseClienta� Code shared with other (non-POSIX) reactors for management of general outgoing connections. Requirements upon subclasses are documented as instance variables rather than abstract methods, in order to avoid MRO confusion, since this base is mixed in to unfortunately weird and distinctive multiple-inheritance hierarchies and many of these attributes are provided by peer classes rather than descendant classes in those hierarchies. @ivar addressFamily: The address family constant (C{socket.AF_INET}, C{socket.AF_INET6}, C{socket.AF_UNIX}) of the underlying socket of this client connection. @type addressFamily: C{int} @ivar socketType: The socket type constant (C{socket.SOCK_STREAM} or C{socket.SOCK_DGRAM}) of the underlying socket. @type socketType: C{int} @ivar _requiresResolution: A flag indicating whether the address of this client will require name resolution. C{True} if the hostname of said address indicates a name that must be resolved by hostname lookup, C{False} if it indicates an IP address literal. @type _requiresResolution: C{bool} @cvar _commonConnection: Subclasses must provide this attribute, which indicates the L{Connection}-alike class to invoke C{__init__} and C{connectionLost} on. @type _commonConnection: C{type} @ivar _stopReadingAndWriting: Subclasses must implement in order to remove this transport from its reactor's notifications in response to a terminated connection attempt. @type _stopReadingAndWriting: 0-argument callable returning L{None} @ivar _closeSocket: Subclasses must implement in order to close the socket in response to a terminated connection attempt. @type _closeSocket: 1-argument callable; see L{_SocketCloser._closeSocket} @ivar _collectSocketDetails: Clean up references to the attached socket in its underlying OS resource (such as a file descriptor or file handle), as part of post connection-failure cleanup. @type _collectSocketDetails: 0-argument callable returning L{None}. @ivar reactor: The class pointed to by C{_commonConnection} should set this attribute in its constructor. @type reactor: L{twisted.internet.interfaces.IReactorTime}, L{twisted.internet.interfaces.IReactorCore}, L{twisted.internet.interfaces.IReactorFDSet} c C s8 |r$| j �| |d|� |�d|� n|�d| j|� dS )ax Called by subclasses to continue to the stage of initialization where the socket connect attempt is made. @param whenDone: A 0-argument callable to invoke once the connection is set up. This is L{None} if the connection could not be prepared due to a previous error. @param skt: The socket object to use to perform the connection. @type skt: C{socket._socketobject} @param error: The error to fail the connection with. @param reactor: The reactor to use for this client. @type reactor: L{twisted.internet.interfaces.IReactorTime} Nr )�_commonConnectionr rv �failIfNotConnected)re �whenDonerV r; ru r r r �_finishInit� s z_BaseBaseClient._finishInitc sL � j r<� j�� jd �}|�� fdd�� |�� j� j� n� �� j� dS )a� Resolve the name that was passed to this L{_BaseBaseClient}, if necessary, and then move on to attempting the connection once an address has been determined. (The connection will be attempted immediately within this function if either name resolution can be synchronous or the address was an IP address literal.) @note: You don't want to call this method from outside, as it won't do anything useful; it's just part of the connection bootstrapping process. Also, although this method is on L{_BaseBaseClient} for historical reasons, it's not used anywhere except for L{Client} itself. @return: L{None} r c s | f� j dd � S r� )rP )�nrz r r rm � rn z0_BaseBaseClient.resolveAddress.<locals>.<lambda>N)�_requiresResolutionru �resolverP ZaddCallbackZaddCallbacks�_setRealAddressr� )re �dr rz r �resolveAddress� s z_BaseBaseClient.resolveAddressc C sT t |�dkrBt�|tjtjB �d }t|gt|dd� � �| _n|| _| �� dS )a� Set the resolved address of this L{_BaseBaseClient} and initiate the connection attempt. @param address: Depending on whether this is an IPv4 or IPv6 connection attempt, a 2-tuple of C{(host, port)} or a 4-tuple of C{(host, port, flow, scope)}. At this point it is a fully resolved address, and the 'host' portion will always be an IP address, not a DNS name. rF r rG N) rI rJ rK rL rM rN rO �realAddress� doConnect)re r9 �hostnamer r r r� � s ��z_BaseBaseClient._setRealAddressc C sf | j s| jst| d�sdS | �� z| �d� W n tyB Y n 0 | �� | j�t � |�� | `dS )z� Generic method called when the attempts to connect failed. It basically cleans everything it can: call connectionFailed, stop read and write, delete socket related members. � connectorNT)� connectedro r� �_stopReadingAndWritingrf �AttributeError�_collectSocketDetailsr� ZconnectionFailedrA rx )re r� r r r r� � s z"_BaseBaseClient.failIfNotConnectedc C s | � t�� � dS )z� If a connection attempt is still outstanding (i.e. no connection is yet established), immediately stop attempting to connect. N)r� r; Z UserErrorrz r r r �stopConnecting� s z_BaseBaseClient.stopConnectingc C s8 | j s| �tj|d�� n| j�| |� | j�|� dS )a� Invoked by lower-level logic when it's time to clean the socket up. Depending on the state of the connection, either inform the attached L{Connector} that the connection attempt has failed, or inform the connected L{IProtocol} that the established connection has been lost. @param reason: the reason that the connection was terminated @type reason: L{Failure} ��stringN)r� r� r; ZConnectErrorr� rw r� �re r� r r r rw � s z_BaseBaseClient.connectionLostN)r r r rg rJ �AF_INET� addressFamily�SOCK_STREAM� socketTyper� r� r� r� r� rw r r r r r� [ s 3r� c @ s@ e Zd ZdZeZeZdd� Zdd� Zdd� Z dd � Z d d� ZdS ) � BaseClienta� A base class for client TCP (and similar) sockets. @ivar realAddress: The address object that will be used for socket.connect; this address is an address tuple (the number of elements dependent upon the address family) which does not contain any names which need to be resolved. @type realAddress: C{tuple} @ivar _base: L{Connection}, which is the base class of this class which has all of the useful file descriptor methods. This is used by L{_TLSServerMixin} to call the right methods to directly manipulate the transport, as is necessary for writing TLS-encrypted bytes (whereas those methods on L{Server} will go through another layer of TLS if it has been enabled). c C s t | d�r| �� | �� dS )z� Implement the POSIX-ish (i.e. L{twisted.internet.interfaces.IReactorFDSet}) method of detaching this socket from the reactor for L{_BaseBaseClient}. ru N)r� rq rr rz r r r r� s z!BaseClient._stopReadingAndWritingc C s | ` | `dS )zn Clean up references to the socket and its file descriptor. @see: L{_BaseBaseClient} N)rJ r� rz r r r r� s z BaseClient._collectSocketDetailsc C s, t � | j| j�}|�d� t�|�� � |S )zc(internal) Create a non-blocking socket using self.addressFamily, self.socketType. r )rJ r� r� r� r<