Package wtf :: Package impl :: Module _connection
[hide private]
[frames] | no frames]

Source Code for Module wtf.impl._connection

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2006-2012 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  Common Connection Implementation 
 19  ================================ 
 20   
 21  This module defines a connection abstraction. 
 22  """ 
 23  __author__ = u"Andr\xe9 Malo" 
 24  __docformat__ = "restructuredtext en" 
 25   
 26  import errno as _errno 
 27  import socket as _socket 
 28   
 29  from wtf import osutil as _osutil 
 30  from wtf import stream as _stream 
 31   
 32   
33 -class Connection(object):
34 """ 35 Connection abstraction 36 37 :IVariables: 38 - `_sock`: Actual connection socket 39 - `server_addr`: tuple of server address and port (the latter is -1 on 40 UNIX domain sockets) (``(addr, port)``) 41 - `remote_addr`: tuple of remote address and port (the latter is -1 on 42 UNIX domain sockets) (``(addr, port)``) 43 44 :Types: 45 - `_sock`: ``socket.socket`` 46 - `server_addr`: ``tuple`` 47 - `remote_addr`: ``tuple`` 48 """ 49 _sock = None 50
51 - def __init__(self, sock, peername):
52 """ 53 Initialization 54 55 :Parameters: 56 - `sock`: The actual connection socket 57 - `peername`: The peername (got from accept) 58 59 :Types: 60 - `sock`: ``socket.socket`` 61 - `peername`: ``str`` or ``tuple`` 62 """ 63 # first thing, in order to be able to close it cleanly 64 self._sock = sock 65 66 sock, peername = _osutil.disable_nagle(sock, peername) 67 68 sockname = sock.getsockname() 69 if isinstance(sockname, str): 70 sockname = sockname, -1 71 self.server_addr = sockname 72 if isinstance(peername, str): 73 peername = peername, -1 74 self.remote_addr = peername
75
76 - def __del__(self):
77 self.close()
78
79 - def close(self):
80 """ Close the connection """ 81 sock, self._sock = self._sock, None 82 if sock is not None: 83 try: 84 try: 85 sock.shutdown(_socket.SHUT_RDWR) 86 except _socket.error, e: 87 if e[0] != _errno.ENOTCONN: 88 raise 89 finally: 90 sock.close()
91
92 - def reader(self):
93 """ 94 Create a new reading stream for the socket 95 96 :return: reading stream 97 :rtype: ``file`` 98 """ 99 return _stream.GenericStream(_stream.MinimalSocketStream( 100 self._sock, _socket.SHUT_RD 101 ))
102
103 - def writer(self):
104 """ 105 Create a new writing stream for the socket 106 107 :return: writing stream 108 :rtype: ``file`` 109 """ 110 return _stream.GenericStream(_stream.MinimalSocketStream( 111 self._sock, _socket.SHUT_WR 112 ))
113
114 - def settimeout(self, timeout):
115 """ 116 Set a socket timeout for next operations 117 118 :Parameters: 119 - `timeout`: Socket timeout to set 120 121 :Types: 122 - `timeout`: ``float`` 123 """ 124 if timeout is not None: 125 timeout = float(timeout) 126 self._sock.settimeout(timeout)
127