B ‚Ž\]ãjã@spdZddlZddlTddlZddlZddlZddlZddlmZmZy ddl Z Wne k rhdZ YnXe e ddƒZ e e ddƒZ e e d dƒZd d d d dgZe e e¡¡e d edd„¡e dedd„¡e dedd„¡e dedd„¡dZdZdd„ZeZej ¡ d¡r iZded<ded<ded <d!ed"<d#ed$<d%ed&<d'ed(<d)ed*<d+ed,<d-ed.<d/ed0<d1ed2<d3ed4<d5ed6<d7ed8<e d9¡Gd:d;„d;eƒZGdd „Z e!ejd?ƒrìd@dA„Z"e dA¡e!edBƒr de#dfdCdB„Z$ne%e#dfdDdB„Z$e dB¡dEe$_e ehZ&GdFdG„dGej'ƒZ(dNdId „Z)e*ƒZ+e+dfdJd „Z,dOdKdL„Z-dS)Pa0This module provides socket operations and some related functions. On Unix, it supports IP (Internet Protocol) and Unix domain sockets. On other systems, it only supports IP. Functions specific for a socket are available as methods of the socket object. Functions: socket() -- create a new socket object socketpair() -- create a pair of new socket objects [*] fromfd() -- create a socket object from an open file descriptor [*] fromshare() -- create a socket object from data received from socket.share() [*] gethostname() -- return the current hostname gethostbyname() -- map a hostname to its IP number gethostbyaddr() -- map an IP number or hostname to DNS info getservbyname() -- map a service name and a protocol name to a port number getprotobyname() -- map a protocol name (e.g. 'tcp') to a number ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order htons(), htonl() -- convert 16, 32 bit int from host to network byte order inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89) socket.getdefaulttimeout() -- get the default timeout value socket.setdefaulttimeout() -- set the default timeout value create_connection() -- connects to an address, with an optional timeout and optional source address. [*] not available on all platforms! Special objects: SocketType -- type object for socket objects error -- exception raised for I/O errors has_ipv6 -- boolean value indicating if IPv6 is supported IntEnum constants: AF_INET, AF_UNIX -- socket domains (first argument to socket() call) SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument) Integer constants: Many other constants may be defined; these may be used in calls to the setsockopt() and getsockopt() methods. éN)Ú*)ÚIntEnumÚIntFlagÚEBADFé ÚEAGAINé Ú EWOULDBLOCKÚfromfdÚgetfqdnÚcreate_connectionÚ AddressFamilyÚ SocketKindcCs| ¡o| d¡S)NZAF_)ÚisupperÚ startswith)ÚC©rú"/usr/local/lib/python3.7/socket.pyÚLórcCs| ¡o| d¡S)NZSOCK_)rr)rrrrrQrZMsgFlagcCs| ¡o| d¡S)NZMSG_)rr)rrrrrVrZ AddressInfocCs| ¡o| d¡S)NZAI_)rr)rrrrr[rz 127.0.0.1z::1cCs"y||ƒStk r|SXdS)z{Convert a numeric family value to an IntEnum member. If it's not a known member, return the numeric value itself. N)Ú ValueError)ÚvalueZ enum_klassrrrÚ_intenum_converterasrÚwinzThe operation was interrupted.i'zA bad file handle was passed.i'zPermission denied.i'z!A fault occurred on the network??i'z#An invalid operation was attempted.i&'z The socket operation would blocki3'z,A blocking operation is already in progress.i4'zThe network address is in use.i@'zThe connection has been reset.iF'zThe network has been shut down.iJ'zThe operation timed out.iL'zConnection refused.iM'zThe name is too long.iO'zThe host is down.iP'zThe host is unreachable.iQ'ÚerrorTabc@s eZdZdS)Ú_GiveupOnSendfileN)Ú__name__Ú __module__Ú __qualname__rrrrr‚srcs,eZdZdZdddgZd9dd„Zd d „Zd d „Zd d„Zdd„Z dd„Z dd„Z d:ddddœdd„Z e edƒrzd;dd„Zn dd"d#„Zd$d%„Zejfd&d'„Zd(d)„Z‡fd*d+„Ze‡fd,d-„ƒZe‡fd.d/„ƒZejd0krd1d2„Zd3d4„Znd5d2„Zd6d4„Zd7e_d8e_‡ZS)?Úsocketz:A subclass of _socket.socket adding the makefile() method.Ú __weakref__Ú_io_refsÚ_closedéÿÿÿÿNcCsP|dkr,|dkrt}|dkr t}|dkr,d}tj |||||¡d|_d|_dS)Nr#rF)ÚAF_INETÚ SOCK_STREAMÚ_socketrÚ__init__r!r")ÚselfÚfamilyÚtypeÚprotoÚfilenorrrr'‹szsocket.__init__cCs|S)Nr)r(rrrÚ __enter__›szsocket.__enter__cGs|js| ¡dS)N)r"Úclose)r(ÚargsrrrÚ__exit__žszsocket.__exit__cCs¸t|ddƒ}d|jj|jj|r"dnd| ¡|j|j|jf}|s¬y | ¡}|r^|dt |ƒ7}Wnt k rtYnXy |  ¡}|r”|dt |ƒ7}Wnt k rªYnX|d7}|S) zVWrap __repr__() to reveal the real class name and socket address(es). r"Fz,<%s.%s%s fd=%i, family=%s, type=%s, proto=%iz [closed]Úz , laddr=%sz , raddr=%sú>) ÚgetattrÚ __class__rrr,r)r*r+Ú getsocknameÚstrÚerrorZ getpeername)r(ÚclosedÚsZladdrZraddrrrrÚ__repr__¢s0   zsocket.__repr__cCs tdƒ‚dS)NzCannot serialize socket object)Ú TypeError)r(rrrÚ __getstate__¿szsocket.__getstate__cCs6t| ¡ƒ}|j|j|j|j|d}| | ¡¡|S)z«dup() -> socket object Duplicate the socket. Return a new socket object connected to the same system resource. The new socket is non-inheritable. )r,)Údupr,r4r)r*r+Ú settimeoutÚ gettimeout)r(ÚfdÚsockrrrr=Âs z socket.dupcCsF| ¡\}}t|j|j|j|d}tƒdkr>| ¡r>| d¡||fS)zúaccept() -> (socket object, address info) Wait for an incoming connection. Return a new socket representing the connection, and the address of the client. For IP sockets, the address info is a pair (hostaddr, port). )r,NT)Z_acceptrr)r*r+Úgetdefaulttimeoutr?Ú setblocking)r(r@ÚaddrrArrrÚacceptÍs   z socket.acceptÚr)ÚencodingÚerrorsÚnewlinec Cst|ƒdddhks td|fƒ‚d|k}d|kp4| }|sB|sBt‚d|k}d} |rZ| d7} |rf| d7} t|| ƒ} |jd7_|dkrŠd}|d kr˜tj}|d kr°|s¬td ƒ‚| S|rÈ|rÈt | | |¡} n&|rÚt | |¡} n|sât‚t  | |¡} |rö| St  | |||¡} || _ | S) zÍmakefile(...) -> an I/O stream connected to the socket The arguments are as for io.open() after the filename, except the only supported mode values are 'r' (default), 'w' and 'b'. rFÚwÚbz&invalid mode %r (only r, w, b allowed)r1éNr#rz!unbuffered streams must be binary) ÚsetrÚAssertionErrorÚSocketIOr!ÚioÚDEFAULT_BUFFER_SIZEÚBufferedRWPairÚBufferedReaderÚBufferedWriterÚ TextIOWrapperÚmode) r(rVÚ bufferingrGrHrIZwritingZreadingZbinaryZrawmodeÚrawÚbufferÚtextrrrÚmakefileÝs@   zsocket.makefileÚsendfilerc Csê| |||¡| ¡}y | ¡}Wn0ttjfk rR}z t|ƒ‚Wdd}~XYnXyt |¡j}Wn*t k rŽ}z t|ƒ‚Wdd}~XYnX|s˜dS|s |n|}|  ¡} | dkr¼t dƒ‚t t dƒrÐt  ¡} nt  ¡} |  |t j¡d} | j} tj} zÌxÆ| r| | ƒst d¡‚|r.|| }|dkr.Py| ||||ƒ}Wn^tk rd| s^| ƒwúYqút k rž}z| dkrˆt|ƒ‚|d‚Wdd}~XYqúX|dkr¬P||7}| |7} qúW| S| dkrät |dƒrä| |¡XdS)Nrz&non-blocking sockets are not supportedÚ PollSelectorz timed outÚseek)Ú_check_sendfile_paramsr,ÚAttributeErrorrPÚUnsupportedOperationrÚosÚfstatÚst_sizeÚOSErrorr?rÚhasattrÚ selectorsr]ZSelectSelectorÚregisterZ EVENT_WRITEZselectr\r&ÚtimeoutÚBlockingIOErrorr^)r(ÚfileÚoffsetÚcountZsocknor,ÚerrZfsizeÚ blocksizeriZselectorÚ total_sentZselector_selectZ os_sendfileÚsentrrrÚ_sendfile_use_sendfile s`         zsocket._sendfile_use_sendfilecCs tdƒ‚dS)Nz,os.sendfile() not available on this platform)r)r(rkrlrmrrrrrJsc Cs| |||¡| ¡dkr"tdƒ‚|r0| |¡|r>t|dƒnd}d}|j}|j}z†x€|rrt|||ƒ}|dkrrPt||ƒƒ}|s„PxLy ||ƒ} Wntk r¨w†Yq†X|| 7}| t |ƒkrÌ|| d…}q†Pq†WqVW|S|dkrút |dƒrú| ||¡XdS)Nrz&non-blocking sockets are not supportedi r^) r_r?rr^ÚminÚreadÚsendÚ memoryviewrjÚlenrf) r(rkrlrmrorpZ file_readZ sock_sendÚdatarqrrrÚ_sendfile_use_sendNs<      zsocket._sendfile_use_sendcCsddt|ddƒkrtdƒ‚|jt@s*tdƒ‚|dk r`t|tƒsJtd |¡ƒ‚|dkr`td |¡ƒ‚dS)NrKrVz$file should be opened in binary modez+only SOCK_STREAM type sockets are supportedz+count must be a positive integer (got {!r})r)r3rr*r%Ú isinstanceÚintr;Úformat)r(rkrlrmrrrr_rs   zsocket._check_sendfile_paramscCs2y| |||¡Stk r,| |||¡SXdS)a_sendfile(file[, offset[, count]]) -> sent Send a file until EOF is reached by using high-performance os.sendfile() and return the total number of bytes which were sent. *file* must be a regular file object opened in binary mode. If os.sendfile() is not available (e.g. Windows) or file is not a regular file socket.send() will be used instead. *offset* tells from where to start reading the file. If specified, *count* is the total number of bytes to transmit as opposed to sending the file until EOF is reached. File position is updated on return or also in case of error in which case file.tell() can be used to figure out the number of bytes which were sent. The socket must be of SOCK_STREAM type. Non-blocking sockets are not supported. N)rrrry)r(rkrlrmrrrr\szsocket.sendfilecCs*|jdkr|jd8_|jr&| ¡dS)NrrL)r!r"r.)r(rrrÚ_decref_socketios–s zsocket._decref_socketioscCs| |¡dS)N)r.)r(Z_ssrrrÚ _real_closeœszsocket._real_closecCsd|_|jdkr| ¡dS)NTr)r"r!r~)r(rrrr. s z socket.closecsd|_tƒ ¡S)a detach() -> file descriptor Close the socket object without closing the underlying file descriptor. The object cannot be used after this call, but the file descriptor can be reused for other purposes. The file descriptor is returned. T)r"ÚsuperÚdetach)r()r4rrr€¦sz socket.detachcsttƒjtƒS)z@Read-only access to the address family for this socket. )rrr)r )r()r4rrr)°sz socket.familycsttƒjtƒS)z-Read-only access to the socket type. )rrr*r)r()r4rrr*¶sz socket.typeÚntcCst | ¡¡S)N)rbZget_handle_inheritabler,)r(rrrÚget_inheritable½szsocket.get_inheritablecCst | ¡|¡dS)N)rbZset_handle_inheritabler,)r(Ú inheritablerrrÚset_inheritable¿szsocket.set_inheritablecCst | ¡¡S)N)rbr‚r,)r(rrrr‚ÂscCst | ¡|¡dS)N)rbr„r,)r(rƒrrrr„Äsz&Get the inheritable flag of the socketz&Set the inheritable flag of the socket)r#r#r#N)rFN)rN)rN)rN)rN) rrrÚ__doc__Ú __slots__r'r-r0r:r<r=rEr[rfrbrrryr_r\r}r&rr~r.r€Úpropertyr)r*Únamer‚r„Ú __classcell__rr)r4rr…s<   )  A  $    rcCst|ƒ}t||||ƒS)z¾ fromfd(fd, family, type[, proto]) -> socket object Create a socket object from a duplicate of the given file descriptor. The remaining arguments are the same as for socket(). )r=r)r@r)r*r+Znfdrrrr ÉsZsharecCstddd|ƒS)z‡ fromshare(info) -> socket object Create a socket object from the bytes object returned by socket.share(pid). r)r)ÚinforrrÚ fromshareÓsr‹Ú socketpaircCsh|dkr*yt}Wntk r(t}YnXt |||¡\}}t|||| ¡ƒ}t|||| ¡ƒ}||fS)aasocketpair([family[, type[, proto]]]) -> (socket object, socket object) Create a pair of socket objects from the sockets returned by the platform socketpair() function. The arguments are the same as for socket() except the default family is AF_UNIX if defined on the platform; otherwise, the default is AF_INET. N)ZAF_UNIXÚ NameErrorr$r&rŒrr€)r)r*r+ÚarKrrrrŒÞs c Cs|tkrt}n|tkrt}ntdƒ‚|tkr4tdƒ‚|dkrDtdƒ‚t|||ƒ}z¢| |df¡| ¡|  ¡dd…\}}t|||ƒ}yP|  d¡y|  ||f¡Wnt t fk r¾YnX|  d¡| ¡\}} Wn| ¡‚YnXWd| ¡X||fS)Nz?Only AF_INET and AF_INET6 socket address families are supportedz)Only SOCK_STREAM socket type is supportedrzOnly protocol zero is supportedéFT)r$Ú _LOCALHOSTZAF_INET6Ú _LOCALHOST_V6rr%rÚbindZlistenr5rCÚconnectrjÚInterruptedErrorrEr.) r)r*r+ÚhostZlsockrDÚportZcsockZssockÚ_rrrrŒós8      a8socketpair([family[, type[, proto]]]) -> (socket object, socket object) Create a pair of socket objects from the sockets returned by the platform socketpair() function. The arguments are the same as for socket() except the default family is AF_UNIX if defined on the platform; otherwise, the default is AF_INET. cspeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Z‡fd d „Z dd„Z e dd„ƒZ e dd„ƒZ dd„Z‡ZS)rOz¬Raw I/O implementation for stream sockets. This class supports the makefile() method on sockets. It provides the raw I/O interface on top of a socket object. cCsZ|dkrtd|ƒ‚tj |¡||_d|kr6|d7}||_d|k|_d|k|_d|_dS)N)rFrJÚrwÚrbÚwbZrwbzinvalid mode: %rrKrFrJF) rrPÚ RawIOBaser'Ú_sockÚ_modeÚ_readingÚ_writingÚ_timeout_occurred)r(rArVrrrr'3s    zSocketIO.__init__c Cs†| ¡| ¡|jrtdƒ‚xby |j |¡Stk rHd|_‚Yq tk r|}z|jdt krjdS‚Wdd}~XYq Xq WdS)a3Read up to len(b) bytes into the writable buffer *b* and return the number of bytes read. If the socket is non-blocking and no bytes are available, None is returned. If *b* is non-empty, a 0 return value indicates that the connection was shutdown at the other end. z!cannot read from timed out objectTrN) Ú _checkClosedÚ_checkReadabler rerœZ recv_intorir7r/Ú_blocking_errnos)r(rKÚerrrÚreadinto?s zSocketIO.readintoc CsX| ¡| ¡y |j |¡Stk rR}z|jdtkr@dS‚Wdd}~XYnXdS)aWrite the given bytes or bytearray object *b* to the socket and return the number of bytes written. This can be less than len(b) if not all data could be written. If the socket is non-blocking and no bytes could be written None is returned. rN)r¡Ú_checkWritablerœrur7r/r£)r(rKr¤rrrÚwriteVs zSocketIO.writecCs|jrtdƒ‚|jS)z2True if the SocketIO is open for reading. zI/O operation on closed socket.)r8rrž)r(rrrÚreadablefszSocketIO.readablecCs|jrtdƒ‚|jS)z2True if the SocketIO is open for writing. zI/O operation on closed socket.)r8rrŸ)r(rrrÚwritablemszSocketIO.writablecs|jrtdƒ‚tƒ ¡S)z2True if the SocketIO is open for seeking. zI/O operation on closed socket.)r8rrÚseekable)r()r4rrrªtszSocketIO.seekablecCs| ¡|j ¡S)z=Return the file descriptor of the underlying socket. )r¡rœr,)r(rrrr,{szSocketIO.filenocCs|js| ¡SdSdS)Nr#)r8r,)r(rrrrˆsz SocketIO.namecCs|jS)N)r)r(rrrrVˆsz SocketIO.modecCs*|jr dStj |¡|j ¡d|_dS)z‡Close the SocketIO object. This doesn't close the underlying socket, except if all references to it have disappeared. N)r8rPr›r.rœr})r(rrrr.Œs   zSocketIO.close)rrrr…r'r¥r§r¨r©rªr,r‡rˆrVr.r‰rr)r4rrO"s     rOr1cCsn| ¡}|r|dkrtƒ}yt|ƒ\}}}Wntk r@Yn*X| d|¡x|D]}d|krTPqTW|}|S)aGet fully qualified domain name from name. An empty argument is interpreted as meaning the local host. First the hostname returned by gethostbyaddr() is checked, then possibly existing aliases. In case no FQDN is available, hostname from gethostname() is returned. z0.0.0.0rÚ.)ÚstripZ gethostnameZ gethostbyaddrr7Úinsert)rˆZhostnameÚaliasesZipaddrsrrrr —s    c CsÆ|\}}d}xžt||dtƒD]Œ}|\}}} } } d} y>t||| ƒ} |tk rR|  |¡|r`|  |¡|  | ¡d}| Stk r¦} z| }| dk r–|  ¡Wdd} ~ XYqXqW|dk rº|‚ntdƒ‚dS)acConnect to *address* and return the socket object. Convenience function. Connect to *address* (a 2-tuple ``(host, port)``) and return the socket object. Passing the optional *timeout* parameter will set the timeout on the socket instance before attempting to connect. If no *timeout* is supplied, the global default timeout setting returned by :func:`getdefaulttimeout` is used. If *source_address* is set it must be a tuple of (host, port) for the socket to bind as a source address before making the connection. A host of '' or port 0 tells the OS to use the default. Nrz!getaddrinfo returns an empty list) Ú getaddrinfor%rÚ_GLOBAL_DEFAULT_TIMEOUTr>r’r“r7r.)ZaddressriZsource_addressr•r–rnÚresÚafÚsocktyper+Ú canonnameÚsarAr—rrrr ³s*    c CsTg}xJt ||||||¡D]2}|\}} }} } | t|tƒt| tƒ|| | f¡qW|S)aºResolve host and port into list of address info entries. Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. host is a domain name, a string representation of an IPv4/v6 address or None. port is a string service name such as 'http', a numeric port number or None. By passing None as the value of host and port, you can pass NULL to the underlying C API. The family, type and proto arguments can be optionally specified in order to narrow the list of addresses returned. Passing zero as a value for each of these arguments selects the full range of results. )r&r¯Úappendrr r) r•r–r)r*r+ÚflagsZaddrlistr±r²r³r´rµrrrr¯Ûs r¯)r)r1)rrrr).r…r&rbÚsysrPrgÚenumrrÚerrnoÚ ImportErrorr3rrr Ú__all__ÚextendÚ_get_exports_listÚ_convertrrr‘rrZ _realsocketÚplatformÚlowerrrr¶Ú Exceptionrr rfr‹r%rŒr$r£r›rOr Úobjectr°r r¯rrrrÚ/s            F   $ u  '