Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/internet/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyc


H!Nc@s{dZddlZddlZddlZddlZddlZddlmZddlm	Z	ddl
mZmZy ddl
mZmZmZWnek
r!y ddlmZmZmZWq"ek
rdefdYZd	efd
YZdefdYZq"XnXe	d
kreZddlmZddlmZddlmZddlmZ ddlm!Z"ddlm#Z$ddlm%Z&ddlm'Z(ddlm)Z*ddlm+Z,eZ-eZ.eZ/ddlm!Z0ddl1m2Z3nddlmZddlmZddlmZddlmZddlm Z ddlm"Z"ddlm$Z$dd lm&Z&dd!lm(Z(dd"lm*Z*dd#lm,Z,dd$lm-Z-dd%lm.Z.dd&lm/Z/dd'lm0Z0dd(l4m3Z3dd)lm5Z5dd*l6m7Z7m8Z8m9Z9dd+l:m;Z;dd,l
m<Z<m=Z=m>Z>dd-l?m@Z@dd.lAmBZBdd/l6mCZCmDZDmEZEmFZFd0efd1YZGd2efd3YZHd4eeCjIeGeHfd5YZJd6eeJfd7YZKd8eKfd9YZLd:eeJfd;YZMd<e7jNeGfd=YZOd>e7jPfd?YZQdS(@st
Various asynchronous TCP/IP classes.

End users shouldn't use this module directly - use the reactor APIs instead.
iN(t
implements(tplatformType(tversionst	deprecate(tConnectionMixintClientMixintServerMixint_TLSConnectionMixincBseZeZRS((t__name__t
__module__tFalsetTLS(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR)st_TLSClientMixincBseZRS((RR	(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR+st_TLSServerMixincBseZRS((RR	(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR
-stwin32(t	WSAEINVAL(tWSAEWOULDBLOCK(tWSAEINPROGRESS(tWSAEALREADY(t
WSAECONNRESET(t
WSAEISCONN(tWSAENOTCONN(tWSAEINTR(t
WSAENOBUFS(t	WSAEMFILE(tformatError(tEPERM(tEINVAL(tEWOULDBLOCK(tEINPROGRESS(tEALREADY(t
ECONNRESET(tEISCONN(tENOTCONN(tEINTR(tENOBUFS(tEMFILE(tENFILE(tENOMEM(tEAGAIN(tECONNABORTED(tstrerror(t	errorcode(tbasetaddresstfdesc(t
deferLater(tlogtfailuretreflect(t
unsignedID(tCannotListenError(tabstracttmaint
interfacesterrort
_SocketClosercBseZdZdZRS(tshutdowncCs|j}yN|r+t||jdn+|jjtjtjtjdddWntjk
rmnXy|j	Wntjk
rnXdS(Nitiiii(
tsockettgetattrt_socketShutdownMethodt
setsockoptt
SOL_SOCKETt	SO_LINGERtstructtpackR7tclose(tselftorderlytskt((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt_closeSocketgs	(RR	R=RG(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR8dst_AbortingMixincBseZdZeZdZRS(s
    Common implementation of C{abortConnection}.

    @ivar _aborting: Set to C{True} when C{abortConnection} is called.
    @type _aborting: C{bool}
    cCsw|js|jrdSt|_|j|jd|_d|_|jjd|j	t
jtj
dS(sf
        Aborts the connection immediately, dropping any buffered data.

        @since: 11.1
        Nc_sdS(N(tNone(targstkwargs((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt<lambda>sc_sdS(N(RI(RJRK((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRLsi(tdisconnectedt	_abortingtTruetstopReadingtstopWritingtdoReadtdoWritetreactort	callLatertconnectionLostR0tFailureR7tConnectionAborted(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytabortConnections	

(RR	t__doc__R
RNRY(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRHst
ConnectioncBseZdZeejejddZdZ	dZ
dZdZdZ
dZdZd	Zd
ZdZdZd
ZRS(s

    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}
    cCsHtjj|d|||_|jjd|j|_||_dS(NRTi(R4tFileDescriptort__init__R;tsetblockingtfilenotprotocol(RDRFR`RT((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR]s
	cCs|jS(s&Return the socket for this connection.(R;(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt	getHandlescCsy|jj|j}Wn4tjk
rO}|jdtkrEdStjSnX|s]tjS|j	j
|}|dk	r|j	j
}d}tj
|tjddddd|}tj||n|S(s\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.
        iNsPReturning a value other than None from %(fqpn)s is deprecated since %(version)s.tTwisteditformat(R;trecvt
bufferSizeR7RJRR5tCONNECTION_LOSTtCONNECTION_DONER`tdataReceivedRIRtgetDeprecationWarningStringRtVersiontwarnAboutFunction(RDtdatatsetrvaltoffendert
warningFormatt
warningString((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRRs"	cCsy#|jjt|d|jSWnZtjk
r}|jdtkrX|j|S|jdtt	fkrudSt
jSnXdS(s
        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.
        iN(R;tsendtbuffert
SEND_LIMITR7RJR"t
writeSomeDataRR#R5Rf(RDRlRm((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRus#
cCsyt|j|jdWntjk
r3nXtj|jd}|ry|jWqt	j
}tj|j
|qXndS(Ni(R<R;R=R7R6tIHalfCloseableProtocolR`RItwriteConnectionLostR0RWR/terrRV(RDtptf((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt_closeWriteConnections
cCsdtj|jd}|rSy|jWq`tj|jtj	q`Xn
|j|dS(N(
R6RvR`RItreadConnectionLostR/RxRVR0RW(RDtreasonRy((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR|s
cCslt|dsdStjj|||j|jtj|j}|`|`	|`
|j|dS(s6See abstract.FileDescriptor.connectionLost().
        R;N(thasattrR4R\RVRGtcheckR7RXR`R;R_(RDR}R`((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRVs	t
UninitializedcCs|jS(sEReturn the prefix to log with when I own the logging thread.
        (tlogstr(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt	logPrefixscCs"tj|jjtjtjS(N(toperatorttruthR;t
getsockopttIPPROTO_TCPtTCP_NODELAY(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt
getTcpNoDelayscCs |jjtjtj|dS(N(R;R>RR(RDtenabled((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt
setTcpNoDelay"scCs"tj|jjtjtjS(N(RRR;RR?tSO_KEEPALIVE(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytgetTcpKeepAlive%scCs |jjtjtj|dS(N(R;R>R?R(RDR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytsetTcpKeepAlive)sN(RR	RZRR6t
ITCPTransportt
ISystemHandleRIR]RaRRRuR{R|RVRRRRRR(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR[s
					
					t
BaseClientcBsweZdZeZejZejZ	dZ
dZdZdZ
dZdZdZdZd	ZRS(
s=
    A base class for client TCP (and similiar) sockets.
    cCsa|rGtj||d||j|_|j|_|jd|n|jd|j|dS(sU
        Called by base classes to continue to next stage of initialization.
        iN(R[R]RIt	doConnectRSRRRUtfailIfNotConnected(RDtwhenDoneRFR7RT((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt_finishInit7scCs|jtjdS(sStop attempt to connect.N(RR7t	UserError(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytstopConnectingCscCs|js"|js"t|dr&dS|jjtj|t|drk|j|j|`ny|j	t
Wntk
rn
X|`|`
dS(s
        Generic method called when the attemps to connect failed. It basically
        cleans everything it can: call connectionFailed, stop read and write,
        delete socket related members.
        t	connectorNRT(t	connectedRMR~RtconnectionFailedR0RWRPRQRGROtAttributeErrorR;R_(RDRx((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRGs

	
cCs<tj|j|j}|jdtj|j|S(sc(internal) Create a non-blocking socket using
        self.addressFamily, self.socketType.
        i(R;t
addressFamilyt
socketTypeR^R-t_setCloseOnExecR_(RDts((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytcreateInternetSocket_s
cCs`tj|jdr-|j|jdn/|jj|jd}|j|j|jdS(Ni(R4tisIPAddresstaddrt_setRealAddressRTtresolvetaddCallbacksR(RDtd((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytresolveAddresshscCs$||jdf|_|jdS(Ni(RtrealAddressR(RDR,((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRoscCsJt|dsdS|jjtjtj}|rZ|jtj|t|fdSy|jj	|j
}Wn#tjk
r}|jd}nX|r|tkrq|t
ttfks|tkrtdkr|j|jdS|jtj|t|fdSn|`|`|j|j|jdS(smI connect the socket.

        Then, call the protocol's makeConnection, and start waiting for data.
        RNiR(R~R;RR?tSO_ERRORRR7tgetConnectErrorR)t
connect_exRRJR RRRRRtstartReadingtstartWritingRSRRRPRQt_connectDone(RDRxt
connectResultRm((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRss2"


"

cCsa|jj|j|_d|_|j|j}d||_|j|jj|dS(Nis	%s,client(	Rt
buildProtocoltgetPeerR`Rt
_getLogPrefixRRtmakeConnection(RDR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRs	

cCsI|js%|jtjd|n tj|||jj|dS(Ntstring(RRR7tConnectErrorR[RVR(RDR}((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRVs	(RR	RZR[t_baseR;tAF_INETRtSOCK_STREAMRRRRRRRRRRV(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR.s										2	tClientcBs5eZdZddZdZdZdZRS(s
A TCP client.c
Cs||_||f|_|j}d}d}y|j}Wn<tjk
r{}	tj|	jd|	jd}d}nX|r|dk	ry|j	|Wqtjk
r}	tj|	jd|	jd}d}qXn|j
||||dS(Nii(RRRRIRR;R7tConnectBindErrorRJtbindR(
RDthosttporttbindAddressRRTRRxRFRm((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR]s"		 
 
cCstjd|jjS(s`Returns an IPv4Address.

        This indicates the address from which I am connecting.
        tTCP(R,tIPv4AddressR;tgetsockname(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytgetHostscCstjd|jS(s\Returns an IPv4Address.

        This indicates the address that I am connected to.
        R(R,RR(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRscCs#d|j|jt|f}|S(Ns<%s to %s at %x>(t	__class__RR2(RDR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt__repr__sN(RR	RZRIR]RRR(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRs
		tServercBs8eZdZeZdZdZdZdZRS(s<
    Serverside socket-stream connection class.

    This is a serverside network connection transport; a socket which came from
    an accept() on a server.

    @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).
    cCstj||||||_||_||_|d|_|j|j}d|||jf|_d|jj	j
|j|jjf|_|j
d|_dS(s

        Server(sock, protocol, client, server, sessionno)

        Initialize it with a socket, a protocol, a descriptor for my peer (a
        tuple of host, port describing the other end of the connection), an
        instance of Port, and a session number.
        is%s,%s,%ss<%s #%s on %s>iN(R[R]tservertclientt	sessionnothostnameRR`RRRt_realPortNumbertrepstrRR(RDtsockR`RRRRTR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR]s			

cCs|jS(s4A string representation of this connection.
        (R(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR	scCstjd|jjS(sNReturns an IPv4Address.

        This indicates the server's address.
        R(R,RR;R(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRscCstjd|jS(sNReturns an IPv4Address.

        This indicates the client's address.
        R(R,RR(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRs(	RR	RZR[RR]RRR(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRs
			tPortcBseZdZeejejZej	Z
eZdZ
dZdZdZdZddddZdZdZdZd	Zd
ZejejdZeZdZd
Z dZ!dZ"RS(s.
    A TCP server port, listening for connections.

    When a connection is accepted, this will call a factory's buildProtocol
    with the incoming address as an argument, according to the specification
    described in L{twisted.internet.interfaces.IProtocolFactory}.

    If you wish to change the sort of transport that will be used, the
    C{transport} attribute will be called with the signature expected for
    C{Server.__init__}, so it can be replaced.

    @ivar deferred: a deferred created when L{stopListening} is called, and
        that will fire when connection is lost. This is not to be used it
        directly: prefer the deferred returned by L{stopListening} instead.
    @type deferred: L{defer.Deferred}

    @ivar disconnecting: flag indicating that the L{stopListening} method has
        been called and that no connections should be accepted anymore.
    @type disconnecting: C{bool}

    @ivar connected: flag set once the listen has successfully been called on
        the socket.
    @type connected: C{bool}

    @ivar _type: A string describing the connections which will be created by
        this port.  Normally this is C{"TCP"}, since this is a TCP port, but
        when the TLS implementation re-uses this class it overrides the value
        with C{"TLS"}.  Only used for logging.
    iti2RcCs>tjj|d|||_||_||_||_dS(s5Initialize with a numeric port to listen on.
        RTN(R+tBasePortR]Rtfactorytbacklogt	interface(RDRRRRRT((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR]Ms
			cCsG|jdk	r,d|j|jj|jfSd|j|jjfSdS(Ns<%s of %s on %s>s<%s of %s (not listening)>(RRIRR(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRVscCsMtjj|}tdkrItjdkrI|jtjtj	dn|S(Ntposixtcygwini(
R+RRRtsystplatformR>R;R?tSO_REUSEADDR(RDR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR]scCsy)|j}|j|j|jfWn.tjk
rY}t|j|j|fnX|jd|_t	j
d|j|j|jf|jj
|j|jt|_||_|jj|_d|_|jdS(sCreate and bind my socket, and begin listening on it.

        This is called on unserialization, and must be called after creating a
        server to begin listening on the specified port.
        is%s starting on %sidN(RRRRR;R7R3RRR/tmsgRRtdoStarttlistenRRORR_t
numberAcceptsR(RDRFtle((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytstartListeningds	
			cCs|\}}tjd||S(NR(R,t_ServerFactoryIPv4Address(RDt.1RR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt
_buildAddrs	c	Csytdkr|j}nd}xxt|D]Z}|jrAdSy|jj\}}Wntjk
r}|jdtt	fkr||_Pn^|jdt
krq.nE|jdttt
ttfkrtjdt|jdfPnnXtj|j|jj|j|}|dkrD|jq.n|j}|d|_|j||||||j}|j|q.W|jd|_WntjnXdS(sCalled when my socket is ready for reading.

        This accepts a connection and calls self.protocol() to handle the
        wire-level protocol.
        RiNis$Could not accept new connection (%s)i( RRtranget
disconnectingR;tacceptR7RJRR'RR$R#R%R&R(R/RR*R-RR_RRRRIRCRt	transportRTRtdeferr(	RDt
numAcceptstiRFRteR`RR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRRs@		"	
	
!cCsEt|_|j|jrAt|jd|j||_|jSdS(s
        Stop accepting connections on this port.

        This will shut down the socket and call self.connectionLost().  It
        returns a deferred which will fire successfully when the port is
        actually closed, or with a failure if an error occurs shutting down.
        iN(RORRPRR.RTRVtdeferred(RDtconnDone((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytloseConnections	
	cCs!tjd|j|jfdS(s.
        Log message for closing port
        s(%s Port %s Closed)N(R/Rt_typeR(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt_logConnectionLostMsgscCsj|jd|_tjj||t|_|jt	|`
|`z|jj
Wdt|_XdS(s'
        Cleans up the socket.
        N(RRIRR+RRVR
RRGROR;R_RtdoStopR(RDR}((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRVs
		
cCstj|jjS(sBReturns the name of my class, to prefix log entries with.
        (R1tqualRR(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRscCstjd|jjS(sNReturns an IPv4Address.

        This indicates the server's address.
        R(R,RR;R(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRsN(#RR	RZRR6tIListeningPortR;RRRRRRRRRRRIRR]RRRRRRR0RWR5RgRt
stopListeningRRVRR(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRs,
								G			t	ConnectorcBs&eZddZdZdZRS(cCs||_t|tjriytj|d}Wqitjk
re}tjdd||fqiXn||_||_	t
jj||||dS(NttcpRs%s (%r)(
Rt
isinstancettypestStringTypesR;t
getservbynameR7tServiceNameUnknownErrorRRR+t
BaseConnectorR](RDRRRttimeoutRRTR((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyR]s	#		cCs"t|j|j|j||jS(N(RRRRRT(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt_makeTransportscCstjd|j|jS(NR(R,RRR(RD((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pytgetDestinationsN(RR	RIR]RR(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyRs	(RRZRR;RRRAtzope.interfaceRttwisted.python.runtimeRttwisted.pythonRRttwisted.internet._newtlsRRRRRR
tImportErrorttwisted.internet._oldtlstobjectRterrnoRRRRRRRRRRRR RR!RR"RR#RR$R%R&R'R(ttwisted.python.win32RR)tosR*ttwisted.internetR+R,R-ttwisted.internet.taskR.R/R0R1ttwisted.python.utilR2ttwisted.internet.errorR3R4R5R6R7R8RHR\R[RRRRRRR(((s8/usr/lib/python2.7/dist-packages/twisted/internet/tcp.pyt<module>	s 
 
			",=