示例#1
0
    def do_send(self):
        addr = (self.host, self.port)
        is_ssl = (self.uri.scheme == "https")
        route = (addr, is_ssl, self.filters, self.ssl_args)
        self._pool = self.conn_manager.get_pool(route)
        tries = 2
        while True:
            try:
                if not self._conn:
                    # get new connection
                    self._conn = self._pool.request()
                # socket
                s = self._conn.socket()

                self.headers = copy.copy(self.bheaders)

                self.headers.extend(self._conn.headers)

                # apply on_request filters
                self.filters.apply("on_request", self, tries)

                # build request headers
                req_headers = self._req_headers()
                self.req_headers = req_headers

                # send request
                log.info('Start request: %s %s', self.method, self.url)
                log.debug("Request headers: [%s]", req_headers)
                
                s.sendall("".join(req_headers))
                
                if self.body is not None:
                    if hasattr(self.body, 'read'):
                        if hasattr(self.body, 'seek'): self.body.seek(0)
                        sock.sendfile(s, self.body, self.chunked)
                    elif isinstance(self.body, types.StringTypes):
                        sock.send(s, self.body, self.chunked)
                    else:
                        sock.sendlines(s, self.body, self.chunked)
                        
                    if self.chunked: # final chunk
                        sock.send_chunk(s, "")
                        
                return self.start_response()
            except socket.gaierror, e:
                self.shutdown_connection()
                raise RequestError(str(e))
            except socket.timeout, e:
                if tries < 0:
                    raise RequestTimeout(str(e))
                self.shutdown_connection()
示例#2
0
文件: client.py 项目: czue/restkit
 def do_send(self):
     tries = 2
     while True:
         try:
             # get socket
             self._sock = self.make_connection()
             
             # apply on request filters
             self.filters.apply("on_request", self)
             
             # build request headers
             self.req_headers = req_headers = self._req_headers()
             
             # send request
             log.info('Start request: %s %s', self.method, self.url)
             log.debug("Request headers: [%s]", req_headers)
             
             self._sock.sendall("".join(req_headers))
             
             if self.body is not None:
                 if hasattr(self.body, 'read'):
                     if hasattr(self.body, 'seek'): self.body.seek(0)
                     sock.sendfile(self._sock, self.body, self.chunked)
                 elif isinstance(self.body, types.StringTypes):
                     sock.send(self._sock, self.body, self.chunked)
                 else:
                     sock.sendlines(self._sock, self.body, self.chunked)
                     
                 if self.chunked: # final chunk
                     sock.send_chunk(self._sock, "")
                     
             return self.start_response()
         except socket.gaierror, e:
             self.clean_connections()
             raise
         except socket.error, e:
             if e[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EPIPE,
                         errno.ECONNREFUSED) or tries <= 0:
                 self.clean_connections()
                 raise
             if e[0] == errno.EPIPE:
                 log.debug("Got EPIPE")
                 self.clean_connections()