コード例 #1
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''

        connection = self.get_connection(request)
        connection.putrequest(request.method, request.path)
        self.send_request_headers(connection, request.headers)
        self.send_request_body(connection, request.body)

        resp = connection.getresponse()
        self.status = int(resp.status)
        self.message = resp.reason
        self.respheader = headers = resp.getheaders()
        respbody = None
        if resp.length is None:
            respbody = resp.read()
        elif resp.length > 0:
            respbody = resp.read(resp.length)

        response = HTTPResponse(int(resp.status), resp.reason, headers,
                                respbody)
        if self.status >= 300:
            raise HTTPError(self.status, self.message, self.respheader,
                            respbody)

        return response
コード例 #2
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''

        connection = self.get_connection(request)
        connection.putrequest(request.method, request.path)

        if sys.platform.lower().startswith('win'):
            if self.proxy_host and self.proxy_user:
                connection.set_proxy_credentials(self.proxy_user, self.proxy_password)

        self.send_request_headers(connection, request.headers)
        self.send_request_body(connection, request.body)

        resp = connection.getresponse()
        self.status = int(resp.status)
        self.message = resp.reason
        self.respheader = headers = resp.getheaders()
        respbody = None
        if resp.length is None:
            respbody = resp.read()
        elif resp.length > 0:
            respbody = resp.read(resp.length)
    
        response = HTTPResponse(int(resp.status), resp.reason, headers, respbody)
        if self.status >= 300:
            raise HTTPError(self.status, self.message, self.respheader, respbody)
        
        return response
コード例 #3
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''
        connection = self.get_connection(request)
        try:
            connection.putrequest(request.method, request.path)

            if not self.use_httplib:
                if self.proxy_host and self.proxy_user:
                    connection.set_proxy_credentials(self.proxy_user,
                                                     self.proxy_password)

            self.send_request_headers(connection, request.headers)
            self.send_request_body(connection, request.body)

            if DEBUG_REQUESTS and request.body:
                print('request:')
                try:
                    print(request.body)
                except:
                    pass

            resp = connection.getresponse()
            self.status = int(resp.status)
            self.message = resp.reason
            self.respheader = headers = resp.getheaders()

            # for consistency across platforms, make header names lowercase
            for i, value in enumerate(headers):
                headers[i] = (value[0].lower(), value[1])

            respbody = None
            if resp.length is None:
                respbody = resp.read()
            elif resp.length > 0:
                respbody = resp.read(resp.length)

            if DEBUG_RESPONSES and respbody:
                print('response:')
                try:
                    print(respbody)
                except:
                    pass

            response = HTTPResponse(int(resp.status), resp.reason, headers,
                                    respbody)
            if self.status == 307:
                new_url = urlparse(dict(headers)['location'])
                request.host = new_url.hostname
                request.path = new_url.path
                request.path, request.query = _update_request_uri_query(
                    request)
                return self.perform_request(request)
            if self.status >= 300:
                raise HTTPError(self.status, self.message, self.respheader,
                                respbody)

            return response
        finally:
            connection.close()
コード例 #4
0
def _get_token(request, account_key, issuer):
    ''' 
    Returns token for the request. 
    
    request: the service bus service request.
    account_key: service bus access key
    issuer: service bus issuer
    '''
    wrap_scope = 'http://' + request.host + request.path

    # Check whether has unexpired cache, return cached token if it is still usable.
    if _tokens.has_key(wrap_scope):
        token = _tokens[wrap_scope]
        if not _token_is_expired(token):
            return token

    #get token from accessconstrol server
    request_body = ('wrap_name=' + urllib2.quote(issuer) + '&wrap_password='******'&wrap_scope=' +
                    urllib2.quote('http://' + request.host + request.path))
    host = request.host.replace('.servicebus.', '-sb.accesscontrol.')
    if sys.platform.lower().startswith('win'):
        import azure.http.winhttp
        connection = azure.http.winhttp._HTTPConnection(host, protocol='https')
    else:
        connection = httplib.HTTPSConnection(host)
    connection.putrequest('POST', '/WRAPv0.9')
    connection.putheader('Content-Length', len(request_body))
    connection.putheader('User-Agent', _USER_AGENT_STRING)
    connection.endheaders()
    connection.send(request_body)
    resp = connection.getresponse()
    token = ''
    if int(resp.status) >= 200 and int(resp.status) < 300:
        if resp.length:
            token = resp.read(resp.length)
        else:
            raise HTTPError(resp.status, resp.reason, resp.getheaders(), None)
    else:
        raise HTTPError(resp.status, resp.reason, resp.getheaders(), None)

    token = urllib2.unquote(token[token.find('=') + 1:token.rfind('&')])
    _tokens[wrap_scope] = token

    return token
コード例 #5
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''
        connection = self.get_connection(request)
        try:
            connection.putrequest(request.method, request.path)

            if not self.use_httplib:
                if self.proxy_host and self.proxy_user:
                    connection.set_proxy_credentials(self.proxy_user,
                                                     self.proxy_password)

            self.send_request_headers(connection, request.headers)
            self.send_request_body(connection, request.body)

            resp = connection.getresponse()
            self.status = int(resp.status)
            self.message = resp.reason
            self.respheader = headers = resp.getheaders()

            # for consistency across platforms, make header names lowercase
            for i, value in enumerate(headers):
                headers[i] = (value[0].lower(), value[1])

            respbody = None
            if resp.length is None:
                respbody = resp.read()
            elif resp.length > 0:
                respbody = resp.read(resp.length)

            response = HTTPResponse(int(resp.status), resp.reason, headers,
                                    respbody)
            if self.status >= 300:
                raise HTTPError(self.status, self.message, self.respheader,
                                respbody)

            return response
        finally:
            connection.close()
コード例 #6
0
    def commit_batch_requests(self):
        ''' Commits the batch requests. '''

        batch_boundary = b'batch_' + _new_boundary()
        changeset_boundary = b'changeset_' + _new_boundary()

        # Commits batch only the requests list is not empty.
        if self.batch_requests:
            request = HTTPRequest()
            request.method = 'POST'
            request.host = self.batch_requests[0].host
            request.path = '/$batch'
            request.headers = [
                ('Content-Type', 'multipart/mixed; boundary=' + \
                    batch_boundary.decode('utf-8')),
                ('Accept', 'application/atom+xml,application/xml'),
                ('Accept-Charset', 'UTF-8')]

            request.body = b'--' + batch_boundary + b'\n'
            request.body += b'Content-Type: multipart/mixed; boundary='
            request.body += changeset_boundary + b'\n\n'

            content_id = 1

            # Adds each request body to the POST data.
            for batch_request in self.batch_requests:
                request.body += b'--' + changeset_boundary + b'\n'
                request.body += b'Content-Type: application/http\n'
                request.body += b'Content-Transfer-Encoding: binary\n\n'
                request.body += batch_request.method.encode('utf-8')
                request.body += b' http://'
                request.body += batch_request.host.encode('utf-8')
                request.body += batch_request.path.encode('utf-8')
                request.body += b' HTTP/1.1\n'
                request.body += b'Content-ID: '
                request.body += str(content_id).encode('utf-8') + b'\n'
                content_id += 1

                # Add different headers for different type requests.
                if not batch_request.method == 'DELETE':
                    request.body += \
                        b'Content-Type: application/atom+xml;type=entry\n'
                    for name, value in batch_request.headers:
                        if name == 'If-Match':
                            request.body += name.encode('utf-8') + b': '
                            request.body += value.encode('utf-8') + b'\n'
                            break
                    request.body += b'Content-Length: '
                    request.body += str(len(
                        batch_request.body)).encode('utf-8')
                    request.body += b'\n\n'
                    request.body += batch_request.body + b'\n'
                else:
                    for name, value in batch_request.headers:
                        # If-Match should be already included in
                        # batch_request.headers, but in case it is missing,
                        # just add it.
                        if name == 'If-Match':
                            request.body += name.encode('utf-8') + b': '
                            request.body += value.encode('utf-8') + b'\n\n'
                            break
                    else:
                        request.body += b'If-Match: *\n\n'

            request.body += b'--' + changeset_boundary + b'--' + b'\n'
            request.body += b'--' + batch_boundary + b'--'

            request.path, request.query = _update_request_uri_query(request)
            request.headers = _update_storage_table_header(request)
            self.authentication.sign_request(request)

            # Submit the whole request as batch request.
            response = self.perform_request(request)
            if response.status >= 300:
                raise HTTPError(response.status, _ERROR_BATCH_COMMIT_FAIL,
                                self.respheader, response.body)

            # http://www.odata.org/documentation/odata-version-2-0/batch-processing/
            # The body of a ChangeSet response is either a response for all the
            # successfully processed change request within the ChangeSet,
            # formatted exactly as it would have appeared outside of a batch,
            # or a single response indicating a failure of the entire ChangeSet.
            responses = self._parse_batch_response(response.body)
            if responses and responses[0].status >= 300:
                self._report_batch_error(responses[0])
コード例 #7
0
ファイル: batchclient.py プロジェクト: jacalata/optimization
    def commit_batch_requests(self):
        ''' Commits the batch requests. '''

        batch_boundary = 'batch_a2e9d677-b28b-435e-a89e-87e6a768a431'
        changeset_boundary = 'changeset_8128b620-b4bb-458c-a177-0959fb14c977'
        
        #Commits batch only the requests list is not empty.
        if self.batch_requests:
            request = HTTPRequest()
            request.method = 'POST'
            request.host = self.batch_requests[0].host
            request.path = '/$batch'
            request.headers = [('Content-Type', 'multipart/mixed; boundary=' + batch_boundary),
                              ('Accept', 'application/atom+xml,application/xml'),
                              ('Accept-Charset', 'UTF-8')]
            
            request.body = '--' + batch_boundary + '\n'
            request.body += 'Content-Type: multipart/mixed; boundary=' + changeset_boundary + '\n\n'

            content_id = 1

            # Adds each request body to the POST data.
            for batch_request in self.batch_requests:
                request.body += '--' + changeset_boundary + '\n'
                request.body += 'Content-Type: application/http\n'
                request.body += 'Content-Transfer-Encoding: binary\n\n'

                request.body += batch_request.method + ' http://' + batch_request.host + batch_request.path + ' HTTP/1.1\n'
                request.body += 'Content-ID: ' + str(content_id) + '\n'
                content_id += 1
                
                # Add different headers for different type requests.
                if not batch_request.method == 'DELETE':
                    request.body += 'Content-Type: application/atom+xml;type=entry\n'
                    request.body += 'Content-Length: ' + str(len(batch_request.body)) + '\n\n'
                    request.body += batch_request.body + '\n'
                else:
                    find_if_match = False
                    for name, value in batch_request.headers:
                        #If-Match should be already included in batch_request.headers, but in case it is missing, just add it.
                        if name == 'If-Match':
                            request.body += name + ': ' + value + '\n\n'
                            break
                    else:
                        request.body += 'If-Match: *\n\n'

            request.body += '--' + changeset_boundary + '--' + '\n'
            request.body += '--' + batch_boundary + '--' 

            request.path, request.query = _update_request_uri_query(request)
            request.headers = _update_storage_table_header(request)
            auth = _sign_storage_table_request(request, 
                                        self.account_name, 
                                        self.account_key)
            request.headers.append(('Authorization', auth))

            #Submit the whole request as batch request.
            response = self.perform_request(request)
            resp = response.body

            if response.status >= 300:
                raise HTTPError(status, azure._ERROR_BATCH_COMMIT_FAIL, self.respheader, resp)
            return resp
コード例 #8
0
    def commit_batch_requests(self):
        ''' Commits the batch requests. '''

        batch_boundary = b'batch_' + _new_boundary()
        changeset_boundary = b'changeset_' + _new_boundary()

        # Commits batch only the requests list is not empty.
        if self.batch_requests:
            request = HTTPRequest()
            request.method = 'POST'
            request.host = self.batch_requests[0].host
            request.path = '/$batch'
            request.headers = [
                ('Content-Type', 'multipart/mixed; boundary=' + \
                    batch_boundary.decode('utf-8')),
                ('Accept', 'application/atom+xml,application/xml'),
                ('Accept-Charset', 'UTF-8')]

            request.body = b'--' + batch_boundary + b'\n'
            request.body += b'Content-Type: multipart/mixed; boundary='
            request.body += changeset_boundary + b'\n\n'

            content_id = 1

            # Adds each request body to the POST data.
            for batch_request in self.batch_requests:
                request.body += b'--' + changeset_boundary + b'\n'
                request.body += b'Content-Type: application/http\n'
                request.body += b'Content-Transfer-Encoding: binary\n\n'
                request.body += batch_request.method.encode('utf-8')
                request.body += b' http://'
                request.body += batch_request.host.encode('utf-8')
                request.body += batch_request.path.encode('utf-8')
                request.body += b' HTTP/1.1\n'
                request.body += b'Content-ID: '
                request.body += str(content_id).encode('utf-8') + b'\n'
                content_id += 1

                # Add different headers for different type requests.
                if not batch_request.method == 'DELETE':
                    request.body += \
                        b'Content-Type: application/atom+xml;type=entry\n'
                    request.body += b'Content-Length: '
                    request.body += str(len(
                        batch_request.body)).encode('utf-8')
                    request.body += b'\n\n'
                    request.body += batch_request.body + b'\n'
                else:
                    for name, value in batch_request.headers:
                        # If-Match should be already included in
                        # batch_request.headers, but in case it is missing,
                        # just add it.
                        if name == 'If-Match':
                            request.body += name.encode('utf-8') + b': '
                            request.body += value.encode('utf-8') + b'\n\n'
                            break
                    else:
                        request.body += b'If-Match: *\n\n'

            request.body += b'--' + changeset_boundary + b'--' + b'\n'
            request.body += b'--' + batch_boundary + b'--'

            request.path, request.query = _update_request_uri_query(request)
            request.headers = _update_storage_table_header(request)
            auth = _sign_storage_table_request(request, self.account_name,
                                               self.account_key)
            request.headers.append(('Authorization', auth))

            # Submit the whole request as batch request.
            response = self.perform_request(request)
            resp = response.body

            if response.status >= 300:
                raise HTTPError(response.status, _ERROR_BATCH_COMMIT_FAIL,
                                self.respheader, resp)
            return resp