B A[/=@sdZddlmZddlmZddlmZddlmZddlmZddlZddlTddl Z ddl Z ddl Z y ddl Z Wne k rdZ YnXee d d Zee d d Zee d d ZddgZee eeZe jdrfiZded<ded<ded<ded<ded<ded<ded<ded <d!ed"<d#ed$<d%ed&<d'ed(<d)ed*<d+ed,<d-ed.<ed/Gd0d1d1ejZd?d2d3Zeejd4rd5d6Zeed7rdedfd8d7Z e!eegZ"Gd9d:d:e j#Z$d@ddZ'dS)AaThis 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 Integer constants: AF_INET, AF_UNIX -- socket domains (first argument to socket() call) SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument) Many other constants may be defined; these may be used in calls to the setsockopt() and getsockopt() methods. )unicode_literals)print_function)division)absolute_import)superN)*EBADF EAGAIN EWOULDBLOCKgetfqdncreate_connectionwinzThe 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'errorTabcseZdZdZdddgZeeddfddZd d Zd d Z d dZ ddZ ddZ ddZ d ddZddZejfddZddZfddZZS)!socketz:A subclass of _socket.socket adding the makefile() method. __weakref___io_refs_closedrNcCs@|dkrtj||||ntj|||||d|_d|_dS)NrF)_socketr__init__rr)selffamilytypeprotofilenorH/opt/alt/python37/lib/python3.7/site-packages/future/backports/socket.pyrcs zsocket.__init__cCs|S)Nr)rrrr __enter__kszsocket.__enter__cGs|js|dS)N)rclose)rargsrrr__exit__nszsocket.__exit__cCsJtj|}|drFd|jj|jjt|ddr4dp6d|ddf}|S) z.Wrap __repr__() to reveal the real class name.z socket object Return a new socket object connected to the same system resource. )r)duprr&rrr settimeout gettimeout)rfdsockrrrr-s z socket.dupcCsF|\}}t|j|j|j|d}tdkr>|r>|d||fS)zaccept() -> (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). )rNT)_acceptrrrrgetdefaulttimeoutr/ setblocking)rr0addrr1rrraccepts   z socket.acceptrcKspd|kr|d}|d=nd}d|kr4|d}|d=nd}d|krP|d}|d=nd}x|D]}|dkrZtdqZWd|k}d|kp| } | s|std |k} d } | r| d7} |r| d7} t|| } |jd 7_|dkrd }|d krtj}|d kr| std| S| r"|r"t| | |} n*| r6t| |} n|s@tt| |} | rV| St | |||}||_ |S)a makefile(...) -> an I/O stream connected to the socket The arguments are as for io.open() after the filename, except the only mode characters supported are 'r', 'w' and 'b'. The semantics are similar too. (XXX refactor to share code?) newlineNerrorsencoding)r7wbz&invalid mode %r (only r, w, b allowed)r;r7r<r"rz!unbuffered streams must be binary) ValueErrorAssertionErrorSocketIOrioDEFAULT_BUFFER_SIZEBufferedRWPairBufferedReaderBufferedWriter TextIOWrappermode)rrH bufferingZ _3to2kwargsr8r9r:cwritingreadingbinaryrawmoderawbuffertextrrrmakefilesZ        zsocket.makefilecCs*|jdkr|jd8_|jr&|dS)Nrr=)rrr)rrrr_decref_socketioss zsocket._decref_socketioscCs||dS)N)r)r_ssrrr _real_closeszsocket._real_closecCsd|_|jdkr|dS)NTr)rrrU)rrrrrs z socket.closecsd|_tS)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)rrdetach)r)r&rrrVsz socket.detach)r7N)r(r' __qualname____doc__ __slots__AF_INET SOCK_STREAMrrr!r$r,r-r6rRrSrrrUrrV __classcell__rr)r&rr]s    0rcCst|}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)r0rrrnfdrrrfromfdsr^sharecCstddd|S)z fromshare(info) -> socket object Create a socket object from a the bytes object returned by socket.share(pid). r)r)inforrr fromsharesra 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)AF_UNIX NameErrorrZrrbrrV)rrrar<rrrrbs cspeZdZdZddZddZddZdd Zd d Zfd d Z ddZ e ddZ e ddZ ddZZS)rAzRaw 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)r7r;rwrbwbrwbzinvalid mode: %rr<r7r;F) r?rB RawIOBaser_sock_mode_reading_writing_timeout_occurred)rr1rHrrrrs    zSocketIO.__init__c Cs|||jrtdxby |j|Stk rHd|_Yq tk r|}z|jdt krjdSWdd}~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_checkReadableroIOErrorrk recv_intotimeouterrorr _blocking_errnos)rr<errrreadinto)s zSocketIO.readintoc CsX||y |j|Stk rR}z|jdtkr@dSWdd}~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)rp_checkWritablerksendrur rv)rr<rwrrrwriteBs zSocketIO.writecCs|jrtd|jS)z2True if the SocketIO is open for reading. zI/O operation on closed socket.)closedr?rm)rrrrreadableRszSocketIO.readablecCs|jrtd|jS)z2True if the SocketIO is open for writing. zI/O operation on closed socket.)r|r?rn)rrrrwritableYszSocketIO.writablecs|jrtdtS)z2True if the SocketIO is open for seeking. zI/O operation on closed socket.)r|r?rseekable)r)r&rrr`szSocketIO.seekablecCs||jS)z=Return the file descriptor of the underlying socket. )rprkr)rrrrrgszSocketIO.filenocCs|js|SdSdS)Nr>)r|r)rrrrnamemsz SocketIO.namecCs|jS)N)rl)rrrrrHtsz SocketIO.modecCs*|jr dStj||jd|_dS)zClose the SocketIO object. This doesn't close the underlying socket, except if all references to it have disappeared. N)r|rBrjrrkrS)rrrrrxs   zSocketIO.close)r(r'rWrXrrxr{r}r~rrpropertyrrHrr\rr)r&rrA s     rAr"cCsn|}|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.)strip gethostname gethostbyaddrruinsert)rhostnamealiasesipaddrsrrrr s    )_GLOBAL_DEFAULT_TIMEOUTc Cs|\}}d}xt||dtD]}|\}}} } } d} y:t||| } |tk rR| ||r`| || | | Stk r} z| }| dk r| Wdd} ~ XYqXqW|dk r|ntddS)adConnect 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. An host of '' or port 0 tells the OS to use the default. Nrz!getaddrinfo returns an empty list) getaddrinfor[rrr.bindconnectrur)addressrtsource_addresshostporterrresafsocktyper canonnamesar1_rrrrs(    )r)r")(rX __future__rrrrZfuture.builtinsrrossysrBerrno ImportErrorr)rr r __all__extend_get_exports_listr _realsocketplatformlowerr%rappendr^hasattrrar[rbsetrvrjrAr rrrrrr-sb              w