def establishSession(self, url, username, password, selector='/cdb/login'): self.host = url self.username = username self.password = password self.sessionCookie = self.loadSession() if self.sessionCookie is not None: return # Could not load session. try: # User will be asked for username/password if they are not # provided. data = { 'username': self.getUsername(username), 'password': self.getPassword(password) } self.logger.debug('Establishing session for user %s @ %s)' % (username, url)) (response, responseData) = self.sendRequest( url='%s%s' % (url, selector), method='POST', contentType='application/x-www-form-urlencoded', data=urllib.urlencode(data)) except urllib2.URLError, ex: self.logger.exception('%s' % ex) raise UrlError(exception=ex)
def sendRequest(self, url, method, contentType='html', data={}): """ Send http request without cookies. """ if url.find('://') < 0: url = '%s%s' % (self.host, url) parsedUrl = urlparse.urlparse(url) protocol = parsedUrl[0] path = parsedUrl[2] self.logger.debug('Sending request: %s' % url) encodedData = '' if data is not None: if type(data) == types.DictType and len(data): encodedData = urllib.urlencode(data) contentType = 'application/x-www-form-urlencoded' elif type(data) == types.StringType: encodedData = data request = urllib2.Request(url, data=encodedData) request.get_method = lambda: method request.add_header('Content-Type', contentType) request.add_header('Content-Length', str(len(data))) if self.sessionCookie != None: request.add_header('Cookie', self.sessionCookie) try: opener = self.getUrlOpener(protocol) response = opener.open(request) except urllib2.HTTPError, ex: # If we see cdb headers, cdb exception will be thrown, # otherwise we'll throw UrlError self.checkResponseHeadersForErrors(ex.hdrs) self.logger.exception('%s' % ex) raise UrlError(exception=ex)
def sendRequest(self, url, method, contentType='html', data={}): """ Send http request without cookies. """ if url.find('://') < 0: url = '%s%s' % (self.host, url) parsedUrl = urllib.parse.urlparse(url) protocol = parsedUrl[0] path = parsedUrl[2] self.logger.debug('Sending request: %s' % url) encodedData = '' if data is not None: if type(data) == dict and len(data): encodedData = urllib.parse.urlencode(data) contentType = 'application/x-www-form-urlencoded' elif type(data) == bytes: encodedData = data elif UrlLibFileStreamUtility.isStreamDataObject(data): encodedData = data contentType = 'application/octet-stream' # In case data was used on invalid session encodedData.seek(0) request = urllib.request.Request(url, data=encodedData) request.get_method = lambda: method request.add_header('Content-Type', contentType) request.add_header('Content-Length', str(len(data))) if self.sessionCookie != None: request.add_header('Cookie', self.sessionCookie) try: opener = self.getUrlOpener(protocol) response = opener.open(request) except urllib.error.HTTPError as ex: # If we see cdb headers, cdb exception will be thrown, # otherwise we'll throw UrlError self.checkResponseHeadersForErrors(ex.hdrs) self.logger.exception('%s' % ex) raise UrlError(exception=ex) except urllib.error.URLError as ex: self.logger.exception('%s' % ex) raise UrlError(exception=ex) # Check headers for errors and update session cookie sessionCookie = self.checkResponseHeadersForErrorsAndSaveSession( response.headers) if sessionCookie != None: self.sessionCookie = sessionCookie responseData = response.read() return (response, responseData)
request.add_header('Content-Type', contentType) request.add_header('Content-Length', str(len(data))) if self.sessionCookie != None: request.add_header('Cookie', self.sessionCookie) try: opener = self.getUrlOpener(protocol) response = opener.open(request) except urllib2.HTTPError, ex: # If we see cdb headers, cdb exception will be thrown, # otherwise we'll throw UrlError self.checkResponseHeadersForErrors(ex.hdrs) self.logger.exception('%s' % ex) raise UrlError(exception=ex) except urllib2.URLError, ex: self.logger.exception('%s' % ex) raise UrlError(exception=ex) # Check headers for errors and update session cookie sessionCookie = self.checkResponseHeadersForErrorsAndSaveSession( response.headers) if sessionCookie != None: self.sessionCookie = sessionCookie responseData = response.read() return (response, responseData) def sendSessionRequest(self, url, method, contentType='html', data={}): """ Send session request. """ if self.sessionCookie is None: self.establishSession(self.host, self.username, self.password) try: return self.sendRequest(url, method, contentType, data)