zfc@sdZddlZddlTddlmZddlmZyddlZWnek r_nXeedZ ddlm Z ddlm Z m Z mZmZmZmZmZmZmZmZmZydd lmZWnek rnXddlZddlZddlZydd lmZWn!ek rNdd lmZnXyddlZWnek rxeZnXeed d Zeed dZ ddgZ!e!j"ej#ee$Z%ej&j'j(driZ)de)dd?d@fZ,ej-dAkre,dBfZ,nej&dCkre,dDfZ,ndEdFdGdHdIdJfZ.dKe/fdLYZ0dMe/fdNYZ1dOZ2x]e,D]UZ3ee2e3Z4e3e4_5ee%e3je4_ee4ee1Z6e7e1e3e6qWWe1Z$Z8dPe/fdQYZ9e/Z:e:edRZ;dS(SsThis 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 [*] 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) ssl() -- secure socket layer support (only available if configured) 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. iN(t*(tpartial(t MethodTypecCs5ddl}tjdtdd|j|||S(Nis;socket.ssl() is deprecated. Use ssl.wrap_socket() instead.t stackleveli(tssltwarningstwarntDeprecationWarningtsslwrap_simple(tsocktkeyfiletcertfilet_realssl((s/usr/lib64/python2.7/socket.pyR:s   (tSSLError( tRAND_addt RAND_statustSSL_ERROR_ZERO_RETURNtSSL_ERROR_WANT_READtSSL_ERROR_WANT_WRITEtSSL_ERROR_WANT_X509_LOOKUPtSSL_ERROR_SYSCALLt SSL_ERROR_SSLtSSL_ERROR_WANT_CONNECTt SSL_ERROR_EOFtSSL_ERROR_INVALID_ERROR_CODE(tRAND_egd(tStringIOtEBADFi tEINTRitgetfqdntcreate_connectiontwinsThe operation was interrupted.i'sA bad file handle was passed.i'sPermission denied.i's!A fault occurred on the network??i's#An invalid operation was attempted.i&'s The socket operation would blocki3's,A blocking operation is already in progress.i4'sThe network address is in use.i@'sThe connection has been reset.iF'sThe network has been shut down.iJ'sThe operation timed out.iL'sConnection refused.iM'sThe name is too long.iO'sThe host is down.iP'sThe host is unreachable.iQ'terrorTabtcCs|j}| s|dkr+t}nyt|\}}}Wntk rWn8X|jd|x$|D]}d|kroPqoqoW|}|S(sGet 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. s0.0.0.0it.(tstript gethostnamet gethostbyaddrterrortinsert(tnamethostnametaliasestipaddrs((s/usr/lib64/python2.7/socket.pyRs     tbindtconnectt connect_extfilenotlistent getpeernamet getsocknamet getsockoptt setsockopttsendallt setblockingt settimeoutt gettimeouttshutdowntnttioctltriscost sleeptaskwtrecvtrecvfromt recv_intot recvfrom_intotsendtsendtot _closedsocketcBs7eZgZdZeZZZZZZ eZ RS(cGsttddS(NsBad file descriptor(R&R(targs((s/usr/lib64/python2.7/socket.pyt_dummys( t__name__t __module__t __slots__RFRBR>R@RCR?RAt __getattr__(((s/usr/lib64/python2.7/socket.pyRDs t _socketobjectcBseZejZddgeeZeedddZ e ee dZ ej je _dZejje_dZddd Zed d d Zed d dZedd dZRS(t_sockt __weakref__icCsX|dkr!t|||}n||_x'tD]}t||t||q1WdS(N(tNonet _realsocketRLt_delegate_methodstsetattrtgetattr(tselftfamilyttypetprotoRLtmethod((s/usr/lib64/python2.7/socket.pyt__init__s    cCs=||_|jj}x|D]}||||qWdS(N(RLRF(RSRDRPRQtdummyRW((s/usr/lib64/python2.7/socket.pytcloses   cCs(|jj\}}td||fS(NRL(RLtacceptRK(RSR taddr((s/usr/lib64/python2.7/socket.pyR[scCstd|jS(sadup() -> socket object Return a new socket object connected to the same system resource.RL(RKRL(RS((s/usr/lib64/python2.7/socket.pytdupstricCst|j||S(smakefile([mode[, bufsize]]) -> file object Return a regular file object corresponding to the socket. The mode and bufsize arguments are as for the built-in open() function.(t _fileobjectRL(RStmodetbufsize((s/usr/lib64/python2.7/socket.pytmakefilescCs |jjS(N(RLRT(RS((s/usr/lib64/python2.7/socket.pytR!tdocsthe socket familycCs |jjS(N(RLRU(RS((s/usr/lib64/python2.7/socket.pyRcR!sthe socket typecCs |jjS(N(RLRV(RS((s/usr/lib64/python2.7/socket.pyRcR!sthe socket protocolN(RGRHROt__doc__tlistRPRItAF_INETt SOCK_STREAMRNRXRDRQRZR[R]RbtpropertyRTRURV(((s/usr/lib64/python2.7/socket.pyRKs   cGst|j||S(N(RRRL(R(RSRE((s/usr/lib64/python2.7/socket.pytmethsR_c BseZdZdZdZddddddd d d d g Zd dedZdZe eddZ dZ dZ dZ dZdZdZddZddZddZdZdZRS(s-Faux file object attached to a socket object.i sR`Rat softspaceRLt _rbufsizet _wbufsizet_rbuft_wbuft _wbuf_lent_closetrbicCs||_||_|dkr*|j}n||_t|_|dkrTd|_n$|dkro|j|_n ||_||_t|_ g|_ d|_ ||_ dS(Nii( RLR`tdefault_bufsizeRatFalseRkRlRmRRnRoRpRq(RSR R`RaRZ((s/usr/lib64/python2.7/socket.pyRXs               cCs |jdkS(N(RLRN(RS((s/usr/lib64/python2.7/socket.pyt _getclosedsRdsTrue if the file is closedcCsDz|jr|jnWd|jr6|jjnd|_XdS(N(RLtflushRqRZRN(RS((s/usr/lib64/python2.7/socket.pyRZs   cCsy|jWnnXdS(N(RZ(RS((s/usr/lib64/python2.7/socket.pyt__del__!scCs|jrdj|j}g|_d|_t|j|j}t|}d}t|}z<x5||kr|jj ||||!||7}qfWWd||kr||}~~|jj |t||_nXndS(NR!i( RotjoinRptmaxRlRstlent memoryviewRLR5tappend(RStdatat buffer_sizet data_sizet write_offsettviewt remainder((s/usr/lib64/python2.7/socket.pyRv(s"       cCs |jjS(N(RLR/(RS((s/usr/lib64/python2.7/socket.pyR/<scCst|}|sdS|jj||jt|7_|jdks|jdkred|ks|jdkr|j|jkr|jndS(Niis (tstrRoR|RpRzRmRv(RSR}((s/usr/lib64/python2.7/socket.pytwrite?s !cCsxtdtt|}|jttt|7_|jj||j dksg|j|j krt|j ndS(Ni( tfilterRNtmapRRptsumRzRotextendRmRv(RSRftlines((s/usr/lib64/python2.7/socket.pyt writelinesJs c Cst|j|j}|j}|jdd|dkrt|_xitry|jj|}Wn/t k r}|j dt krqInnX|sPn|j |qIW|j S|j}||kr|jd|j|}t|_|jj |j|St|_xtr ||}y|jj|}Wn/t k r|}|j dt krvq%nnX|sPnt|} | |kr| r|S| |kr|j |~Pn| |kstd|| f|j ||| 7}~q%W|j SdS(Niisrecv(%d) returned %d bytes(RyRlRsRntseekRtTrueRLR>R&RERRtgetvaluettelltreadRztAssertionError( RStsizetrbufsizetbufR}tetbuf_lentrvtlefttn((s/usr/lib64/python2.7/socket.pyRTs\               "  c Cs|j}|jdd|jdkr|jd|j|}|jdsht||krt|_|jj|j|S~n|dkrV|j dkrp|jd|jg}t|_d}|j j }xwt rby:x3|dkr*|d}|sPn|j|qWWn/tk r]}|jdtkrWqnnXPqWdj|S|jddt|_xt rKy|j j |j }Wn/tk r}|jdtkrqnnX|sPn|jd}|dkr;|d7}|j|| |jj||~Pn|j|qW|jS|jdd|j} | |kr|jd|j|} t|_|jj|j| St|_x=t r y|j j |j }Wn/tk r}|jdtkrqnnX|s*Pn|| } |jdd| }|dkr|d7}|jj||| r|j|| Pq|| Snt|} | |kr| r|S| | kr|j|| |jj|| Pn|j|| | 7} qW|jSdS(Niis iR!(RnRRtreadlinetendswithRzRRRRlRNRLR>RR|R&RERRxtfindR( RSRRtblinetbuffersR}R>RtnlRRRR((s/usr/lib64/python2.7/socket.pyRs  !                          icCsfd}g}xStra|j}|s+Pn|j||t|7}|r||krPqqW|S(Ni(RRR|Rz(RStsizehintttotalRftline((s/usr/lib64/python2.7/socket.pyt readliness   cCs|S(N((RS((s/usr/lib64/python2.7/socket.pyt__iter__scCs|j}|stn|S(N(Rt StopIteration(RSR((s/usr/lib64/python2.7/socket.pytnexts  (RGRHReRsR(RIRtRXRuRitclosedRZRwRvR/RRRRRRR(((s/usr/lib64/python2.7/socket.pyR_s(       F i  cCs|\}}d}xt||dtD]}|\}}} } } d} yYt||| } |tk rz| j|n|r| j|n| j| | SWq(tk r} | }| dk r| j qq(Xq(W|dk r|n tddS(scConnect 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. is!getaddrinfo returns an empty listN( RNt getaddrinfoRhtsockett_GLOBAL_DEFAULT_TIMEOUTR7R,R-R&RZ(taddressttimeouttsource_addressthosttportterrtrestaftsocktypeRVt canonnametsaR t_((s/usr/lib64/python2.7/socket.pyRs(      (<Ret_sockett functoolsRttypesRt_sslt ImportErrorRNRR tsslerrorRRRRRRRRRRRRtostsysRt cStringIORterrnoRRRRt__all__Rt_get_exports_listRROtplatformtlowert startswithR R|Rt_socketmethodsR(RPtobjectRDRKRjt_mtpRGtmRQt SocketTypeR_RR(((s/usr/lib64/python2.7/socket.pyt-s    L  $                        ,    -