def request_to_sakila(path="", body=None, method='GET', headers=None):
    from http.client import HTTPConnection, HTTPResponse
    import json_with_dates
    #print('path', path)
    if body:
        jbody = json_with_dates.dumps(body)
        enjbody = jbody.encode()
    else:
        enjbody = None
    hconn = HTTPConnection('localhost', 80)
    if headers:
        hconn.request(method, '/cgi-bin/sakila_rest/sakila.py' + path, body=enjbody,
            headers=headers)
    else:
        hconn.request(method, '/cgi-bin/sakila_rest/sakila.py' + path, body=enjbody)
    resp = hconn.getresponse()
    #print('status', resp.status)
    bodyJ = resp.read()
    #print('body',body)
    bodystr = bodyJ.decode()
    if resp.status == 200:
        #data = json_with_dates.loads(bodystr)
        #return data #json_with_dates.loads(hconn.getresponse().read().decode())
        body = json_with_dates.loads(bodystr)
        return ('K',body)
    else:
        return ('E', resp.status, bodystr)
        #print("response status", resp.status)
        #print('bodystr', bodystr)
Exemple #2
0
def download_match(m, quiet=False):
    conn = HTTPConnection('www.thebluealliance.com')
    conn.request('GET', '/api/v1/match/details?match=' + m, headers=DEF_HEADERS)

    r = conn.getresponse()
    answer = r.read().decode('utf-8')
    return answer
 def _get_content(self, url):
     con = HTTPConnection(self.site_url)
     encoded_url = quote(unquote(url), "/=?&")
     print(encoded_url)
     con.request('GET', encoded_url)
     response = con.getresponse()
     return response.read()
Exemple #4
0
def geoencode(city, country):
	msg = {
		'q': city+', '+country,
		'output': 'json',
		'oe': 'utf8',
		'sensor': 'true_or_false',
		'key': 'ABQIAAAAzT63NsNCcpw5Af6mLso2FxSgcUpOcSOIawgl-Zf9E7s32CuX-RQF5sRXdcMDlQa3cpL8L_S63UUpFA',
	}

	conn = HTTPConnection('maps.google.com')
	conn.request('GET', '/maps/geo?'+urlencode(msg))
	res = conn.getresponse()
	data = res.read()
	
	conn.close()
	data = json.loads(data.decode("utf-8"))
	
	lat, lng = 0, 0
	loc_name = city
	if data and 'Status' in data and 'code' in data['Status'] and data['Status']['code'] == 200 and 'Placemark' in data:		
		for p in data['Placemark']:
			if 'AddressDetails' in p and 'Country' in p['AddressDetails'] and 'CountryName' in p['AddressDetails']['Country'] \
			and p['AddressDetails']['Country']['CountryName'].lower() == country and 'Point' in p and 'coordinates' in p['Point']:
				lng = p['Point']['coordinates'][0]
				lat = p['Point']['coordinates'][1]
				break
	return lat, lng,loc_name
Exemple #5
0
def download_regionals(year):
    conn = HTTPConnection('www.thebluealliance.com')
    conn.request('GET', '/api/v1/events/list?year=' + year, headers=DEF_HEADERS)

    r = conn.getresponse()
    answer = r.read().decode('utf-8')
    return answer
Exemple #6
0
 def test_requesting_basebackup(self, pghoard):
     nonexistent_basebackup = "/{}/archive/basebackup".format(pghoard.test_site)
     conn = HTTPConnection(host="127.0.0.1", port=pghoard.config["http_port"])
     conn.request("PUT", nonexistent_basebackup)
     status = conn.getresponse().status
     assert status == 201
     assert pghoard.requested_basebackup_sites == {"test_requesting_basebackup"}
Exemple #7
0
    def do_remote(self, path, body=None, headers={}):
        """和远程主机通讯,同时处理501错误(不是代理请求)"""
        if self.handleHeaders:
            del self.headers["Proxy-Connection"]
            del self.headers["Keep-Alive"]
            del self.headers["Proxy-Authorization"]
            self.headers["Connection"] = "close"

        if not path.scheme:
            self.send_response(501)
            self.send_header("Server", self.server_version)
            self.send_header("Content-Type", "text/html; charset=utf-8")
            if self.command in ("GET", "POST"):
                content = directError.format(
                    server_version=self.server_version,
                    domain=self.server.server_address[0],
                    port=self.server.server_address[1],
                ).encode("utf-8")
            else:
                content = "Unknown Error"
            self.send_header("Content-Length", str(len(content)))
            self.end_headers()
            self.wfile.write(content)
            self.remoteResponse = None
            return

        client = HTTPConnection(path.netloc)
        headers = dict(headers)
        # 有些网站,比如 WebQQ,在 cookie 中插入了非 ASCII 字符
        # XXX:如果 UTF-8 不适合怎么办?
        for i in headers:
            headers[i] = headers[i].encode("utf-8")
        client.request(self.command, path.getpath(), body, headers)
        self.remoteResponse = client.getresponse()
        self.getdata = self.remoteResponse.read()
Exemple #8
0
def checkAuth(usr):
	from http.client import HTTPConnection
	from urllib.parse import urlencode

	hdrs = {
		'Content-Type': 'application/x-www-form-urlencoded',
		'Via': 'SMS',
	}
	from base64 import b64encode
	hdrs['Authorization'] = b'Basic ' + b64encode(usr.encode() + b':b87410354627d7f999a52fef67bb608e')
	#print(url, params, hdrs)

	conn = HTTPConnection(settings.API_DOMAIN)
	conn.request('POST', '/' + 'get/interests/list', '', hdrs)
	res = conn.getresponse()
	data = '';
	if res.status == 401:
		data = res.read()
		data = str(data, encoding='utf-8')
		print('Wrong response status:')
		#print(' Command: {}'.format(url))
		print(' Status: {}'.format(res.status))
		print(' Data: {}'.format(data))
		data = '401'
	conn.close()
	return data
Exemple #9
0
def lookup_unspent_outputs(bitcoin_addresses):
    conn = HTTPConnection('blockchain.info')
    conn.request('GET', '/unspent?active={}'.format('|'.join(bitcoin_addresses)))
    result = json.loads(conn.getresponse().read().decode('utf8'))
    unspent = defaultdict(list)

    for u in result['unspent_outputs']:
        program_bytes = Bitcoin.hexstring_to_bytes(u['script'], reverse=False)
        scriptPubKey, _ = script.Script.unserialize(program_bytes, len(program_bytes))
        address = None

        # Try to extract the address from the scriptpubkey program
        if len(scriptPubKey.program) == 6 and \
           scriptPubKey.program[0][0] == script.OP_DUP and scriptPubKey.program[1][0] == script.OP_HASH160 and \
           scriptPubKey.program[2][0] == 20 and \
           scriptPubKey.program[4][0] == script.OP_EQUALVERIFY and scriptPubKey.program[5][0] == script.OP_CHECKSIG:
                address = scriptPubKey.program[3]
        elif len(scriptPubKey.program) == 3 and scriptPubKey.program[2][0] == script.OP_CHECKSIG:
            if scriptPubKey.program[2][0] in (0x04, 0x03, 0x02):
                address = base58.decode_to_bytes(addressgen.generate_address(scriptPubKey.program[1], version=0))[-24:-4]

        if address is not None:
            i = 0
            while address[i] == 0:
                i += 1
            address = '1' + ('1' * i) + addressgen.base58_check(address, version=0)
            unspent[address].append(u)
    return unspent
        def retrieve_resources(server):
            http_conn = HTTPConnection(server.hostname, server.port)

            http_conn.request("GET", endpoint, headers={"Content-Type": "application/json"})
            results["response"] = http_conn.getresponse()

            http_conn.close()
        def create_resource(server):
            for resource_body in expected_resources:
                results["response"] = None

                http_conn = HTTPConnection(server.hostname, server.port)

                create_subresources = resource_body == expected_resources[0]

                resource_body = json.dumps(resource_body).encode()

                http_conn.request("POST", self._endpoint, resource_body,
                                  headers={"Content-Type": "application/json",
                                           "Content-Length": len(resource_body)})
                results["response"] = http_conn.getresponse()

                http_conn.close()

                location = results["response"].headers["Location"]
                self._locations_delete.append(location)

                resource_id = int(location.split("/")[-1])
                self._expected_resources[len(self._locations_delete) - 1]["id"] = resource_id

                if not create_subresources:
                    continue

                for subresource_body in self._expected_subresources:
                    self._create_subresource_for_resource(subresource_body, resource_id, server)
        def request_options(server):
            http_conn = HTTPConnection(server.hostname, server.port)

            http_conn.request("OPTIONS", endpoint, headers={"Access-Control-Request-Headers": "Header2"})
            results["response"] = http_conn.getresponse()

            http_conn.close()
    def _create_subresource_for_resource(self, subresource_body, resource_id, server):
        '''This method creates the given subresource and assign it to the given resource unique identifier.'''

        subresource_body["resource_id"] = resource_id

        http_conn = HTTPConnection(server.hostname, server.port)
        http_conn.connect()

        http_conn.request("POST", self._endpoint_subresource_latest, json.dumps(subresource_body).encode(),
                          headers={"Content-Type": "application/json"})

        response = http_conn.getresponse()

        http_conn.close()

        self.assertEqual(201, response.status)
        self.assertEqual("application/json; charset=UTF-8", response.headers["Content-Type"])
        self.assertEqual("0", response.headers["Content-Length"])

        location = response.headers["Location"]
        self.assertIsNotNone(location)

        self._locations_subresource_delete.insert(0, location)

        subresource_id = int(location.split("/")[-1])
        subresource_body["id"] = subresource_id
Exemple #14
0
    def render(self, formula):
        # Prepare the formula
        formula = formula.replace("$", "")
        encoded_formula = formula.replace("%", "[comment]").replace("+", "%2B")
        display_formula = formula.replace("\n", "")
        print('Rendering: %s ...' % display_formula)

        # Prepare POST request to QuickLaTeX via ProblemSetMarmoset
        # (for added processing)
        params = urlencode({
            'engine': 'quicklatex',
            'input': encoded_formula,
        })
        headers = {"Content-type": "application/x-www-form-urlencoded",
                   "Accept": "text/plain"}
        conn = HTTPConnection("www.problemsetmarmoset.com")

        # Make the request
        conn.request("POST", "/latex/render.php", params, headers)
        response = conn.getresponse()
        img_url = response.read()

        # Display as Markdown image
        rendered_tex = '![{0}]({1} "{0}")\n'.format(display_formula,
                                                    img_url.decode('utf-8'))
        return rendered_tex
Exemple #15
0
def index():
    form = LineConfigForm()
    configs_lines_api_url = os.path.join(current_app.config['DAC_HOST_URL'],
                                         current_app.config['DAC_API_CONFIGS_LINES_URL'])
    try:
        conn = HTTPConnection(current_app.config['DAC_HOST_URL'])
        conn.request("GET", configs_lines_api_url)
        resp = conn.getresponse()
        if resp.code == 200:
            data = json.loads(resp.read().decode())
        else:
            raise HTTPException()
    except HTTPException:
        data = {}
        flash(_("Can not connect to DAC server."))
    except ConnectionError:
        data = {}
        flash(_("Can not connect to DAC server."))
    finally:
        conn.close()

    if data != {}:
        lines = [line for line in data['data']['configs'].keys()]
        configs = data['data']['configs']
    else:
        lines = []
        configs = {}

    return render_template('config_view.html', form=form, data=configs, lines=lines, totail_lines=form.line_no.choices)
def get_contents(user):
    '''
    得到目录的方法:首先,根据首页的信息可以知道目录一共有多少页,然后构造页数
    大于实际页数的URL,便可以直接得到所有博客的目录列表,省去了很多麻烦。
    此处直接取一个较大的值,比如 100000.

    DO NOT need the cookie in headers.
    '''
    class ContentsParser(HTMLParser):
        def __init__(self):
            HTMLParser.__init__(self)
            self.contents = {}
            self.flag = 0
            self.link = ''
        def handle_starttag(self, tag, attrs):
            if tag == 'span' and dict(attrs).get('class') == 'link_title':
                self.flag = 1
            if self.flag > 0:
                self.flag += 1
                if tag == 'a':
                    self.link = dict(attrs).get('href')
        def handle_endtag(self, tag):
            self.flag -= 1
        def handle_data(self, data):
            if self.flag == 3:
                self.contents[self.link] = data.strip()

    conn = HTTPConnection(spider['blog']['host'])
    conn.request('GET', '/'+user+spider['blog']['path']+'10000', headers = spider['headers'])
    text = gzip.decompress(conn.getresponse().read()).decode('utf-8')
    hp = ContentsParser()
    hp.feed(text)
    hp.close()
    return hp.contents
Exemple #17
0
def upload_line():
    file = request.files['config_file'] if 'config_file' in request.files else None
    if file:
        filename = secure_filename(file.filename)
        upload_api = os.path.join(current_app.config['DAC_API_CONFIGS_LINES_URL'], request.form['line_no'])
        # print(upload_url)
        headers = {"Content-type": "application/x-www-form-urlencoded",
                   "Accept": "text/plain"}
        data = parse.urlencode({'file': file.read().decode('utf-8')})

        try:
            conn = HTTPConnection(current_app.config['DAC_HOST_URL'])
            conn.request("POST", upload_api, data, headers)
            resp = conn.getresponse()
            resp_data = json.loads(resp.read().decode('utf-8'))
            print(resp_data)
        except HTTPException as e:
            print("POST CONFIG FILE ERROR", e)
            resp_data = {}
        finally:
            conn.close()
        flash(resp_data)
    else:
        message = "Please select a file for upload."
        flash(message)
    return redirect(url_for('config.index'))
Exemple #18
0
def check_url_path(path, redirected=0):
    if redirected > MAX_REDIRECTION_ALLOWED:
        return False
    try:
        parse_result = urllib.parse.urlparse(path)
        server_name = parse_result.netloc
        urlpath = parse_result.path
        if not urlpath:
            # Just a server, as with a repo.
            with contextlib.closing(urllib.request.urlopen(path)) as res:
                code = res.getcode()
        else:
            # socket.gaierror could be raised,
            #   which is a child class of IOError
            conn = HTTPConnection(server_name, timeout=15)
            # Don't try to get the whole file:
            conn.request('HEAD', path)
            response = conn.getresponse()
            code = response.status
            conn.close()
        if code == 200:
            return True
        elif code == 301 or code == 302:
            for header in response.getheaders():
                if header[0] == 'location':
                    return check_url_path(header[1], redirected + 1)
        else:
            return False
    except (urllib.error.URLError, HTTPException, IOError, ValueError):
        return False
    return True
Exemple #19
0
class Client:
    #構構子
    def __init__(self):
        self.conn = None

    #對 mops service 送出 POST
    def requestServer(self, ajaxService, form_body):
        self.conn = HTTPConnection("61.57.47.131", 80)
        headers = {"Accept":"*/*",
                   "Accept-Encoding":"gzip, deflate",
                   "Accept-Language":"zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4",
                   "Connection":"keep-alive",
                   "Content-Type":"application/x-www-form-urlencoded",
                   "Host":"mops.twse.com.tw",
                   "Origin":"http://mops.twse.com.tw",
                   "Referer":"http://mops.twse.com.tw/mops/web/" + ajaxService,
                   "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"}
        body = form_body
        self.conn.request("POST", "/mops/web/" + ajaxService, body, headers)
        res = self.conn.getresponse()
        res_raw = res.read()
        res_data = res_raw.decode("utf-8")
        return res_data

    #關閉連線
    def closeConnection(self):
        self.conn.close()
    def _send_notify_event(self, uuid, notify_url, notify_message):
        """
        Send out notify event to subscriber and return a response.
        """
        # Remove <> that surround the real unicode url if they exist...
        notify_url = notify_url.translate({ord(k): u"" for k in "<>"})
        parsed_url = urlparse(notify_url)

        headers = {
            'HOST': parsed_url.netloc,
            'Content-Type': 'text/xml',
            'SID': 'uuid:' + uuid,
            'Content-Length': len(notify_message),
            'NT': 'upnp:event',
            'NTS': 'upnp:propchange'
        }

        http_handler = HTTPConnection(parsed_url.netloc)

        http_handler.request('NOTIFY', parsed_url.path, notify_message, headers)
        http_response = http_handler.getresponse()

        current_app.logger.info('_send_notify_event: status:{0} reason:{1} headers:{2}'.format(http_response.status,http_response.reason,headers))

        if http_response.status != 200:
            error_msg = 'UPNPPush Notification failed: ({0}: {1})'.format(http_response.status, http_response.read())

            current_app.logger.warning(error_msg)
            raise Exception(error_msg)
Exemple #21
0
def run(info):
    if (not isinstance(info, Info)):
        print(__name__ + "::run() info is not instance Info, please check")
        sys.exit(1)

    url = info.GetServiceUrl(Constant.ClientPort)
    print("Use endpoint to detect service: " + url)

    try:
        # Found error:timed out while health check, continue loop... need to create every time.
        conn = HTTPConnection(url, timeout=Constant.ConnectTimeout)
        # check health info, etcd case sensitive
        conn.request("GET", "/health")
        resp = conn.getresponse()
        con = resp.read().decode("UTF-8").strip("")

        # json need to docode too
        try:
            health = json.loads(con)
        except Exception as err:
            health = ""

        # str(resp.headers)
        print("Get health response:" + str(resp.status) + " " + str(resp.reason) + " " + str(health))

        if (con != "" and isinstance(health, object) and health["health"] == "true"):
            print("Node " + info.GetNodename() + " health status check passing")
        else:
            print("Cluster health checks failed, /health info check failed.")
            sys.exit(1)

    except Exception as err:
        print("Found error:" + str(err) + " while health check, continue loop...")
        print(traceback.format_exc())
        sys.exit(1)
Exemple #22
0
class Request(object):
    def __init__(self, method, resource, content_type='text/plain', **kwargs):
        auth = b64encode(CREDENTIALS.encode()).decode()
        self._headers = {
            'Authorization': 'Basic {0}'.format(auth),
            'Content-Type': content_type,
            'Accept': '*/*'
            }
        self._method = method
        self._href = resource
        self._kwargs = kwargs
        self._conn = None

    def __enter__(self):
        # First try encrypted HTTPS protocol
        self._conn = HTTPSConnection(SERVER)
        #self._conn.set_debuglevel(1)
        try:
            self._conn.request(self._method, self._href, headers=self._headers, **self._kwargs)
        except ssl.SSLError as err:
            # Catching possible ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol
            # in case the port is listening for unencrypted HTTP protocol
            self._conn = HTTPConnection(SERVER)
            self._conn.request(self._method, self._href, headers=self._headers, **self._kwargs)
        response = self._conn.getresponse()
        return response

    def __exit__(self, *args, **kwargs):
        if self._conn:
            self._conn.close()
def get_topic(use_offline):
    class TopicParser(HTMLParser):
        def __init__(self):
            HTMLParser.__init__(self)
            self.pattern = re.compile('http://www.zhihu.com/topic/\d{8}/questions')
            self.links = {}
            self.href = None
        def handle_starttag(self, tag, attrs):
            attrs = dict(attrs)
            if (tag == 'a' and attrs.get('href') and
                    re.match(self.pattern, attrs.get('href'))):
                self.href = attrs.get('href')
        def handle_data(self, data):
            if self.href != None:
                self.links[data] = self.href
                self.href = None
    text = None
    if use_offline:
        with open(spider['topic']['offline'], 'r', encoding = 'utf-8') as fp:
            text = fp.read()
    else:
        conn = HTTPConnection(spider['host'])
        conn.request('GET', spider['topic']['online'])
        text = conn.getresponse().read().decode('utf-8')
    hp = TopicParser()
    hp.feed(text)
    hp.close()
    return hp.links
	def HTTPProxy(self, domain, method, path, headers, post_data = None):
		conn = HTTPConnection(domain)
		conn.request(method, path, post_data, headers)
		response = conn.getresponse()
		response_headers = response.getheaders()
		data = response.read()
		if 'Content-Length' not in dict(response_headers).keys() and 'content-length' not in dict(response_headers).keys():
			response_headers.append(('Content-Length', len(data)))

		for item in response_headers:
			if item[0].lower() == 'transfer-encoding' and item[1].lower() ==  'chunked':
				response_headers[response_headers.index(item)] = ('Transfer-Encoding', 'text/html; charset=utf-8')
		
		self.push(HTTP_RESPONSE(response.version, response.status, response.reason))
		for item in response_headers:
			self.push(HTTP_HEADER(item))
		self.push(HTTP_RN)
		self.push(data)
		self.push(HTTP_RN)
		print("%s %s %s %d %s %d" % (self.version, method, path, response.status, response.reason, len(data)))

		for item in response_headers:
			if item[0].lower() == 'connection' and item[1].lower() == 'keep-alive':
				self.push(HTTP_0RN + HTTP_RN)
				return 
		self.close_when_done()
class YQLQuery(object):

    connection = None

    def __enter__(self):
        self.connection = None

    def __init__(self):

        while True:
            self.connection = HTTPConnection("query.yahooapis.com")

            if self.connection != None:
                break
            else:
                time.sleep(1)

    def execute(self, yql, token=None):
        while True:
            self.connection.request(
                "GET", PUBLIC_API_URL + "?" + urlencode({"q": yql, "format": "json", "env": DATATABLES_URL})
            )
            try:
                return simplejson.loads(self.connection.getresponse().read())
            except Exception, e:
                error_log = open("/usr/lib/cgi-bin/kchang_cdlcapital/logs/yql_log.txt", "a")
                error_log.write(str(e))
                print str(e)
                error_log.close()
                time.sleep(1)
    def resume(self):
        if self.next_token:
            verb = "ListRecords"
            print(self.next_token)
            command = "&resumptionToken=" + self.next_token

            url = urlparse(self.OAIbaseURL + "?verb=" + verb + command)
            response = False

            try:
                conn = HTTPConnection(url.netloc)
                conn.request("GET", url.path + "?" + url.query)
                response = conn.getresponse()
            except:
                print(self.OAIbaseURL + "?verb=" + verb + command)
                print("No 200 OK !!, sleeping 1 min.")
                time.sleep(50)
            if response:
                if response.status == 200:
                    data = response.read().decode("utf-8", "ignore")
                    self.data = fromstring(data)
                    token = self.data.findall(".//{http://www.openarchives.org/OAI/2.0/}resumptionToken")[0].text

                    if token and not self.token == token:
                        self.token = self.next_token
                        self.next_token = token
                    elif token:
                        print("Token = Next_token")
                        os._exit(-1)
                else:
                    print("No 200 OK !!, sleeping 1 min.")
                    time.sleep(50)
Exemple #27
0
    def test_closes_connection_without_content_length(self):
        """
        A HTTP 1.1 server is supposed to support keep-alive. Since our
        development server is rather simple we support it only in cases where
        we can detect a content length from the response. This should be doable
        for all simple views and streaming responses where an iterable with
        length of one is passed. The latter follows as result of `set_content_length`
        from https://github.com/python/cpython/blob/master/Lib/wsgiref/handlers.py.

        If we cannot detect a content length we explicitly set the `Connection`
        header to `close` to notify the client that we do not actually support
        it.
        """
        conn = HTTPConnection(LiveServerViews.server_thread.host, LiveServerViews.server_thread.port, timeout=1)
        try:
            conn.request('GET', '/streaming_example_view/', headers={'Connection': 'keep-alive'})
            response = conn.getresponse()
            self.assertTrue(response.will_close)
            self.assertEqual(response.read(), b'Iamastream')
            self.assertEqual(response.status, 200)
            self.assertEqual(response.getheader('Connection'), 'close')

            conn.request('GET', '/streaming_example_view/', headers={'Connection': 'close'})
            response = conn.getresponse()
            self.assertTrue(response.will_close)
            self.assertEqual(response.read(), b'Iamastream')
            self.assertEqual(response.status, 200)
            self.assertEqual(response.getheader('Connection'), 'close')
        finally:
            conn.close()
Exemple #28
0
def connect_to_tester():
    """
    Connect to the tester

    Returns:
        HTTPConnection: A connection to the tester
    """

    while True:
        try:
            # Get the host
            host = input("Please provide the IP of the host (input nothing for localhost): ").strip()
            if not host:
                host = "localhost"

            # Connect
            print(info_message("Connecting to tester..."))
            connection = HTTPConnection(host, PORT)

            # Inform the tester about the connection
            connection.request('POST', ROUTE_CONNECT_INFO, socket.gethostbyname(socket.gethostname()))
            print(info_message(connection.getresponse().read().decode()))
            break
        except socket.gaierror:
            print(info_message("Invalid host '{}'".format(host)))

    return connection
Exemple #29
0
    def request(self, method, endpoint, qargs={}, data=None):
        path = self.format_path(endpoint, qargs)
        conn = HTTPConnection("%s.myinsales.ru:80" % self.account, timeout=self.response_timeout)
        auth = b64encode("{0}:{1}".format(self.api_key, self.password).encode("utf-8")).decode("utf-8")
        headers = {"Authorization": "Basic {0}".format(auth), "Content-Type": "application/xml"}

        done = False
        while not done:
            try:
                conn.request(method, path, headers=headers, body=data)
            except (socket.gaierror, socket.timeout):
                if self.retry_on_socket_error:
                    time.sleep(self.retry_timeout)
                    continue
                else:
                    raise

            resp = conn.getresponse()
            body = resp.read()

            if resp.status == 503 and self.retry_on_503:
                time.sleep(self.retry_timeout)
            else:
                done = True

        if 200 <= resp.status < 300:
            return body
        else:
            raise ApiError("%s request to %s returned: %s\n%s" % (method, path, resp.status, body), resp.status)
Exemple #30
0
def httpStatus(host, path, status):
    try:
        conn = HTTPConnection(host)
        conn.request("HEAD", path)
        return conn.getresponse().status == status
    except:
        return False
Exemple #31
0
from http.client import HTTPConnection
con = HTTPConnection('www.google.com')
con.request('GET', '/')
result = con.getresponse()
contents = result.read()
print(contents)
Exemple #32
0
    def http(self, environ, start_response):
        try:
            host, port = get_destination(environ)
            log.info("HTTP request to (%s:%d)" % (host, port))
            if host == '127.0.0.1':
                status = '200 OK'
                response_headers = [('Content-Type', 'text/plain')]
                start_response(status, response_headers)
                yield b'It\'s Work!'
                return
            method, url, body, headers = copy_request(environ)
            if 'wxclient/app/attendance/addForEc' in url:
                if method == 'POST':
                    headers[
                        'User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0_3 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A432 MicroMessenger/6.5.21 NetType/WIFI Language/zh_CN'
                    body = u'{"signTime":"2017-11-08 14:15:28","success":false,"msg":"温馨提示:您的地理位置距离签到地点超过最小签到范围限制的500米","btnType":"2"}'
                    status = '200 OK'
                    response_headers = [
                        ('Content-Type', 'application/json;charset=UTF-8'),
                        ('Content-Length', str(len(str.encode(body))))
                    ]
                    start_response(status, response_headers)
                    yield str.encode(body)
                    return
        except Exception as e:
            log.error("[Exception][http 400]: %s" % str(e))
            start_response("400 Bad Request",
                           [("Content-Type", "text/plain; charset=utf-8")])
            yield "Bad Request"
            return

        try:
            # dismiss x-forwarders
            # set_forwarded_for(environ, headers)
            http_conn = socket.create_connection((host, port),
                                                 timeout=self.timeout)
            conn = HTTPConnection(host, port=port)
            conn.sock = http_conn
            if '/app/attendance/js/sign_ec.js' in url:
                # url = '/app/attendance/js/sign_ec.js?'+str(random.randint(0, 9999))
                # headers['If-None-Match'] = 'W/"10040-1502791023965"'
                print('**********', url, '**********')
                with open(sys.path[0] + "/sign_ec.js", 'rb') as sign_ec_js:
                    data = sign_ec_js.read()
                status = '200 OK'
                response_headers = [
                    ('X-Frame-Options', 'SAMEORIGIN'),
                    ('Accept-Ranges', 'bytes'),
                    ('Cache-Control', 'public, max-age=31536000'),
                    ('Content-Type', 'application/javascript'),
                    ('Date', 'Thu,  09 Nov 2017 05:54:54 GMT'),
                    ('ETag', 'W/"10040-1502791023964"'),
                    ('Last-Modified', 'Tue,  15 Aug 2017 09:57:03 GMT'),
                    ('Server', 'Apache-Coyote/1.1'),
                    ('Content-Length', str(len(data)))
                ]
                start_response(status, response_headers)
                yield data
                conn.close()
                return

            u = urllib.parse.urlsplit(url)
            path = urllib.parse.urlunsplit(("", "", u.path, u.query, ""))
            # Host header put by conn.request
            conn.request(method, path, body, headers)
            resp = conn.getresponse()
            headers = resp.getheaders()
            for header in headers:
                if 'Content-Length' in header:
                    headers.remove(header)

            chunked_encoding = ('Transfer-Encoding', 'chunked')
            headers.append(chunked_encoding)
            if '/app/attendance/js/sign_ec.js' in url:
                # url = '/app/attendance/js/sign_ec.js?'+str(random.randint(0, 9999))
                # headers['If-None-Match'] = 'W/"10040-1502791023965"'
                log.info('Respone for %s' % url)
                for header in headers:
                    if 'Cache-Control' in header:
                        headers.remove(header)

                CacheControl = ('Cache-Control', 'public,max-age=31536000')
                headers.append(CacheControl)
                print(url)
            start_response("%d %s" % (resp.status, resp.reason), headers)
            while True:
                data = resp.read(CHUNKSIZE)
                if not data:
                    break
                if '/app/attendance/js/sign_ec.js' in url:
                    data = bytes.decode(data)
                    with open(sys.path[0] + "/config.json", 'r') as load_f:
                        load_dict = json.load(load_f)
                    # lng = float('%s%d' % (load_dict['lng'], random.randint(0, 999)))
                    # lat = float('%s%d' % (load_dict['lat'], random.randint(0, 999)))
                    # print(lng, ',', lat)
                    data = re.sub(
                        r'setLngLat\(lng, lat\) {',
                        'setLngLat(lng, lat) {\n\tlng=%s+Math.random()*1000/1000000;\n\tlat=%s+Math.random()*1000/1000000;\n'
                        % (load_dict['lng'], load_dict['lat']), data)
                    data = re.sub(r'定位点', '牛X的定位点', data)
                    ''' data = re.sub(r'longitude = position.coords.longitude;',
                                  'longitude = 116.404731;', data)
                    data = re.sub(r'latitude = position.coords.latitude;',
                                  'latitude = 39.905679";', data) '''
                    log.info(url + 'REPLACE++++++++++')
                    data = str.encode(data)
                if 'wxclient/app/attendance/addForEc' in url:
                    if method == 'POST':
                        data = bytes.decode(data)
                        log.info(data)
                        data = str.encode(data)
                yield data

            conn.close()
        except Exception as e:
            log.error("[Exception][http 500]: %s" % str(e))
            start_response("500 Internal Server Error",
                           [("Content-Type", "text/plain; charset=utf-8")])
            yield "Internal Server Error"
            return
Exemple #33
0
def instantiate_ns(nsId, nsd_json, vnfds_json, request, nestedInfo=None):
    """
    Function description
    Parameters
    ----------
    param1: type
        param1 description
    Returns
    -------
    name: type
        return description
    """
    # extract the relevant information for the PA algorithm from the nsd_vnfd
    extracted_info = extract_nsd_info_for_pa(nsd_json, vnfds_json, request)
    log_queue.put(["INFO", dumps(extracted_info, indent=4)])
    # first get mtp resources and lock db
    resources = sbi.get_mtp_resources()
    log_queue.put(["INFO", "MTP resources are:"])
    log_queue.put(["INFO", dumps(resources, indent=4)])

    # ask pa to calculate the placement - read pa config from properties file
    config = RawConfigParser()
    config.read("../../sm/rooe/rooe.properties")
    pa_ip = config.get("PA", "pa.ip")
    pa_port = config.get("PA", "pa.port")
    pa_path = config.get("PA", "pa.path")
    pa_uri = "http://" + pa_ip + ":" + pa_port + pa_path
    # ask pa to calculate the placement - prepare the body
    paId = str(uuid4())
    pa_resources = parse_resources_for_pa(resources, vnfds_json.keys())
    body_pa = {
        "ReqId": paId,
        "nfvi": pa_resources,
        "nsd": extracted_info["nsd"],
        "callback": "http://localhost:8080/5gt/so/v1/__callbacks/pa/" + paId
    }
    log_queue.put(["INFO", "Body for PA is:"])
    log_queue.put(["INFO", dumps(body_pa, indent=4)])
    # ask pa to calculate the placement - do request
    header = {'Content-Type': 'application/json', 'Accept': 'application/json'}
    placement_info = {}
    try:
        conn = HTTPConnection(pa_ip, pa_port)
        conn.request("POST", pa_uri, dumps(body_pa), header)
        # ask pa to calculate the placement - read response and close connection
        rsp = conn.getresponse()
        placement_info = rsp.read().decode('utf-8')
        placement_info = loads(placement_info)
        conn.close()
    except ConnectionRefusedError:
        # the PA server is not running or the connection configuration is wrong
        log_queue.put([
            "ERROR",
            "the PA server is not running or the connection configuration is wrong"
        ])
    placement_info = amending_pa_output(extracted_info["nsd"], placement_info)
    log_queue.put(["INFO", "PA output is:"])
    log_queue.put(["DEBUG", placement_info])
    if nestedInfo:
        key = next(iter(nestedInfo))
        log_queue.put(["DEBUG", "the key of nestedInfo in ROOE is: %s" % key])
        if len(nestedInfo[key]) > 1:
            # nested from a consumer domain
            nsId_tmp = nsId
        else:
            # nested local
            nsId_tmp = nsId + '_' + next(iter(nestedInfo))
    else:
        nsId_tmp = nsId

    nsir_db.save_placement_info(nsId_tmp, placement_info)
    # ask cloudify/OSM to deploy vnfs
    coreMano = createWrapper()
    deployed_vnfs_info = {}
    deployed_vnfs_info = coreMano.instantiate_ns(nsId, nsd_json, vnfds_json,
                                                 request, placement_info,
                                                 resources, nestedInfo)
    log_queue.put(["INFO", "The deployed_vnfs_info"])
    log_queue.put(["INFO", dumps(deployed_vnfs_info, indent=4)])
    if deployed_vnfs_info is not None and "sapInfo" in deployed_vnfs_info:
        log_queue.put([
            "INFO",
            "ROOE: updating nsi:%s sapInfo: %s" %
            (nsId, deployed_vnfs_info["sapInfo"])
        ])
        ns_db.save_sap_info(nsId, deployed_vnfs_info["sapInfo"])

    # list of VLs to be deployed
    vls_info = extract_vls_info_mtp(resources, extracted_info, placement_info,
                                    nsId_tmp)
    # ask network execution engine to deploy the virtual links
    eenet.deploy_vls(vls_info, nsId_tmp)
    # eenet.deploy_vls(vls_info, nsId)

    # set operation status as SUCCESSFULLY_DONE
    if (nsId_tmp.find('_') == -1):
        # the service is single, I can update the operationId, and the status
        operationId = operation_db.get_operationId(nsId, "INSTANTIATION")
        if deployed_vnfs_info is not None:
            log_queue.put(["INFO", "NS Instantiation finished correctly"])
            operation_db.set_operation_status(operationId, "SUCCESSFULLY_DONE")
            # set ns status as INSTANTIATED
            ns_db.set_ns_status(nsId, "INSTANTIATED")
        else:
            log_queue.put(["ERROR", "NS Instantiation FAILED"])
            operation_db.set_operation_status(operationId, "FAILED")
            # set ns status as FAILED
            ns_db.set_ns_status(nsId, "FAILED")

    log_queue.put(["INFO", "INSTANTIATION FINISHED :)"])
Exemple #34
0
def main(sys_argv=None):  # pylint: disable=too-many-branches
    if sys_argv is None:
        sys_argv = sys.argv[1:]

    short_options = "u:p:P:t:f:hv"
    long_options = [
        "username="******"password="******"port=",
        "timeout=",
        "filename=",
        "help",
        "verbose",
        "debug",
    ]

    opt_username = None
    opt_password = None
    opt_port = 8090
    opt_timeout = 30
    opt_verbose = False
    opt_debug = False
    opt_filename = None

    try:
        opts, args = getopt.getopt(sys_argv, short_options, long_options)
    except getopt.GetoptError as err:
        sys.stderr.write("%s\n" % err)
        return 1

    for o, a in opts:
        if o in ["-h", "--help"]:
            usage()
            sys.exit(0)
        elif o in ["-u", "--username"]:
            opt_username = a
        elif o in ["-p", "--password"]:
            opt_password = a
        elif o in ["-f", "--filename"]:
            opt_filename = a
        elif o in ["-P", "--port"]:
            try:
                opt_port = int(a)
                if opt_port < 1 or opt_port > 65534:
                    raise ValueError
            except ValueError:
                sys.stderr.write(
                    "Port is not a valid integer in range 1-65534\n")
                return 1
        elif o in ["-t", "--timeout"]:
            try:
                opt_timeout = int(a)
            except ValueError:
                sys.stderr.write("Timeout is not a valid integer\n")
                return 1
        elif o in ["-v", "--verbose"]:
            opt_verbose = True
        elif o in ["--debug"]:
            opt_debug = True

    if len(args) < 2:
        sys.stderr.write("Too few arguments\n")
        usage()
        return 1

    arg_host, arg_application = args[0:2]

    if opt_filename:
        try:
            data = json.loads(Path(opt_filename).read_text())
        except Exception as e:
            sys.stderr.write("Cannot read JSON data from file %s: %s\n" %
                             (opt_filename, e))
            if opt_debug:
                raise
            return 1

    else:
        url = (
            "/controller/rest/applications/%(application)s/metric-data"
            "?metric-path=Application%%20Infrastructure%%20Performance|*|Individual%%20Nodes|*|%(object)s|*|*"
            "&time-range-type=BEFORE_NOW&duration-in-mins=1&output=json")

        socket.setdefaulttimeout(opt_timeout)

        data = []

        # Initialize server connection
        try:
            connection = HTTPConnection(arg_host, opt_port)

            if opt_verbose:
                sys.stdout.write("Connecting to %s:%s...\n" %
                                 (arg_host, opt_port))
            connection.connect()

            auth = b64encode(("%s:%s" % (opt_username, opt_password)).encode())
            headers = {"Authorization": "Basic " + auth.decode()}
            for obj in ["Agent", "*|*"]:
                connection.request("GET",
                                   url % {
                                       "application": arg_application,
                                       "object": obj
                                   },
                                   headers=headers)
                response = connection.getresponse()

                if response.status != 200:
                    sys.stderr.write(
                        "Could not fetch data from AppDynamics server. "
                        "HTTP %s: %s\n" % (response.status, response.reason))
                    return 1

                data += json.loads(response.read())

        except Exception as e:
            sys.stderr.write("Cannot connect to AppDynamics server. %s\n" % e)
            if opt_debug:
                raise
            return 1

    grouped_data: Dict[str, Dict[str, Dict[str, Any]]] = {}
    for metric in data:
        path_parts = metric["metricPath"].split("|")
        if len(path_parts) == 7:  # Unit missing
            path_parts.append("")

        _base, application, _section, node, provider, typename, item, unit = path_parts

        try:
            value = metric["metricValues"][0]["current"]
        except IndexError:
            continue  # Skip empty values

        if provider not in ("Agent", "JMX", "JVM"):
            continue  # Skip unwanted values

        grouped_data.setdefault(node,
                                {}).setdefault(application, {}).setdefault(
                                    typename, {}).setdefault(item,
                                                             {})[unit] = value

    for node, applications in grouped_data.items():
        sys.stdout.write("<<<<%s>>>>\n" % node)
        for application, types in applications.items():
            for typename, items in types.items():
                typename = typename.lower().replace(" ", "_")
                if typename in [
                        "app", "memory", "sessions", "web_container_runtime"
                ]:
                    sys.stdout.write("<<<appdynamics_%s:sep(124)>>>\n" %
                                     (typename.replace("_runtime", "")))
                    for item, values in items.items():
                        if values:
                            output_items = [application, item]
                            for name, value in values.items():
                                if not name:
                                    output_items.append("%s" % value)
                                else:
                                    output_items.append("%s:%s" %
                                                        (name, value))
                            sys.stdout.write("|".join(output_items) + "\n")
    sys.stdout.write("<<<<>>>>\n")
Exemple #35
0
from http.client import HTTPConnection

try:
    connection = HTTPConnection("google.com")
    connection.request("GET", "/")
    result = connection.getresponse()
    print(result)

    UrlContent = result.read()
    print(UrlContent)
except:
    print("Invalid URL")
Exemple #36
0
from http.client import HTTPConnection
from urllib.parse import urlencode

host = '127.0.0.1:8000'
params = urlencode({
    'language': 'python',
    'name': '김석훈',
    'email': '*****@*****.**',
})
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'text/plain',
}

conn = HTTPConnection(host)
conn.request('PUT', '', params, headers)
resp = conn.getresponse()
print(resp.status, resp.reason)

data = resp.read()
print(data.decode('utf-8'))

conn.close()
Exemple #37
0
from http.client import HTTPConnection

conn = HTTPConnection('www.example.com')
conn.request('HEAD', '/')

resp = conn.getresponse()
print(resp.status, resp.reason)

data = resp.read()
print(len(data))
print(data == b'')
def smoke_test_release(release, files, expected_hash, plugins):
    for release_file in files:
        if not os.path.isfile(release_file):
            raise RuntimeError('Smoketest failed missing file %s' %
                               (release_file))
        tmp_dir = tempfile.mkdtemp()
        if release_file.endswith('tar.gz'):
            run('tar -xzf %s -C %s' % (release_file, tmp_dir))
        elif release_file.endswith('zip'):
            run('unzip %s -d %s' % (release_file, tmp_dir))
        else:
            log('Skip SmokeTest for [%s]' % release_file)
            continue  # nothing to do here
        es_run_path = os.path.join(tmp_dir, 'elasticsearch-%s' % (release),
                                   'bin/elasticsearch')
        print('  Smoke testing package [%s]' % release_file)
        es_plugin_path = os.path.join(tmp_dir, 'elasticsearch-%s' % (release),
                                      'bin/plugin')
        plugin_names = {}
        for name, plugin in plugins:
            print('  Install plugin [%s] from [%s]' % (name, plugin))
            run('%s %s %s' % (es_plugin_path, '-install', plugin))
            plugin_names[name] = True

        if release.startswith("0.90."):
            background = ''  # 0.90.x starts in background automatically
        else:
            background = '-d'
        print('  Starting elasticsearch deamon from [%s]' %
              os.path.join(tmp_dir, 'elasticsearch-%s' % release))
        run('%s; %s -Des.node.name=smoke_tester -Des.cluster.name=prepare_release -Des.discovery.zen.ping.multicast.enabled=false %s'
            % (java_exe(), es_run_path, background))
        conn = HTTPConnection('127.0.0.1', 9200, 20)
        wait_for_node_startup()
        try:
            try:
                conn.request('GET', '')
                res = conn.getresponse()
                if res.status == 200:
                    version = json.loads(res.read().decode("utf-8"))['version']
                    if release != version['number']:
                        raise RuntimeError(
                            'Expected version [%s] but was [%s]' %
                            (release, version['number']))
                    if version['build_snapshot']:
                        raise RuntimeError('Expected non snapshot version')
                    if version['build_hash'].strip() != expected_hash:
                        raise RuntimeError(
                            'HEAD hash does not match expected [%s] but got [%s]'
                            % (expected_hash, version['build_hash']))
                    print('  Running REST Spec tests against package [%s]' %
                          release_file)
                    run_mvn('test -Dtests.rest=%s -Dtests.class=*.*RestTests' %
                            ("127.0.0.1:9200"))
                    print('  Verify if plugins are listed in _nodes')
                    conn.request('GET', '/_nodes?plugin=true&pretty=true')
                    res = conn.getresponse()
                    if res.status == 200:
                        nodes = json.loads(res.read().decode("utf-8"))['nodes']
                        for _, node in nodes.items():
                            node_plugins = node['plugins']
                            for node_plugin in node_plugins:
                                if not plugin_names.get(
                                        node_plugin['name'], False):
                                    raise RuntimeError('Unexpeced plugin %s' %
                                                       node_plugin['name'])
                                del plugin_names[node_plugin['name']]
                        if plugin_names:
                            raise RuntimeError('Plugins not loaded %s' %
                                               list(plugin_names.keys()))

                    else:
                        raise RuntimeError('Expected HTTP 200 but got %s' %
                                           res.status)
                else:
                    raise RuntimeError('Expected HTTP 200 but got %s' %
                                       res.status)
            finally:
                conn.request('POST', '/_cluster/nodes/_local/_shutdown')
                time.sleep(1)  # give the node some time to shut down
                if conn.getresponse().status != 200:
                    raise RuntimeError(
                        'Expected HTTP 200 but got %s on node shutdown' %
                        res.status)

        finally:
            conn.close()
        shutil.rmtree(tmp_dir)
from http.client import HTTPConnection
import json

conn = HTTPConnection("127.0.0.1:5000")

# http.client places Accept-Encoding: identity if nothing is specified
headers = {"Content-Type": "application/json"}

# http.client encodes the body as extended ascii (ISO-8859-1) if no Content-Type is specified
body = {"one": 1, "two": 2}

conn.request("POST", "", json.dumps(body), headers)
resp = conn.getresponse()
resp_bytes = resp.read()

print(resp_byte)
Exemple #40
0
def update_state_server(http_server_host_port, topology_name, key, value):
    connection = HTTPConnection(http_server_host_port)
    connection.request('POST', '/state/%s_%s' % (topology_name, key),
                       '"%s"' % value)
    response = connection.getresponse()
    return response.status == 200
Exemple #41
0
class HTTPRelayClient(ProtocolClient):
    PLUGIN_NAME = "HTTP"

    def __init__(self, serverConfig, target, targetPort = 80, extendedSecurity=True ):
        ProtocolClient.__init__(self, serverConfig, target, targetPort, extendedSecurity)
        self.extendedSecurity = extendedSecurity
        self.negotiateMessage = None
        self.authenticateMessageBlob = None
        self.server = None

    def initConnection(self):
        self.session = HTTPConnection(self.targetHost,self.targetPort)
        self.lastresult = None
        if self.target.path == '':
            self.path = '/'
        else:
            self.path = self.target.path
        return True

    def sendNegotiate(self,negotiateMessage):
        #Check if server wants auth
        self.session.request('GET', self.path)
        res = self.session.getresponse()
        res.read()
        if res.status != 401:
            LOG.info('Status code returned: %d. Authentication does not seem required for URL' % res.status)
        try:
            if 'NTLM' not in res.getheader('WWW-Authenticate'):
                LOG.error('NTLM Auth not offered by URL, offered protocols: %s' % res.getheader('WWW-Authenticate'))
                return False
        except (KeyError, TypeError):
            LOG.error('No authentication requested by the server for url %s' % self.targetHost)
            return False

        #Negotiate auth
        negotiate = base64.b64encode(negotiateMessage)
        headers = {'Authorization':'NTLM %s' % negotiate}
        self.session.request('GET', self.path ,headers=headers)
        res = self.session.getresponse()
        res.read()
        try:
            serverChallengeBase64 = re.search('NTLM ([a-zA-Z0-9+/]+={0,2})', res.getheader('WWW-Authenticate')).group(1)
            serverChallenge = base64.b64decode(serverChallengeBase64)
            challenge = NTLMAuthChallenge()
            challenge.fromString(serverChallenge)
            return challenge
        except (IndexError, KeyError, AttributeError):
            LOG.error('No NTLM challenge returned from server')

    def sendAuth(self, authenticateMessageBlob, serverChallenge=None):
        if unpack('B', authenticateMessageBlob[:1])[0] == SPNEGO_NegTokenResp.SPNEGO_NEG_TOKEN_RESP:
            respToken2 = SPNEGO_NegTokenResp(authenticateMessageBlob)
            token = respToken2['ResponseToken']
        else:
            token = authenticateMessageBlob
        auth = base64.b64encode(token)
        headers = {'Authorization':'NTLM %s' % auth}
        self.session.request('GET', self.path,headers=headers)
        res = self.session.getresponse()
        if res.status == 401:
            return None, STATUS_ACCESS_DENIED
        else:
            LOG.info('HTTP server returned error code %d, treating as a successful login' % res.status)
            #Cache this
            self.lastresult = res.read()
            return None, STATUS_SUCCESS

    def killConnection(self):
        if self.session is not None:
            self.session.close()
            self.session = None

    def keepAlive(self):
        # Do a HEAD for favicon.ico
        self.session.request('HEAD','/favicon.ico')
        self.session.getresponse()
Exemple #42
0
# f = urllib.request.urlopen(url)
#
# print("getur():", f.geturl())
# print(f.read().decode('utf-8'))
#
# # < 2.2.3 urllib.request 모듈 예제 > 의 < 예제 2-7 > 는 별도 파일로 생성

# 2.2.4 http.client 모듈
# 예제 2-8 : http.client 모듈 사용 - GET 방식 요청
# >python

from http.client import HTTPConnection

host = 'www.example.com'
conn = HTTPConnection(host)
conn.request('GET', '/')  # url을 넣는 두 번째 인자에 왜 '/'만 넣는 걸까? (질문)
r1 = conn.getresponse()
print(r1.status, r1.reason)  # 200 OK가 출력됨

data1 = r1.read()

conn.request('GET', '/')  # 두 번째 요청, 왜 요청을 또하는 거지?
r2 = conn.getresponse()
print(r2.status, r2.reason)  # 역시나 200 OK

data2 = r2.read()
print(data2.decode())  # HTML 파일이 출력된다

conn.close()

# 예제 2-9 : http.client 모듈 사용 - HEAD 방식 요청
Exemple #43
0
    from pyquery.pyquery import PyQuery as pq
    from http.client import HTTPConnection
    pqa = pq
else:
    from cStringIO import StringIO
    import pyquery
    from httplib import HTTPConnection
    from webob import Request, Response, exc
    from pyquery import PyQuery as pq
    from ajax import PyQuery as pqa

socket.setdefaulttimeout(1)

try:
    conn = HTTPConnection("pyquery.org:80")
    conn.request("GET", "/")
    response = conn.getresponse()
except (socket.timeout, socket.error):
    GOT_NET = False
else:
    GOT_NET = True


def with_net(func):
    if GOT_NET:
        return func


def not_py3k(func):
    if not PY3k:
        return func
Exemple #44
0
def request_api(params, host='localhost'):
    h = HTTPConnection(host, PORT, 10)
    query = '&'.join('{}={}'.format(k, quote(v)) for (k, v) in params.items())
    url = '/git-pull/api?token=secret&{}'.format(query)
    h.request('GET', url)
    return h.getresponse()
Exemple #45
0
class PlaySession(object):
    """
    A Google Play Music session.

    It allows for authentication and the making of authenticated
    requests through the MusicManager API (protocol buffers), Web client requests,
    and the Skyjam client API.
    """

    # The URL for authenticating against Google Play Music
    PLAY_URL = 'https://play.google.com/music/listen?u=0&hl=en'

    # Common User Agent used for web requests
    _user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)"

    def __init__(self):
        """
        Initializes a default unauthenticated session.
        """
        self.client = None
        self.cookies = None
        self.logged_in = False

        # Wish there were better names for these
        self.android = HTTPSConnection('android.clients.google.com')
        self.jumper = HTTPConnection('uploadsj.clients.google.com')

    def _get_cookies(self):
        """
        Gets cookies needed for web and media streaming access.
        Returns True if the necessary cookies are found, False otherwise.
        """
        if self.logged_in:
            raise AlreadyLoggedIn

        handler = build_opener(HTTPCookieProcessor(self.cookies))
        req = Request(self.PLAY_URL, None, {})  #header)
        resp_obj = handler.open(req)

        return (self.get_cookie('sjsaid') is not None
                and self.get_cookie('xt') is not None)

    def get_cookie(self, name):
        """
        Finds the value of a cookie by name, returning None on failure.

        :param name: The name of the cookie to find.
        """
        for cookie in self.cookies:
            if cookie.name == name:
                return cookie.value

        return None

    def login(self, email, password):
        """
        Attempts to create an authenticated session using the email and
        password provided.
        Return True if the login was successful, False otherwise.
        Raises AlreadyLoggedIn if the session is already authenticated.

        :param email: The email address of the account to log in.
        :param password: The password of the account to log in.
        """
        if self.logged_in:
            raise AlreadyLoggedIn

        self.client = ClientLogin(email, password, 'sj')
        tokenauth = TokenAuth('sj', self.PLAY_URL, 'jumper')

        if self.client.get_auth_token() is None:
            return False

        tokenauth.authenticate(self.client)
        self.cookies = tokenauth.get_cookies()

        self.logged_in = self._get_cookies()

        return self.logged_in

    def logout(self):
        """
        Resets the session to an unauthenticated default state.
        """
        self.__init__()

    def open_web_url(self, url_builder, extra_args=None, data=None, ua=None):
        """
        Opens an https url using the current session and returns the response.
        Code adapted from:
        http://code.google.com/p/gdatacopier/source/browse/tags/gdatacopier-1.0.2/gdatacopier.py

        :param url_builder: the url, or a function to receieve a dictionary of querystring arg/val pairs and return the url.
        :param extra_args: (optional) key/val querystring pairs.
        :param data: (optional) encoded POST data.
        :param ua: (optional) The User Age to use for the request.
        """
        # I couldn't find a case where we don't need to be logged in
        if not self.logged_in:
            raise NotLoggedIn

        if isinstance(url_builder, basestring):
            url = url_builder
        else:
            url = url_builder({'xt': self.get_cookie("xt")})

        #Add in optional pairs to the querystring.
        if extra_args:
            #Assumes that a qs has already been started (ie we don't need to put a ? first)
            assert (url.find('?') >= 0)

            extra_url_args = ""
            for name, val in extra_args.iteritems():
                extra_url_args += "&{0}={1}".format(name, val)

            url += extra_url_args

        opener = build_opener(HTTPCookieProcessor(self.cookies))

        if not ua:
            ua = self._user_agent

        opener.addheaders = [('User-agent', ua)]

        if data:
            response = opener.open(url, data)
        else:
            response = opener.open(url)

        return response

    def post_protobuf(self, path, protobuf):
        """
        Returns the response from encoding and posting the given data.

        :param path: the name of the service url
        :param proto: data to be encoded with protobuff
        """
        if not self.logged_in:
            raise NotLoggedIn

        urlpath = '/upsj/' + path
        self.android.request(
            'POST', urlpath, protobuf.SerializeToString(), {
                'Cookie': 'SID=%s' % self.client.get_sid_token(),
                'Content-Type': 'application/x-google-protobuf'
            })

        resp = self.android.getresponse()

        return resp.read()

    def post_jumper(self, url, encoded_data, headers=None):
        """
        Returns the response of a post to the MusicManager jumper service.
        """
        if not self.logged_in:
            raise NotLoggedIn

        if not headers:
            headers = {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie': 'SID=%s' % self.client.get_sid_token()
            }

        self.jumper.request('POST', url, encoded_data, headers)
        return self.jumper.getresponse()
Exemple #46
0
    def test_get_invalid(self, pghoard, tmpdir):
        ne_wal_seg = "0000FFFF0000000C000000FE"
        nonexistent_wal = "/{}/archive/{}".format(pghoard.test_site,
                                                  ne_wal_seg)
        # x-pghoard-target-path missing
        conn = HTTPConnection(host="127.0.0.1",
                              port=pghoard.config["http_port"])
        conn.request("GET", nonexistent_wal)
        status = conn.getresponse().status
        assert status == 400
        # missing WAL file
        headers = {
            "x-pghoard-target-path": str(tmpdir.join("test_get_invalid"))
        }
        conn.request("GET", nonexistent_wal, headers=headers)
        status = conn.getresponse().status
        assert status == 404
        # no x-pghoard-target-path for head
        headers = {
            "x-pghoard-target-path": str(tmpdir.join("test_get_invalid"))
        }
        conn.request("HEAD", nonexistent_wal, headers=headers)
        status = conn.getresponse().status
        assert status == 400
        # missing WAL file
        headers = {
            "x-pghoard-target-path": str(tmpdir.join("test_get_invalid"))
        }
        conn.request("HEAD", nonexistent_wal)
        status = conn.getresponse().status
        assert status == 404
        # missing WAL file using restore_command
        with pytest.raises(postgres_command.PGCError) as excinfo:
            restore_command(site=pghoard.test_site,
                            xlog=os.path.basename(nonexistent_wal),
                            host="127.0.0.1",
                            port=pghoard.config["http_port"],
                            output=None,
                            retry_interval=0.1)
        assert excinfo.value.exit_code == postgres_command.EXIT_NOT_FOUND

        # write failures, this should be retried a couple of times
        # start by making sure we can access the file normally
        valid_wal_seg = "0000DDDD0000000D000000FC"
        valid_wal = "/{}/xlog/{}".format(pghoard.test_site, valid_wal_seg)
        store = pghoard.transfer_agents[0].get_object_storage(
            pghoard.test_site)
        store.store_file_from_memory(valid_wal,
                                     wal_header_for_file(valid_wal_seg),
                                     metadata={"a": "b"})
        conn.request("HEAD", valid_wal)
        status = conn.getresponse().status
        assert status == 200
        restore_command(site=pghoard.test_site,
                        xlog=os.path.basename(valid_wal),
                        host="127.0.0.1",
                        port=pghoard.config["http_port"],
                        output=None,
                        retry_interval=0.1)

        # write to non-existent directory
        headers = {
            "x-pghoard-target-path": str(tmpdir.join("NA", "test_get_invalid"))
        }
        conn.request("GET", valid_wal, headers=headers)
        status = conn.getresponse().status
        assert status == 409
Exemple #47
0
    def run_test(self):

        #################################################
        # lowlevel check for http persistent connection #
        #################################################
        url = urlparse(self.nodes[0].url)
        authpair = url.username + ':' + url.password
        headers = {"Authorization": "Basic " + str_to_b64str(authpair)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read()
        assert_equal(b'"error":null' in out1, True)
        assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!

        # send 2nd request without closing connection
        conn.request('POST', '/', '{"method": "getchaintips"}', headers)
        out2 = conn.getresponse().read()
        assert_equal(b'"error":null' in out2, True) # must also response with a correct json-rpc message
        assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!
        conn.close()

        # same should be if we add keep-alive because this should be the std. behaviour
        headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection": "keep-alive"}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read()
        assert_equal(b'"error":null' in out1, True)
        assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!

        # send 2nd request without closing connection
        conn.request('POST', '/', '{"method": "getchaintips"}', headers)
        out2 = conn.getresponse().read()
        assert_equal(b'"error":null' in out2, True) # must also response with a correct json-rpc message
        assert_equal(conn.sock!=None, True) # according to http/1.1 connection must still be open!
        conn.close()

        # now do the same with "Connection: close"
        headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection":"close"}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read()
        assert_equal(b'"error":null' in out1, True)
        assert_equal(conn.sock!=None, False) # now the connection must be closed after the response

        # node1 (2nd node) is running with disabled keep-alive option
        urlNode1 = urlparse(self.nodes[1].url)
        authpair = urlNode1.username + ':' + urlNode1.password
        headers = {"Authorization": "Basic " + str_to_b64str(authpair)}

        conn = HTTPConnection(urlNode1.hostname, urlNode1.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read()
        assert_equal(b'"error":null' in out1, True)

        # node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
        urlNode2 = urlparse(self.nodes[2].url)
        authpair = urlNode2.username + ':' + urlNode2.password
        headers = {"Authorization": "Basic " + str_to_b64str(authpair)}

        conn = HTTPConnection(urlNode2.hostname, urlNode2.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read()
        assert_equal(b'"error":null' in out1, True)
        assert_equal(conn.sock!=None, True) # connection must be closed because bitcoind should use keep-alive by default

        # Check excessive request size
        conn = HTTPConnection(urlNode2.hostname, urlNode2.port)
        conn.connect()
        conn.request('GET', '/' + ('x'*1000), '', headers)
        out1 = conn.getresponse()
        assert_equal(out1.status, NOT_FOUND)

        conn = HTTPConnection(urlNode2.hostname, urlNode2.port)
        conn.connect()
        conn.request('GET', '/' + ('x'*10000), '', headers)
        out1 = conn.getresponse()
        assert_equal(out1.status, BAD_REQUEST)
Exemple #48
0
def update_state_server(http_server_host_port, topology_name, key, value):
    connection = HTTPConnection(http_server_host_port)
    connection.request('POST', f'/state/{topology_name}_{key}', f'"{value}"')
    response = connection.getresponse()
    return response.status == 200
Exemple #49
0
from http.client import HTTPConnection

conn = HTTPConnection("127.0.0.1:8127", timeout=30)
conn.request("CONNECT",
             "www.126.com:443",
             body=None,
             headers={
                 "Host": "www.126.com:443",
                 "User-Agent": "vivo X6D_5.1_weibo_6.5.0_android",
                 "Connection": "Alive",
                 "Content-Length": "0"
             })
conn.set_debuglevel(1)
r1 = conn.getresponse()
r1.read()
from http.client import HTTPConnection
from urllib.parse import urlencode

url = '127.0.0.1:8000'

#POST요청으로 보낼 Parameters

params = urlencode({'language': 'python', 'name': 'Hoplin', 'email': 'a@b'})

print(params)

# Headers

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'text/plain'
}

conn = HTTPConnection(url)
conn.request('PUT', '', params,
             headers)  #request(method,URL,body,headers) 순서의 매개변수를 가지고 있다.
resp = conn.getresponse()
print(resp.status, resp.reason)

data = resp.read()
print(data.decode('utf-8'))

conn.close()
# -*- coding:utf-8 -*-
from http.client import HTTPConnection
from getNaverNews import resBody
from json import loads

# api.openweathermap.org/data/2.5/weather
# ?q=seoul
# &appid=baff8f3c6cbc28a4024e336599de28c4
# &units=metric
# &lang=kr
# key = baff8f3c6cbc28a4024e336599de28c4

# HTTP통신 -> 다운받아서 콘솔 출력
huc = HTTPConnection("api.openweathermap.org")
huc.request("GET", "/data/2.5/weather?q=seoul&appid=baff8f3c6cbc28a4024e336599de28c4&units=metric&lang=kr")
resBody = huc.getresponse().read()
# print(resBody.decode())
################################################
# JOSN파싱
# JSON.init
weatherData = loads(resBody) # Python객체로
print(type(weatherData))

l = weatherData["weather"][0]["description"]
# print(type(l))
print(weatherData["weather"][0]["description"])
print(weatherData["main"]["temp"])
print(weatherData["main"]["feels_like"])
print(weatherData["main"]["humidity"])
if option == '1':	
	constructorString = 'DryBlock:STD:Internal'
elif option == '2':
	tag = input("TAG?")
	control = input("Control by external probe (true or false)?")
	constructorString = 'DryBlock:{}:External|{}'.format(tag, control)
	
print("Constructor String: " + constructorString)

username = '******'
password = '******'

currentDirectory = os.path.dirname(__file__)
fullPath = os.path.join(currentDirectory, ipConfigFile)
file = open(fullPath) 
ipValue = file.read()

connection = HTTPConnection(ipValue, port=port)
authKey = b64encode((username+":"+password).encode('utf-8')).decode('utf-8')
headers = {"Authorization":"Basic " + authKey}
connection.request(method, url.format(constructorString), headers=headers)
response = connection.getresponse()

print("http://" + ipValue + ":" + str(port) + url.format(constructorString))

print(response.read().decode())




__email__ = "*****@*****.**"

from http.client import HTTPConnection
from base64 import b64encode
import os

ipConfigFile = 'ip_address_config.txt'
port = 5000
url = '/taserver/pages/getinput.cgi'
method = 'GET'

username = '******'
password = '******'

currentDirectory = os.path.dirname(__file__)
fullPath = os.path.join(currentDirectory, ipConfigFile)
file = open(fullPath)
ipValue = file.read()

connection = HTTPConnection(ipValue, port=port)
authKey = b64encode(
    (username + ":" + password).encode('utf-8')).decode('utf-8')
headers = {"Authorization": "Basic " + authKey}
connection.request(method, url, headers=headers)
response = connection.getresponse()

print("http://" + ipValue + ":" + str(port) + url)

value, unit = response.read().decode().split('|')
print(value + " " + unit)
def request(method,
            urlString,
            body='',
            headers={},
            auth=None,
            verbose=False,
            https_proxy=os.getenv('https_proxy', None),
            timeout=60):
    url = urlparse(urlString)
    if url.scheme == 'http':
        conn = HTTPConnection(url.netloc, timeout=timeout)
    else:
        if httpRequestProps['secure'] or not hasattr(
                ssl, '_create_unverified_context'):
            conn = HTTPSConnection(
                url.netloc if https_proxy is None else https_proxy,
                timeout=timeout)
        else:
            conn = HTTPSConnection(
                url.netloc if https_proxy is None else https_proxy,
                context=ssl._create_unverified_context(),
                timeout=timeout)
        if https_proxy:
            conn.set_tunnel(url.netloc)

    if auth is not None:
        auth = base64.b64encode(auth.encode()).decode()
        headers['Authorization'] = 'Basic %s' % auth

    if verbose:
        print('========')
        print('REQUEST:')
        print('%s %s' % (method, urlString))
        print('Headers sent:')
        print(getPrettyJson(headers))
        if body != '':
            print('Body sent:')
            print(body)

    try:
        conn.request(method, urlString, body, headers)
        res = conn.getresponse()
        body = ''
        try:
            body = res.read()
        except IncompleteRead as e:
            body = e.partial

        # patch the read to return just the body since the normal read
        # can only be done once
        res.read = lambda: body

        if verbose:
            print('--------')
            print('RESPONSE:')
            print('Got response with code %s' % res.status)
            print('Body received:')
            print(res.read())
            print('========')
        return res
    except socket.timeout:
        return ErrorResponse(status=500,
                             error='request timed out at %d seconds' % timeout)
    except Exception as e:
        return ErrorResponse(status=500, error=str(e))
Exemple #55
0
def datasource(lat, lon, source_dir):
    """ Return a gdal datasource for an SRTM1 lat, lon corner.
    
        If it doesn't already exist locally in source_dir, grab a new one.
    """
    #
    # Create a URL
    #
    try:
        reg = region(lat, lon)
    except ValueError:
        # we're probably outside a known region
        return None

    # FIXME for western / southern hemispheres
    fmt = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM1/Region_%02d/N%02dW%03d.hgt.zip'
    url = fmt % (reg, abs(lat), abs(lon))

    #
    # Create a local filepath
    #
    s, host, path, p, q, f = urlparse(url)

    dem_dir = md5(url).hexdigest()[:3]
    dem_dir = join(source_dir, dem_dir)

    dem_path = join(dem_dir, basename(path)[:-4])
    dem_none = dem_path[:-4] + '.404'

    #
    # Check if the file exists locally
    #
    if exists(dem_path):
        return gdal.Open(dem_path, gdal.GA_ReadOnly)

    if exists(dem_none):
        return None

    if not exists(dem_dir):
        makedirs(dem_dir)
        chmod(dem_dir, 0o777)

    assert isdir(dem_dir)

    #
    # Grab a fresh remote copy
    #
    print('Retrieving', url, 'in DEM.SRTM1.datasource().', file=stderr)

    conn = HTTPConnection(host, 80)
    conn.request('GET', path)
    resp = conn.getresponse()

    if resp.status == 404:
        # we're probably outside the coverage area
        print(url, file=open(dem_none, 'w'))
        return None

    assert resp.status == 200, (resp.status, resp.read())

    try:
        #
        # Get the DEM out of the zip file
        #
        handle, zip_path = mkstemp(prefix='srtm1-', suffix='.zip')
        write(handle, resp.read())
        close(handle)

        zipfile = ZipFile(zip_path, 'r')

        #
        # Write the actual DEM
        #
        dem_file = open(dem_path, 'w')
        dem_file.write(zipfile.read(zipfile.namelist()[0]))
        dem_file.close()

        chmod(dem_path, 0o666)

    finally:
        unlink(zip_path)

    #
    # The file better exist locally now
    #
    return gdal.Open(dem_path, gdal.GA_ReadOnly)
Exemple #56
0
from http.client import HTTPConnection


#1 연결
connection=HTTPConnection('www.example.com')

#2 요청보내기
connection.request('GET','/') # '/'만써주면 웹에 설정되어있는 default문서 받아옴
# GET방식으로 받아옴

#3 응답받기
response = connection.getresponse()
print(response.status,response.reason)

#4 Body읽어오기
body = response.read()
print(body)

#
# 404 에러 받아보기
#
connection.request('GET','/babo.html')
response=connection.getresponse()
print(response.status, response.reason)
from http.client import HTTPConnection

conn = HTTPConnection('www.example.com')

conn.request('GET', '/')
resp = conn.getresponse()
print(resp.status, resp.reason)

# 성공
# GET / HTTP / 1.1
# 200 OK
if resp.status == 200:
    body = resp.read()
    print(type(body), body)

# 실패
# GET / hello.html HTTP / 1.1
# 404 Not Found
conn.request('GET', '/hello.html')
resp = conn.getresponse
print(resp.status, resp.reason)
import socket

print(socket.gethostname())

import http
from http.client import HTTPConnection

conn = HTTPConnection("api.open-notify.org")
conn.connect()
conn.request("GET", "/iss-now.json")

result = conn.getresponse()
print("\n", result.read().decode("utf-8"))

conn.close()
def smoke_test_release(release, files, expected_hash, plugins):
    for release_file in files:
        if not os.path.isfile(release_file):
            raise RuntimeError('Smoketest failed missing file %s' %
                               (release_file))
        tmp_dir = tempfile.mkdtemp()
        if release_file.endswith('tar.gz'):
            run('tar -xzf %s -C %s' % (release_file, tmp_dir))
        elif release_file.endswith('zip'):
            run('unzip %s -d %s' % (release_file, tmp_dir))
        else:
            print('  Skip SmokeTest for [%s]' % release_file)
            continue  # nothing to do here
        es_dir = os.path.join(tmp_dir, 'elasticsearch-%s' % (release))
        es_run_path = os.path.join(es_dir, 'bin/elasticsearch')
        print('  Smoke testing package [%s]' % release_file)
        es_plugin_path = os.path.join(es_dir, 'bin/elasticsearch-plugin')
        plugin_names = {}
        for plugin in plugins:
            print('     Install plugin [%s]' % (plugin))
            run('%s; export ES_JAVA_OPTS="-Des.plugins.staging=%s"; %s %s %s' %
                (java_exe(), expected_hash, es_plugin_path, 'install -b',
                 plugin))
            plugin_names[plugin] = True
        if 'x-pack' in plugin_names:
            headers = {
                'Authorization':
                'Basic %s' %
                base64.b64encode(b"es_admin:foobar").decode("UTF-8")
            }
            es_shield_path = os.path.join(es_dir, 'bin/x-pack/users')
            print("     Install dummy shield user")
            run('%s; %s  useradd es_admin -r superuser -p foobar' %
                (java_exe(), es_shield_path))
        else:
            headers = {}
        print('  Starting elasticsearch deamon from [%s]' % es_dir)
        try:
            run('%s; %s -Enode.name=smoke_tester -Ecluster.name=prepare_release -Escript.inline=true -Escript.stored=true -Erepositories.url.allowed_urls=http://snapshot.test* %s -Epidfile=%s -Enode.portsfile=true'
                % (java_exe(), es_run_path, '-d',
                   os.path.join(es_dir, 'es-smoke.pid')))
            if not wait_for_node_startup(es_dir, header=headers):
                print("elasticsearch logs:")
                print('*' * 80)
                logs = read_fully(
                    os.path.join(es_dir, 'logs/prepare_release.log'))
                print(logs)
                print('*' * 80)
                raise RuntimeError('server didn\'t start up')
            try:  # we now get / and /_nodes to fetch basic infos like hashes etc and the installed plugins
                host = get_host_from_ports_file(es_dir)
                conn = HTTPConnection(host, timeout=20)
                conn.request('GET', '/', headers=headers)
                res = conn.getresponse()
                if res.status == 200:
                    version = json.loads(res.read().decode("utf-8"))['version']
                    if release != version['number']:
                        raise RuntimeError(
                            'Expected version [%s] but was [%s]' %
                            (release, version['number']))
                    if version['build_snapshot']:
                        raise RuntimeError('Expected non snapshot version')
                    if expected_hash != version['build_hash'].strip():
                        raise RuntimeError(
                            'HEAD hash does not match expected [%s] but got [%s]'
                            % (expected_hash, version['build_hash']))
                    print('  Verify if plugins are listed in _nodes')
                    conn.request('GET',
                                 '/_nodes/plugins?pretty=true',
                                 headers=headers)
                    res = conn.getresponse()
                    if res.status == 200:
                        nodes = json.loads(res.read().decode("utf-8"))['nodes']
                        for _, node in nodes.items():
                            node_plugins = node['plugins']
                            for node_plugin in node_plugins:
                                if not plugin_names.get(
                                        node_plugin['name'].strip(), False):
                                    raise RuntimeError('Unexpected plugin %s' %
                                                       node_plugin['name'])
                                del plugin_names[node_plugin['name']]
                        if plugin_names:
                            raise RuntimeError('Plugins not loaded %s' %
                                               list(plugin_names.keys()))

                    else:
                        raise RuntimeError('Expected HTTP 200 but got %s' %
                                           res.status)
                else:
                    raise RuntimeError('Expected HTTP 200 but got %s' %
                                       res.status)
            finally:
                conn.close()
        finally:
            pid_path = os.path.join(es_dir, 'es-smoke.pid')
            if os.path.exists(
                    pid_path):  # try reading the pid and kill the node
                pid = int(read_fully(pid_path))
                os.kill(pid, signal.SIGKILL)
            shutil.rmtree(tmp_dir)
        print('  ' + '*' * 80)
        print()
Exemple #60
0
method = 'GET'

taskName = input('Enter the name of task: ')

username = '******'
password = '******'

currentDirectory = os.path.dirname(__file__)
fullPath = os.path.join(currentDirectory, ipConfigFile)
file = open(fullPath)
ipValue = file.read()

connection = HTTPConnection(ipValue, port=port)
authKey = b64encode(
    (username + ":" + password).encode('utf-8')).decode('utf-8')
headers = {"Authorization": "Basic " + authKey}
connection.request(method, url.format(taskName), headers=headers)
response = connection.getresponse()
fileContent = response.read().decode()

print("http://" + ipValue + ":" + str(port) + url.format(taskName))

print(fileContent)

fullPath = os.path.join(currentDirectory, '{}.xml'.format(taskName))
file = open(fullPath, 'w+')
fileContent = fileContent.replace('\r', '')
fileContent = fileContent.replace('TAGMAN DATA: ', '')
file.write(fileContent)
file.close()