def get_body(case): body = None if case.bodyText is not None: body = case.bodyText#.encode('UTF-8') elif case.bodyArgs is not None: if case.headers.__contains__('Content-Type') and 'x-www-form-urlencoded' in case.headers['Content-Type']: if utils.getPythonVersion() == '2': body = urllib.urlencode(case.bodyArgs) else: body = urllib.parse.urlencode(case.bodyArgs) if case.headers.__contains__('Content-Type') and 'json' in case.headers['Content-Type']: body = json.dumps(case.bodyArgs) return body
def html_reader(url_dir, data_identifier=[ "", ]): # create a subclass and override the handler methods # from https://docs.python.org/2/library/htmlparser.html urls = [] pyversion = utils.getPythonVersion() if pyversion == 2: from HTMLParser import HTMLParser #this seems not to work in python3 import urllib2, argparse response = urllib2.urlopen(url_dir) #for python2 elif pyversion == 3: from html.parser import HTMLParser #python 3 version from urllib.request import urlopen response = urlopen( url_dir ) #for python 3, should work but havne't tested this yet (as of 6/1/2020) else: print("This code only works with python version 2 or 3") print( "Python version is listed as {0}, please change".format(pyversion)) exit() class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): #print("Encountered a start tag: {0}".format(tag)) pass def handle_endtag(self, tag): #print("Encountered an end tag: {0}".format(tag)) pass def handle_data(self, data): #print("Encountered some data : {0}".format(data)) if any([ext in data for ext in data_identifier]): urls.append(data) raw_html = response.read() parser = MyHTMLParser() parser.feed( str(raw_html) ) #Need to convert bytes to str for python3, should work of for python2 also return raw_html, urls
import report import runner try: import httplib2 except ImportError as msg: import platform if platform.system()=="Windows": os.chdir('..\\external\\httplib2-0.9.1\\') cmd = 'python setup.py install' lines = utils.run_cmd(cmd) os.chdir('..\\..\\rest\\') if utils.getPythonVersion() == '2': reload(sys) sys.setdefaultencoding('utf8') logger = logging.getLogger("test.rest.rath") logging.config.fileConfig('./conf/rest-test-log.conf',disable_existing_loggers=False) starttime = time.time() def get_log_level(level_name): if level_name == 'debug': return logging.DEBUG if level_name == 'info': return logging.INFO if level_name == 'warn': return logging.WARN
def request(self, resource, method = "get", args = None, body = None, parts=None, headers={}): params = None path = resource headers['User-Agent'] = 'Basic Agent' BOUNDARY = r'00hoYUXOnLD5RQ8SKGYVgLLt64jejnMwtO7q8XE1' CRLF = r'\r\n' if parts: # Attempt to find the Mimetype headers['Content-Type']='multipart/form-data; boundary='+BOUNDARY encode_string = StringIO() for part in parts: if part.type != 'file': continue encode_string.write(r'--' + BOUNDARY) encode_string.write(CRLF) body = None if part.type == 'file': encode_string.write(r'Content-Disposition: form-data; name="%s"; filename="%s"' % (part.key, os.path.basename(part.text))) encode_string.write(CRLF) content_type = self.get_content_type(part.text) encode_string.write(r'Content-Type: %s' % content_type) encode_string.write(CRLF) encode_string.write(r'Content-Transfer-Encoding: base64') encode_string.write(CRLF) encode_string.write(CRLF) with open(part.text, "rb") as file: body = base64.b64encode(file.read()).decode() #body=str(file.read()) else: encode_string.write(r'Content-Disposition: form-data; name="%s"' % (part.key)) encode_string.write(CRLF) encode_string.write(r'Content-Type: %s' % part.contentType) encode_string.write(CRLF) encode_string.write(CRLF) body = part.text encode_string.write(body) encode_string.write(CRLF) encode_string.write(r'--' + BOUNDARY + r'--' + CRLF) body = encode_string.getvalue() headers['Content-Length'] = str(len(body)) elif body: if not headers.get('Content-Type', None): headers['Content-Type']='text/xml' headers['Content-Length'] = str(len(body)) else: if not headers.get('Content-Type', None): headers['Content-Type']='text/xml' domain=self.base_url.split("//")[1].split("/")[0] if args: if utils.getPythonVersion() == '2': path += r"?" + urllib.urlencode(args) else: path += r"?" + urlparse.urlencode(args) request_path = [] if self.path != "/": if self.path.endswith('/'): request_path.append(self.path[:-1]) else: request_path.append(self.path) if path.startswith('/'): request_path.append(path[1:]) else: request_path.append(path) # if self.host=='211.136.86.203':# kuan chang com IP # url = r"%s://%s%s" % (self.scheme, self.host, ''.join(request_path)) # else:# normal flow url = r"%s://%s%s" % (self.scheme, self.host, ''.join(request_path)) url = url.rstrip('/') logger.info('request: %s' % url) resp, content = self.h.request(url, method.upper(), body=body, headers=headers ) encoding = 'UTF-8' if 'content-type' in resp and 'charset' in resp['content-type']: items = resp['content-type'].split('=') encoding = items[1] if 'content-type' in resp and 'application' in resp['content-type'] and 'json' not in resp['content-type']: encoding = None if 'content-type' in resp and 'image' in resp['content-type']: encoding = None if encoding is not None: return {r'headers':resp, r'body':content.decode(encoding)} else: # not return binary data return {r'headers':resp, r'body':''}
def request(self, resource, method="get", args=None, body=None, parts=None, headers={}): params = None path = resource headers['User-Agent'] = 'Basic Agent' BOUNDARY = r'00hoYUXOnLD5RQ8SKGYVgLLt64jejnMwtO7q8XE1' CRLF = r'\r\n' if parts: # Attempt to find the Mimetype headers[ 'Content-Type'] = 'multipart/form-data; boundary=' + BOUNDARY encode_string = StringIO() for part in parts: if part.type != 'file': continue encode_string.write(r'--' + BOUNDARY) encode_string.write(CRLF) body = None if part.type == 'file': encode_string.write( r'Content-Disposition: form-data; name="%s"; filename="%s"' % (part.key, os.path.basename(part.text))) encode_string.write(CRLF) content_type = self.get_content_type(part.text) encode_string.write(r'Content-Type: %s' % content_type) encode_string.write(CRLF) encode_string.write(r'Content-Transfer-Encoding: base64') encode_string.write(CRLF) encode_string.write(CRLF) with open(part.text, "rb") as file: body = base64.b64encode(file.read()).decode() #body=str(file.read()) else: encode_string.write( r'Content-Disposition: form-data; name="%s"' % (part.key)) encode_string.write(CRLF) encode_string.write(r'Content-Type: %s' % part.contentType) encode_string.write(CRLF) encode_string.write(CRLF) body = part.text encode_string.write(body) encode_string.write(CRLF) encode_string.write(r'--' + BOUNDARY + r'--' + CRLF) body = encode_string.getvalue() headers['Content-Length'] = str(len(body)) elif body: if not headers.get('Content-Type', None): headers['Content-Type'] = 'text/xml' headers['Content-Length'] = str(len(body)) else: if not headers.get('Content-Type', None): headers['Content-Type'] = 'text/xml' domain = self.base_url.split("//")[1].split("/")[0] if args: if utils.getPythonVersion() == '2': path += r"?" + urllib.urlencode(args) else: path += r"?" + urlparse.urlencode(args) request_path = [] if self.path != "/": if self.path.endswith('/'): request_path.append(self.path[:-1]) else: request_path.append(self.path) if path.startswith('/'): request_path.append(path[1:]) else: request_path.append(path) # if self.host=='211.136.86.203':# kuan chang com IP # url = r"%s://%s%s" % (self.scheme, self.host, ''.join(request_path)) # else:# normal flow url = r"%s://%s%s" % (self.scheme, self.host, ''.join(request_path)) url = url.rstrip('/') logger.info('request: %s' % url) resp, content = self.h.request(url, method.upper(), body=body, headers=headers) encoding = 'UTF-8' if 'content-type' in resp and 'charset' in resp['content-type']: items = resp['content-type'].split('=') encoding = items[1] if 'content-type' in resp and 'application' in resp[ 'content-type'] and 'json' not in resp['content-type']: encoding = None if 'content-type' in resp and 'image' in resp['content-type']: encoding = None if encoding is not None: return {r'headers': resp, r'body': content.decode(encoding)} else: # not return binary data return {r'headers': resp, r'body': ''}