示例#1
0
文件: tests.py 项目: acdha/django
    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()
示例#2
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)

    while True:
        try:
            conn = HTTPConnection(url, timeout=Constant.ConnectTimeout)
            # check leader exists
            conn.request("GET", "/v1/status/leader")
            resp = conn.getresponse()
            con = resp.read().decode("UTF-8").strip("")
            # json need to docode too
            try:
                leader = json.loads(con)
            except Exception as err:
                leader = ""

            print("Get leader response:" + str(resp.status) + " " + str(resp.reason) + " " + str(leader))

            conn.request("GET", "/v1/status/peers")
            resp = conn.getresponse()
            peers = resp.read().decode("UTF-8").strip("")
            print("Get peers response:" + str(resp.status) + " " + str(resp.reason) + " " + str(leader))
            if leader != "" and peers != "":
                peerlist = json.loads(peers)
                if leader in peerlist:

                    # check leader format
                    leaderip = leader[:str.find(leader, ":")]
                    try:
                        ipv4 = ipaddress.IPv4Address(leaderip)
                        print("Leader ip check pass: "******"Check completed, the cluster is in sync state.")
                        break
                    except Exception as err:
                        print("Found error:" + str(err) + " will quit health check.")
                        print(traceback.format_exc())
                        sys.exit(1)

                else:
                    print("Cluster status checks failed, leader is not in the peerlist.")
            else:
                print("Cluster status checks failed, leader or peers empty.")

        except Exception as err:
            print("Found error:" + str(err) + " while check started, continue loop...")
            print(traceback.format_exc())

        # sleep 100ms no , 1s is enough
        time.sleep(1)
        def update_item(server):
            http_conn = HTTPConnection(server.hostname, server.port)
            http_conn.connect()

            http_conn.request("PUT", endpoint, json.dumps(expected_body).encode(), {"Content-Type": "application/json"})

            results["response"] = http_conn.getresponse()

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

            results["response"] = http_conn.getresponse()

            http_conn.close()
示例#4
0
文件: tests.py 项目: Damgaard/django
 def test_protocol(self):
     """Launched server serves with HTTP 1.1."""
     with captured_stdout() as debug_output:
         conn = HTTPConnection(LiveServerViews.server_thread.host, LiveServerViews.server_thread.port)
         try:
             conn.set_debuglevel(1)
             conn.request('GET', '/example_view/', headers={"Connection": "keep-alive"})
             conn.getresponse().read()
             conn.request('GET', '/example_view/', headers={"Connection": "close"})
             conn.getresponse()
         finally:
             conn.close()
     self.assertEqual(debug_output.getvalue().count("reply: 'HTTP/1.1 200 OK"), 2)
    def testBasic(self):
        """
        Test Basic
        """
        console.terse("{0}\n".format(self.testBasic.__doc__))

        console.terse("{0}\n".format("Connecting ...\n"))
        hc = HTTPConnection('127.0.0.1', port=8080, timeout=1.0,)

        hc.connect()

        console.terse("{0}\n".format("Get '/echo?name=fame' ...\n"))
        headers = odict([('Accept', 'application/json')])
        hc.request(method='GET', path='/echo?name=fame', body=None, headers=headers )
        response = hc.getresponse()
        console.terse(str(response.fileno()) + "\n")  # must call this before read
        console.terse(str(response.getheaders()) + "\n")
        console.terse(str(response.msg)  + "\n")
        console.terse(str(response.version) + "\n")
        console.terse(str(response.status) + "\n")
        console.terse(response.reason + "\n")
        console.terse(str(response.read()) + "\n")

        console.terse("{0}\n".format("Post ...\n"))
        headers = odict([('Accept', 'application/json'), ('Content-Type', 'application/json')])
        body = odict([('name', 'Peter'), ('occupation', 'Engineer')])
        body = ns2b(json.dumps(body, separators=(',', ':')))
        hc.request(method='POST', path='/demo', body=body, headers=headers )
        response = hc.getresponse()
        console.terse(str(response.fileno()) + "\n") # must call this before read
        console.terse(str(response.getheaders()) + "\n")
        console.terse(str(response.msg)  + "\n")
        console.terse(str(response.version) + "\n")
        console.terse(str(response.status) + "\n")
        console.terse(response.reason+ "\n")
        console.terse(str(response.read()) + "\n")

        #console.terse("{0}\n".format("SSE stream ...\n"))
        #body = b''
        #headers = odict([('Accept', 'application/json'), ('Content-Type', 'application/json')])
        #hc.request(method='GET', path='/stream', body=body, headers=headers )
        #response = hc.getresponse()
        #console.terse(str(response.fileno()) + "\n") # must call this before read
        #console.terse(str(response.getheaders()) + "\n")
        #console.terse(str(response.msg)  + "\n")
        #console.terse(str(response.version) + "\n")
        #console.terse(str(response.status) + "\n")
        #console.terse(response.reason+ "\n")
        #console.terse(str(response.read()) + "\n")

        hc.close()
示例#6
0
    def test_requesting_metrics_disabled(self, pghoard):
        pghoard.write_backup_state_to_json_file()
        conn = HTTPConnection(host="127.0.0.1", port=pghoard.config["http_port"])
        conn.request("GET", "/metrics")
        response = conn.getresponse()
        assert response.status == 501

        conn.request("GET", "/metrics/foo")
        response = conn.getresponse()
        assert response.status == 400

        conn.request("GET", "/{}/metrics".format(pghoard.test_site))
        response = conn.getresponse()
        assert response.status == 400
示例#7
0
文件: support.py 项目: loads/ailoads
def run_server(port=8888):
    """Running in a subprocess to avoid any interference
    """
    def _run():
        os.chdir(HERE)
        socketserver.TCPServer.allow_reuse_address = True
        attempts = 0
        httpd = None
        error = None

        while attempts < 3:
            try:
                httpd = socketserver.TCPServer(("", port), HandlerRedirect)
                break
            except Exception as e:
                error = e
                attempts += 1
                time.sleep(.1)

        if httpd is None:
            raise OSError("Could not start the coserver: %s" % str(error))

        def _shutdown(*args, **kw):
            httpd.server_close()
            sys.exit(0)

        signal.signal(signal.SIGTERM, _shutdown)
        signal.signal(signal.SIGINT, _shutdown)
        httpd.serve_forever()

    p = multiprocessing.Process(target=_run)
    p.start()
    start = time.time()
    connected = False

    while time.time() - start < 5 and not connected:
        try:
            conn = HTTPConnection('localhost', 8888)
            conn.request("GET", "/")
            conn.getresponse()
            connected = True
        except Exception:
            time.sleep(.1)
    if not connected:
        os.kill(p.pid, signal.SIGTERM)
        p.join(timeout=1.)
        raise OSError('Could not connect to coserver')
    return p
示例#8
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'))
    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)
示例#10
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()
示例#11
0
def query_host(world):
    u = urlparse(world['status'])
    
    c = HTTPConnection(u.netloc, 80)
    c.putrequest("GET", u.path + '?' + u.query)
    c.putheader("Content-Type", "text/xml; charset=utf-8")
    c.endheaders()

    r = c.getresponse()
    if r.getcode() is not 200:
        raise RuntimeError("Failed to query information about the server.")
    
    rdata = r.read().decode("utf-8")

    if len(rdata) is 0:
        print("The given server appears to be down.")
        exit(0)

    xml = ElementTree.fromstring(rdata)
    loginserver = xml.find("loginservers").text
    loginservers = loginserver.split(';')
    worldqueue = xml.find("queueurls").text
    worldqueues = worldqueue.split(';')

    world['host'] = world['login'] = loginservers[0]
    world['queue'] = worldqueues[0]

    return world
示例#12
0
 def send_encoded(self, url, data):
     url2 = urlparse(url)
     host = url2.netloc
     path = url2.path or '/'
     query = '?' + url2.query if url2.query else ''
     request = path + query
     
     data2 = '&'.join((
         quote(k) + '=' + quote_from_bytes(str(v).encode('utf8'))
         for k, v in data.items()
     )).encode('ascii')
     
     try:
         http = HTTPConnection(host)
         http.putrequest('POST', request)
         http.putheader('Content-Type', 'application/x-www-form-urlencoded')
         http.putheader('User-Agent', 'Fluxid MOC Scrobbler 0.2')
         http.putheader('Content-Length', str(len(data2)))
         http.endheaders()
         http.send(data2)
         response = http.getresponse().read().decode('utf8').upper().strip()
     except Exception as e:
         raise HardErrorException(str(e))
     if response == 'BADSESSION':
         raise BadSessionException
     elif response.startswith('FAILED'):
         raise FailedException(response.split(' ', 1)[1].strip() + (' POST = [%r]' % data2))
示例#13
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)
示例#14
0
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)
示例#15
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"}
示例#16
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
示例#17
0
文件: client.py 项目: Turgon37/Netsav
  def queryState(self):
    """Execute a request for retrieving the associated host's state

    Make an HTTP query to determine if the server is reachable or not
    @return[int] : the server status
    """
    c_retry = 0
    c_success = 0

    # Max retry is defined by config
    while c_retry < self.max_retry:
      # if the query success the try block will continue
      # if not the except block will be run
      h = HTTPConnection(self.address, self.port, timeout=self.tcp_timeout)
      try:
        # try to query
        status = h.request(self.query_method, '/')
        # parsing the result
        res = h.getresponse()
        c_success += 1
        sys_log.debug('[' + self.getName() + '] get server code : %d',
                      res.status)
        # if we have sufficient number of success
        if c_success >= self.min_retry:
          return self.AVAILABLE
      except:
        sys_log.debug('[' + self.getName() + '] unable to reach the host')
      finally:
        c_retry += 1
    # if we have reach the max retry amount
    return self.UNAVAILABLE
示例#18
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
示例#19
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
示例#20
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)
示例#21
0
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)
示例#22
0
 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()
示例#23
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
示例#24
0
文件: utils.py 项目: alinefm/kimchi
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
示例#25
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()
示例#26
0
文件: tests.py 项目: acdha/django
    def test_keep_alive_connection_clears_previous_request_data(self):
        conn = HTTPConnection(LiveServerViews.server_thread.host, LiveServerViews.server_thread.port)
        try:
            conn.request('POST', '/method_view/', b'{}', headers={"Connection": "keep-alive"})
            response = conn.getresponse()
            self.assertFalse(response.will_close)
            self.assertEqual(response.status, 200)
            self.assertEqual(response.read(), b'POST')

            conn.request('POST', '/method_view/', b'{}', headers={"Connection": "close"})
            response = conn.getresponse()
            self.assertFalse(response.will_close)
            self.assertEqual(response.status, 200)
            self.assertEqual(response.read(), b'POST')
        finally:
            conn.close()
示例#27
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)
示例#28
0
	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()
示例#29
0
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
示例#30
0
    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)
示例#31
0
    def test_get_invalid(self, pghoard, tmpdir):
        ne_xlog_seg = "0000FFFF0000000C000000FE"
        nonexistent_xlog = "/{}/archive/{}".format(pghoard.test_site,
                                                   ne_xlog_seg)
        # x-pghoard-target-path missing
        conn = HTTPConnection(host="127.0.0.1",
                              port=pghoard.config["http_port"])
        conn.request("GET", nonexistent_xlog)
        status = conn.getresponse().status
        assert status == 400
        # missing xlog file
        headers = {
            "x-pghoard-target-path": str(tmpdir.join("test_get_invalid"))
        }
        conn.request("GET", nonexistent_xlog, 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_xlog, headers=headers)
        status = conn.getresponse().status
        assert status == 400
        # missing xlog file
        headers = {
            "x-pghoard-target-path": str(tmpdir.join("test_get_invalid"))
        }
        conn.request("HEAD", nonexistent_xlog)
        status = conn.getresponse().status
        assert status == 404
        # missing xlog file using restore_command
        with pytest.raises(postgres_command.PGCError) as excinfo:
            restore_command(site=pghoard.test_site,
                            xlog=os.path.basename(nonexistent_xlog),
                            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_xlog_seg = "0000DDDD0000000D000000FC"
        valid_xlog = "/{}/xlog/{}".format(pghoard.test_site, valid_xlog_seg)
        store = pghoard.transfer_agents[0].get_object_storage(
            pghoard.test_site)
        store.store_file_from_memory(valid_xlog,
                                     wal_header_for_file(valid_xlog_seg),
                                     metadata={"a": "b"})
        conn.request("HEAD", valid_xlog)
        status = conn.getresponse().status
        assert status == 200
        restore_command(site=pghoard.test_site,
                        xlog=os.path.basename(valid_xlog),
                        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_xlog, headers=headers)
        status = conn.getresponse().status
        assert status == 400

        # inject a failure by making a static function fail
        failures = [0, ""]

        def get_failing_func(orig_func):
            def failing_func(*args):
                if failures[0] > 0:
                    failures[0] -= 1
                    raise Exception("test_get_invalid failure: {}".format(
                        failures[1]))
                return orig_func(*args)

            return failing_func

        for ta in pghoard.transfer_agents:
            store = ta.get_object_storage(pghoard.test_site)
            store.get_contents_to_string = get_failing_func(
                store.get_contents_to_string)

        prefetch_n = pghoard.config["restore_prefetch"]
        try:
            # we should have two retries + all prefetch operations
            pghoard.webserver.server.prefetch_404.clear()
            failures[0] = 2 + prefetch_n
            failures[1] = "test_two_fails_success"
            headers = {
                "x-pghoard-target-path": str(tmpdir.join("test_get_invalid_2"))
            }
            conn.request("GET", valid_xlog, headers=headers)
            status = conn.getresponse().status
            assert status == 201
            assert failures[0] == 0

            # so we should have a hard failure after three attempts
            pghoard.webserver.server.prefetch_404.clear()
            failures[0] = 4 + prefetch_n
            failures[1] = "test_three_fails_error"
            headers = {
                "x-pghoard-target-path": str(tmpdir.join("test_get_invalid_3"))
            }
            conn.request("GET", valid_xlog, headers=headers)
            status = conn.getresponse().status
            assert status == 500
            assert failures[0] == 1
        finally:
            # clear transfer cache to avoid using our failing versions
            for ta in pghoard.transfer_agents:
                ta.site_transfers = {}
示例#32
0
def readFromServer(path):
    conn = HTTPConnection('mobilewin.csse.rose-hulman.edu', port=5600)
    conn.request('GET', str(path))
    response = conn.getresponse()
    data = response.read()
    return str(data, 'UTF-8')
示例#33
0
from http.client import HTTPConnection

host = 'www.example'
conn = HTTPConnection(host)
conn.request('GET', '/')
r1 = conn.getresponse()
print(r1.status, r1.reason)

data1 = r1.read()
#data1 = r1.read(100)

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

data2 = r2.read()
print(data2.decode())

conn.close()

示例#34
0
class TestRegression(TestCase):
    def setUp(self):
        self.ip = "localhost"
        self.port = "8080"
        self.headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
        self.timeout = 10
        # connect
        self.conn = HTTPConnection(self.ip, self.port, timeout=self.timeout)
        # on board necessary descriptors
        # drop all databases
        # create the 5gtso db
        # python imports

        # load db IP port
        config = RawConfigParser()
        config.read("../db/db.properties")
        db_ip = config.get("MongoDB", "db.ip")
        db_port = int(config.get("MongoDB", "db.port"))
        operation_client = MongoClient(db_ip, db_port)
        fgtso_db = operation_client.fgtso
        fgtso_db.nsd.delete_many({})
        fgtso_db.vnfd.delete_many({})
        fgtso_db.ns.delete_many({})
        fgtso_db.nsir.delete_many({})
        fgtso_db.operation.delete_many({})
        fgtso_db.resources.delete_many({})
        # load desccriptors
        # path to descriptors folders

        path = "../descriptors/"
        # list of file names that contain ns and vnf descriptors
        ns_descriptors = ["CDN_all_NSD_0_2.json"]
        vnf_descriptors = [
            "CDN_SPR1_VNFD_0_2.json", "CDN_SPR21_VNFD_0_2.json",
            "CDN_SPR22_VNFD_0_2.json", "CDN_WEBSERVER_VNFD_0_2.json"
        ]
        # correspondance of nsdId and nsdCloudifyId
        nsdCloudifyId = {"vCDN_v02": "unknown"}
        # for each nsd create record to be inserted
        nsd_json = {}  # load json file here
        for nsd_file in ns_descriptors:
            with open(path + nsd_file) as nsd_json:
                nsd_json = load(nsd_json)
            nsd_record = {
                "nsdId": nsd_json["nsd"]["nsdIdentifier"],
                "nsdCloudifyId":
                nsdCloudifyId[nsd_json["nsd"]["nsdIdentifier"]],
                "version": nsd_json["nsd"]["version"],
                "nsdName": nsd_json["nsd"]["nsdName"],
                "nsdJson": nsd_json
            }
            fgtso_db.nsd.insert_one(nsd_record)
        # for each nsd create record to be inserted
        vnfd_json = {}  # load json file here
        for vnfd_file in vnf_descriptors:
            with open(path + vnfd_file) as vnfd_json:
                vnfd_json = load(vnfd_json)
            vnfd_record = {
                "vnfdId": vnfd_json["vnfdId"],
                "vnfdVersion": vnfd_json["vnfdVersion"],
                "vnfdName": vnfd_json["vnfProductName"],
                "vnfdJson": vnfd_json
            }
            fgtso_db.vnfd.insert_one(vnfd_record)

    def tearDown(self):
        self.conn.close()

    def test_1_basic_instantiation_and_termination(self):
        """
        Check basic operations tests expecting a 200/201 result.
        """

        # request an nsId
        path = "/5gt/so/v1/ns"
        data = {
            "nsDescription": "nsDescription",
            "nsName": "nsName",
            "nsdId": "vCDN_v02"
        }
        self.conn.request("POST", path, dumps(data), self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        data = rsp.read().decode()
        self.assertEqual(rsp.status, 201)
        data = loads(data)
        nsId = data["nsId"]

        # instantiate the service
        path = "/5gt/so/v1/ns/" + nsId + "/instantiate"
        data = {
            "flavourId": "df_vCDN",
            "nsInstantiationLevelId": "il_vCDN_small"
        }
        self.conn.request("PUT", path, dumps(data), self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        data = rsp.read().decode()
        self.assertEqual(rsp.status, 200)
        data = loads(data)
        operationId = data["operationId"]

        # get operation status
        path = "/5gt/so/v1/operation/" + operationId
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 200)

        # get ns information
        path = "/5gt/so/v1/ns/" + nsId
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 200)

        # terminate the service
        path = "/5gt/so/v1/ns/" + nsId + "/terminate"
        self.conn.request("PUT", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 200)

        # the following tests expect the NSD vCDN 0.2 and its VNFs to be loaded in the NSD/VNFD catalogues
        # get a NSD
        path = "/5gt/so/v1/ns/nsd/vCDN_v02/0.2"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 200)

        # get a VNFD
        path = "/5gt/so/v1/ns/vnfd/spr22/0.2"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 200)

    def test_2_nbi_400_404_errors(self):
        """
        Check that NBI returns error 400/404.
        """

        # request an nsId
        path = "/5gt/so/v1/ns"
        data = {"nsDescription": "nsDescription", "nsName": "nsName"}
        self.conn.request("POST", path, dumps(data), self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 400)

        # request an nsId
        path = "/5gt/so/v1/ns"
        data = {
            "nsDescription": "nsDescription",
            "nsName": "nsName",
            "nsdId": "vCDN_v02"
        }
        self.conn.request("POST", path, dumps(data), self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        data = rsp.read().decode()
        self.assertEqual(rsp.status, 201)
        data = loads(data)
        nsId = data["nsId"]

        # instantiate the service
        path = "/5gt/so/v1/ns/" + nsId + "/instantiate"
        data = {"flavourId": 1}
        self.conn.request("PUT", path, dumps(data), self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 400)

        # instantiate the service
        path = "/5gt/so/v1/ns/unknowonId/instantiate"
        data = {
            "flavourId": "flavourId",
            "nsInstantiationLevelId": "nsInstantiationLevelId"
        }
        self.conn.request("PUT", path, dumps(data), self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # get operation status
        path = "/5gt/so/v1/operation/unknownId"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # get ns information
        path = "/5gt/so/v1/ns/unkownId"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # terminate the service
        path = "/5gt/so/v1/ns/unknownId/terminate"
        self.conn.request("PUT", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # get a NSD
        path = "/5gt/so/v1/ns/nsd/CDN_v02/0.2"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # get a NSD
        path = "/5gt/so/v1/ns/nsd/vCDN_v02/0.3"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # get a VNFD
        path = "/5gt/so/v1/ns/vnfd/tpr22/0.2"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)

        # get a VNFD
        path = "/5gt/so/v1/ns/vnfd/spr22/0.3"
        self.conn.request("GET", path, None, self.headers)
        self.conn.sock.settimeout(self.timeout)
        rsp = self.conn.getresponse()
        self.assertEqual(rsp.status, 404)
示例#35
0
def _request(method, url, params={}, data={}, headers={}):
    splits = urlsplit(url)
    netloc = splits[1]
    if '@' in netloc:
        netloc_noauth = netloc.split('@')[1]
    else:
        netloc_noauth = netloc

    scheme = splits[0]
    path = splits[2]
    query = splits[3]
    fragment = splits[4]

    username = ''
    password = ''
    if '@' in netloc:
        password = netloc.split('@')[0][1:]
    if ':' in netloc_noauth:
        netloc_noauth, port = netloc_noauth.split(':')
    else:
        port = 80

    url = urlunsplit((scheme, netloc_noauth, path, query, fragment))
    if method in ['GET', 'DELETE']:
        params = urlencode(params, True)
        if params:
            if '?' not in url:
                url += '?' + params
            else:
                url += '&' + params

    connection = HTTPConnection(netloc_noauth, port)
    if username or password:
        credentials = "%s:%s" % (username, password)
        base64_credentials = base64.encodebytes(credentials.encode()).decode()
        authorization = "Basic %s" % base64_credentials[:-1]
        headers['Authorization'] = authorization

    headers['User-Agent'] = __USER_AGENT

    if data:
        body = anyjson.serialize(data)
    else:
        body = ''

    connection.request(method, url, body, headers)

    response = connection.getresponse()
    response.body = response.read()
    if _is_ok(response.status):
        if response.body:
            try:
                response.body = anyjson.deserialize(response.body.decode())
            except ValueError as e:
                raise InvalidResponseFromServer(
                    'The JSON response could not be parsed: %s.\n%s' %
                    (e, response.body))
            ret = response.status, response.body
        else:
            ret = response.status, None
    elif response.status == 401:
        raise Unauthorized('Authorization required. Use your private api_url.')
    else:
        raise HttpException(response.status, response.body)
    connection.close()
    return ret
示例#36
0
def datasource(lat, lon, source_dir):
    """ Return a gdal datasource for a NED 10m lat, lon corner.
    
        If it doesn't already exist locally in source_dir, grab a new one.
    """
    # FIXME for southern/western hemispheres
    fmt = 'http://ned.stamen.com/1km/n%02dw%03d.tif.gz'
    url = fmt % (abs(lat), abs(lon))

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

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

    local_base = join(local_dir, basename(path)[:-7])
    local_path = local_base + '.tif'
    local_none = local_base + '.404'

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

    if exists(local_none):
        return None

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

    assert isdir(local_dir)

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

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

    if resp.status in range(400, 500):
        # we're probably outside the coverage area
        print(url, file=open(local_none, 'w'))
        return None

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

    body = StringIO(resp.read())
    file = GzipFile(fileobj=body, mode='r')

    open(local_path, mode='w').write(file.read())

    #
    # The file better exist locally now
    #
    return gdal.Open(local_path, gdal.GA_ReadOnly)
示例#37
0
            s.starttls()
            s.ehlo()
            s.login(sender, '2rhksrn7493')
            s.sendmail(sender, reciver, msg.as_string())
            print('전송완료')
            s.close()


원자력리스트 = 원전리스트()

conn = HTTPConnection('khnp.co.kr')
conn.request(
    'GET',
    'http://www.khnp.co.kr/environ/service/realtime/radiorate?userkey=zuxJrlJij2dzU48AWBr2F02LjlKKdW8qLfCb7vimllYvqNyj6d02rPkiTqnHojcA3px9EKugkbmgLVTJuujfEg%3D%3D'
)
req = conn.getresponse()
print(req.status)
data = req.read()
tree = ElementTree.fromstring(data)
ele = tree.getiterator('item')

mquit = 1

#원자력리스트.목록출력()
#원자력리스트.원전정렬()
#원자력리스트.목록출력()


def AllList():
    global 원전목록
    global ele
 def test_GET_response_header_is_proxied_back_to_the_client(self):
     self.app.route('GET', status=200, headers={'Foo': 'Bar'})
     client = HTTPConnection('localhost', self.stenograpi.port)
     client.request('GET', '/')
     response = client.getresponse()
     self.assertEqual(response.getheader('Foo'), 'Bar')
示例#39
0
        try:
            version = re.search('mcdhttpd/(\d\.\d)', curBanner['data']).group(1)
        except AttributeError:
            version = 'unknown'
        # Limit scan to selected country
        if args.country is not None and args.country != country_code:
            continue
        host_count += 1
        user_and_pass = b64encode(CREDENTIALS).decode('ascii')
        try:
            c = HTTPConnection(ip, port=port, timeout=2)
            headers = {'Authorization': 'Basic {}'.format(user_and_pass)}

            # Extract the product type
            c.request('GET', '/branding/branding.js', headers=headers)
            branding_request = c.getresponse()
            if branding_request.status == 200:
                js = branding_request.read().decode('utf-8')
                try:
                    product = re.search('product:"([^"]+)",ddns:', js).group(1)
                except AttributeError:
                    product = None
            else:
                product = None

            # Extract the status-data type (publicly available)
            c.request('GET', '/get_status.cgi', headers=headers)
            status_request = c.getresponse()
            status = status_request.read().decode('utf-8') if status_request.status == 200 else None

            # Extract the configuration-data type
示例#40
0
def open_port(service_url,
              external_port,
              internal_client,
              internal_port=None,
              protocol='TCP',
              duration=0,
              description=None,
              enabled=1):
    parsedurl = urlparse(service_url)

    if internal_port == None:
        internal_port = external_port

    if description == None:
        description = 'generated by port-forward.py'

    if not enabled:
        duration = 1

    doc = Document()

    # create the envelope element and set its attributes
    envelope = doc.createElementNS('', 's:Envelope')
    envelope.setAttribute('xmlns:s',
                          'http://schemas.xmlsoap.org/soap/envelope/')
    envelope.setAttribute('s:encodingStyle',
                          'http://schemas.xmlsoap.org/soap/encoding/')

    # create the body element
    body = doc.createElementNS('', 's:Body')

    # create the function element and set its attribute
    fn = doc.createElementNS('', 'u:AddPortMapping')
    fn.setAttribute('xmlns:u',
                    'urn:schemas-upnp-org:service:WANIPConnection:1')

    # setup the argument element names and values
    # using a list of tuples to preserve order
    arguments = [
        ('NewRemoteHost', ""),  # unused - but required
        ('NewExternalPort', external_port),  # specify port on router
        ('NewProtocol', protocol),  # specify protocol
        ('NewInternalPort', internal_port),  # specify port on internal host
        ('NewInternalClient', internal_client),  # specify IP of internal host
        ('NewEnabled', enabled),  # turn mapping ON
        ('NewPortMappingDescription', description),  # add a description
        ('NewLeaseDuration', duration)
    ]  # how long should it be opened?

    # NewEnabled should be 1 by default, but better supply it.
    # NewPortMappingDescription Can be anything you want, even an empty string.
    # NewLeaseDuration can be any integer BUT some UPnP devices don't support it,
    # so set it to 0 for better compatibility.

    # container for created nodes
    argument_list = []

    # iterate over arguments, create nodes, create text nodes,
    # append text nodes to nodes, and finally add the ready product
    # to argument_list
    for k, v in arguments:
        v = str(v)
        tmp_node = doc.createElement(k)
        tmp_text_node = doc.createTextNode(v)
        tmp_node.appendChild(tmp_text_node)
        argument_list.append(tmp_node)

    # append the prepared argument nodes to the function element
    for arg in argument_list:
        fn.appendChild(arg)

    # append function element to the body element
    body.appendChild(fn)

    # append body element to envelope element
    envelope.appendChild(body)

    # append envelope element to document, making it the root element
    doc.appendChild(envelope)

    # our tree is ready, conver it to a string
    pure_xml = doc.toxml()

    # use the object returned by urlparse.urlparse to get the hostname and port
    conn = HTTPConnection(parsedurl.hostname, parsedurl.port)

    # use the path of WANIPConnection (or WANPPPConnection) to target that service,
    # insert the xml payload,
    # add two headers to make tell the server what we're sending exactly.
    conn.request(
        'POST', parsedurl.path, pure_xml, {
            'SOAPAction':
            '"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"',
            'Content-Type': 'text/xml'
        })

    # wait for a response
    resp = conn.getresponse()

    return resp.status, resp.read()
class Client:
    """
   Crossbar.io HTTP bridge client.
   """
    def __init__(self, url, key=None, secret=None, timeout=5, context=None):
        """
      Create a new Crossbar.io push client.

      The only mandatory argument is the Push service endpoint of the Crossbar.io
      instance to push to.

      For signed pushes, provide authentication key and secret. If those are not
      given, unsigned pushes are performed.

      :param url: URL of the HTTP bridge of Crossbar.io (e.g. http://example.com:8080/push).
      :type url: str
      :param key: Optional key to use for signing requests.
      :type key: str
      :param secret: When using signed request, the secret corresponding to key.
      :type secret: str
      :param timeout: Timeout for requests.
      :type timeout: int
      :param context: If the HTTP bridge is running on HTTPS (that is securely over TLS),
         then the context provides the SSL settings the client should use (e.g. the
         certificate chain against which to verify the server certificate). This parameter
         is only available on Python 2.7.9+ and Python 3 (otherwise the parameter is silently
         ignored!). See: https://docs.python.org/2/library/ssl.html#ssl.SSLContext
      :type context: obj or None
      """
        assert (type(url) == str)
        assert ((key and secret) or (not key and not secret))
        assert (key is None or type(key) == str)
        assert (secret is None or type(secret) == str)
        assert (type(timeout) == int)
        if _HAS_SSL and _HAS_SSL_CLIENT_CONTEXT:
            assert (context is None or isinstance(context, ssl.SSLContext))

        self._seq = 1
        self._key = key
        self._secret = secret

        self._endpoint = _parse_url(url)
        self._endpoint['headers'] = {
            "Content-type": "application/json",
            "User-agent": "crossbarconnect-python"
        }

        if self._endpoint['secure']:
            if not _HAS_SSL:
                raise Exception(
                    "Bridge URL is using HTTPS, but Python SSL module is missing"
                )
            if _HAS_SSL_CLIENT_CONTEXT:
                self._connection = HTTPSConnection(self._endpoint['host'],
                                                   self._endpoint['port'],
                                                   timeout=timeout,
                                                   context=context)
            else:
                self._connection = HTTPSConnection(self._endpoint['host'],
                                                   self._endpoint['port'],
                                                   timeout=timeout)
        else:
            self._connection = HTTPConnection(self._endpoint['host'],
                                              self._endpoint['port'],
                                              timeout=timeout)

    def publish(self, topic, *args, **kwargs):
        """
      Publish an event to subscribers on specified topic via Crossbar.io HTTP bridge.

      The event payload (positional and keyword) can be of any type that can be
      serialized to JSON.

      If `kwargs` contains an `options` attribute, this is expected to
      be a dictionary with the following possible parameters:

       * `exclude`: A list of WAMP session IDs to exclude from receivers.
       * `eligible`: A list of WAMP session IDs eligible as receivers.

      :param topic: Topic to push to.
      :type topic: str
      :param args: Arbitrary application payload for the event (positional arguments).
      :type args: list
      :param kwargs: Arbitrary application payload for the event (keyword arguments).
      :type kwargs: dict

      :returns int -- The event publication ID assigned by the broker.
      """
        assert (type(topic) == str)

        ## this will get filled and later serialized into HTTP/POST body
        ##
        event = {'topic': topic}

        if 'options' in kwargs:
            event['options'] = kwargs.pop('options')
            assert (type(event['options']) == dict)

        if args:
            event['args'] = args

        if kwargs:
            event['kwargs'] = kwargs

        try:
            body = json.dumps(event, separators=(',', ':'))
            body = body.encode('utf8')

        except Exception as e:
            raise Exception(
                "invalid event payload - not JSON serializable: {0}".format(e))

        params = {
            'timestamp': _utcnow(),
            'seq': self._seq,
        }

        if self._key:
            ## if the request is to be signed, create extra fields and signature
            params['key'] = self._key
            params['nonce'] = random.randint(0, 9007199254740992)

            # HMAC[SHA256]_{secret} (key | timestamp | seq | nonce | body) => signature

            hm = hmac.new(self._secret.encode('utf8'), None, hashlib.sha256)
            hm.update(params['key'].encode('utf8'))
            hm.update(params['timestamp'].encode('utf8'))
            hm.update("{0}".format(params['seq']).encode('utf8'))
            hm.update("{0}".format(params['nonce']).encode('utf8'))
            hm.update(body)
            signature = base64.urlsafe_b64encode(hm.digest())

            params['signature'] = signature

        self._seq += 1

        path = "{0}?{1}".format(parse.quote(self._endpoint['path']),
                                parse.urlencode(params))

        ## now issue the HTTP/POST
        ##
        self._connection.request('POST', path, body, self._endpoint['headers'])
        response = self._connection.getresponse()
        response_body = response.read()

        if response.status != 202:
            raise Exception(
                "publication request failed {0} [{1}] - {2}".format(
                    response.status, response.reason, response_body))

        try:
            res = json.loads(response_body)
        except Exception as e:
            raise Exception("publication request bogus result - {0}".format(e))

        return res['id']
示例#42
0
Example: {0} foo john secret 12345
'''.format(basename(sys.argv[0])))
    sys.exit(0)

service = quote_plus(sys.argv[1])
credential = sys.argv[2] + ':' + sys.argv[3]
basic = 'Basic ' + base64.b64encode(credential.encode('utf-8')).decode('utf-8')
if len(sys.argv) > 4:
  port = sys.argv[4]
else:
  port = '8080'

conn = HTTPConnection('localhost', port)
conn.request('GET', '/login?TARGET=' + service, headers={'Authorization': basic})
response = conn.getresponse()
if response.status != 302:
    print('Unexpected status: ', response.status, response.reason)
    sys.exit(0)
location = response.getheader('Location')
qs = parse_qs(urlparse(location).query)
ticket = qs['SAMLart'][0]
validate_url = '/samlValidate?TARGET={service}'.format(service=service)
body = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <samlp:Request IssueInstant="{now}"
            MajorVersion="1" MinorVersion="1"
            RequestID="{id}" xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol">
            <samlp:AssertionArtifact>{ticket}</samlp:AssertionArtifact>
示例#43
0
def http_request(domain, path, method):
    """ Makes the http request using method """
    conn = HTTPConnection(domain)
    conn.request(method, path)
    resp = conn.getresponse()
    return resp.read()
示例#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()
示例#45
0
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_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 plugin in plugins:
            print('     Install plugin [%s]' % (plugin))
            run('%s; %s -Des.plugins.staging=true %s %s' %
                (java_exe(), es_plugin_path, 'install', plugin))
            plugin_names[plugin] = True
        if 'shield' in plugin_names:
            headers = {
                'Authorization':
                'Basic %s' %
                base64.b64encode(b"es_admin:foobar").decode("UTF-8")
            }
            es_shield_path = os.path.join(tmp_dir,
                                          'elasticsearch-%s' % (release),
                                          'bin/shield/esusers')
            print("     Install dummy shield user")
            run('%s; %s  useradd es_admin -r admin -p foobar' %
                (java_exe(), es_shield_path))
        else:
            headers = {}
        print('  Starting elasticsearch deamon from [%s]' %
              os.path.join(tmp_dir, 'elasticsearch-%s' % release))
        try:
            run('%s; %s -Des.node.name=smoke_tester -Des.cluster.name=prepare_release -Des.script.inline=on -Des.script.indexed=on -Des.repositories.url.allowed_urls=http://snapshot.test* %s -Des.pidfile=%s'
                % (java_exe(), es_run_path, '-d',
                   os.path.join(tmp_dir, 'elasticsearch-%s' %
                                (release), 'es-smoke.pid')))
            conn = HTTPConnection(host='127.0.0.1', port=9200, timeout=20)
            if not wait_for_node_startup(header=headers):
                print("elasticsearch logs:")
                print('*' * 80)
                logs = read_fully(
                    os.path.join(tmp_dir, 'elasticsearch-%s' % (release),
                                 '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
                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.startswith(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?plugin=true&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('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.close()
        finally:
            pid_path = os.path.join(tmp_dir, 'elasticsearch-%s' % (release),
                                    '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()
示例#46
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('POST', '', params, headers)
resp =conn.getresponse()
print(resp.status, resp.reason)

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

conn.close()
 def test_Server_response_header_is_empty_by_default(self):
     self.app.route('GET', status=200)
     client = HTTPConnection('localhost', self.stenograpi.port)
     client.request('GET', '/')
     response = client.getresponse()
     self.assertEqual(response.getheader('Server'), None)
示例#48
0
def Upload(body, headers={}):
    conn = HTTPConnection(f"{host}:{PORT}")
    conn.request('POST', '/', body=body, headers=headers)
    res = conn.getresponse()
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 %s' %
                (java_exe(), 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 -Des.script.inline=on -Des.script.indexed=on %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.cluster=%s -Dtests.jvms=1 -Dtests.class=*.*RestTests'
                        % ("127.0.0.1:9300"))
                    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)
 def test_POST_response_body_is_proxied_back_to_the_client(self):
     self.app.route('POST', '/', 200, b'POST RESPONSE')
     client = HTTPConnection('localhost', self.stenograpi.port)
     client.request('POST', '/')
     response = client.getresponse()
     self.assertEqual(response.read(), b'POST RESPONSE')
'''
 @Author: Ritika Patidar
 @Date: 2021-02-24 10:09:10
 @Last Modified by: Ritika Patidar
 @Last Modified time: 2021-02-24 10:09:38  
 @Title :  access and print a URL's content to the console problem
'''
#import module
from http.client import HTTPConnection
#make connection to the host
conn = HTTPConnection("google.com")
#get response of from the host
conn.request("GET", "/")
#get response to host
result = conn.getresponse()
# retrieves the entire contents.
contents = result.read()
print("======================================================\n", contents)
示例#52
0
    def run_test(self):

        ##################################################
        # Check correctness of the rpcauth config option #
        ##################################################
        url = urlparse(self.nodes[0].url)

        #Old authpair
        authpair = url.username + ':' + url.password

        #New authpair generated via share/rpcuser tool
        #rpcauth = "rpcauth=rt:93648e835a54c573682c2eb19f882535$7681e9c5b74bdd85e78166031d2058e1069b3ed7ed967c93fc63abba06f31144"
        password = "******"

        #Second authpair with different username
        #rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
        password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
        authpairnew = "rt:" + password

        headers = {"Authorization": "Basic " + str_to_b64str(authpair)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        resp = conn.getresponse()
        assert_equal(resp.status == 401, False)
        conn.close()

        #Use new authpair to confirm both work
        headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        resp = conn.getresponse()
        assert_equal(resp.status == 401, False)
        conn.close()

        #Wrong login name with rt's password
        authpairnew = "rtwrong:" + password
        headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        resp = conn.getresponse()
        assert_equal(resp.status == 401, True)
        conn.close()

        #Wrong password for rt
        authpairnew = "rt:" + password + "wrong"
        headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        resp = conn.getresponse()
        assert_equal(resp.status == 401, True)
        conn.close()

        #Correct for rt2
        authpairnew = "rt2:" + password2
        headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        resp = conn.getresponse()
        assert_equal(resp.status == 401, False)
        conn.close()

        #Wrong password for rt2
        authpairnew = "rt2:" + password2 + "wrong"
        headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}

        conn = HTTPConnection(url.hostname, url.port)
        conn.connect()
        conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
        resp = conn.getresponse()
        assert_equal(resp.status == 401, True)
        conn.close()
示例#53
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
    log_queue.put(["INFO", "*****Time measure: ROE starting ROE processing"])
    extracted_info = extract_nsd_info_for_pa(nsd_json, vnfds_json, request)
    log_queue.put(["INFO", "*****Time measure: ROE extracted NSD info at ROE"])
    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)])
    log_queue.put(["INFO", "*****Time measure: ROE retrieved MTP resources"])

    # 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_enable = config.get("PA", "pa.enable")
    placement_info = {}
    if pa_enable == "yes":
        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'
        }
        log_queue.put(["INFO", "*****Time measure: ROE PA request generated"])
        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"
            ])
        log_queue.put(["INFO", "output of the PA is: "])
        log_queue.put(["INFO", placement_info])
        placement_info = amending_pa_output(extracted_info["nsd"],
                                            placement_info)
        log_queue.put(["INFO", "*****Time measure: ROE PA calculation done"])
        log_queue.put(["INFO", "PA tuned output is:"])
        log_queue.put(["INFO", placement_info])
    else:
        # to be removed when PA code tested: static placement for testing purposes
        pa_responses = config.items("RESPONSE")
        for pa_response in pa_responses:
            if (nsd_json["nsd"]["nsdIdentifier"].lower().find(pa_response[0])
                    != -1):
                placement_info = json.loads(pa_response[1])
        log_queue.put(["INFO", "PA TUNED (manually) output is:"])
        log_queue.put(["DEBUG", placement_info])

    log_queue.put(
        ["DEBUG",
         "Service NameId is: %s" % nsd_json["nsd"]["nsdIdentifier"]])
    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"])
        log_queue.put(["INFO", "*****Time measure: ROE created VNF's"])
    if deployed_vnfs_info is not None:
        # list of VLs to be deployed
        vls_info = extract_vls_info_mtp(resources, extracted_info,
                                        placement_info, nsId_tmp, nestedInfo)
        log_queue.put(["INFO", "*****Time measure: ROE extracted VL's at MTP"])
        # ask network execution engine to deploy the virtual links
        eenet.deploy_vls(vls_info, nsId_tmp)
    log_queue.put(["INFO", "*****Time measure: ROE created LL's at MTP"])

    # 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 :)"])
def run_smart(aa_seq):
    """Makes a web request to the remote predictions server.
    Processes the reply (HTML) with beautiful soup and extracts the predictions list. 
    Returns the predictions in the end.
    """
    
    base_url = "http://smart.embl-heidelberg.de/smart/"
    smart_script_url = urljoin(base_url, 'show_motifs.pl')
    data = 'SEQUENCE={}'.format(aa_seq)
    _log.info('Web request to SMART..')
    reply = query_server(smart_script_url, data)
    soup = BeautifulSoup(reply, 'html.parser')
    #if the sequence is not precalculated, it might be put in queue
    if 'Sequence submitted into the processing queue' in soup.title.string:
        tags = soup.find_all('a')
        for t in tags:
            if 'job_status.pl?' in t['href']:
                job_url = t['href']
        newreq = urljoin(base_url, job_url)
        #we keep refreshing until done
        while True:
            conn = HTTPConnection("smart.embl-heidelberg.de")
            conn.request("GET", newreq)
            res = conn.getresponse()
            nbody = res.read()
            conn.close()
            soup = BeautifulSoup(nbody, 'html.parser')
            if soup.title.string == 'SMART: Sequence analysis results':
                break
            time.sleep(10)
            soup = BeautifulSoup(reply, 'html.parser')
    # If there are multiple identical sequences in the database (e.g. PCNA_HUMAN
    # SMART will ask the user to choose which one to display 
    if "Multiple matching sequences" in soup.title.string:
        protein_url = soup.find_all('td', {'class': 'prot'})[0].next['href']
        reply = requests.get(urljoin(base_url, protein_url))
        soup = BeautifulSoup(reply.content, 'html.parser') 
    scripts = soup('script', {'type' : 'text/javascript'})
    for s in scripts:
        if 'var domNfo' in s.text:
            script_text = s.text
            break
    else:
        return ['SMART','-'*len(aa_seq),[]] #return dashes only
    domain_info = re.compile(r'var domNfo={((.|\n)*)};')
    match = domain_info.search(script_text)
    if not match: #no domains found in protein
        return ['SMART','-'*len(aa_seq),[]] #return dashes only
    #remove var declaration and final ; to get clean json
    match = match.group().replace('var domNfo=','').replace('};','}') 
    domains = json.loads(match)
    annotated = ['-']*len(aa_seq)
    pdomains = []
    for domain in domains.values():
        start = int(domain['st'])
        stop = int(domain['en'])
        dname = domain['n']
        if dname == 'Low complexity region':
            dname = 'low complexity'
        start = int(domain['st'])
        stop = int(domain['en'])
        dlen = stop - start
        displayed = f'{dname} * * '
        dnlen = len(dname)
        if dlen <= dnlen:
            mult = 1
        else:
            mult = (dlen // dnlen) + 1
        dspan = displayed * mult
        annotated[start-1: stop] = dspan[:dlen+1]
        pdomains.append([start, stop, dname])
    annotated = ''.join(annotated)
    return ['SMART', annotated, pdomains]
 def test_404_response_status_is_proxied_back_to_the_client(self):
     self.app.route('GET', '/not-found', 404, b'Not found')
     client = HTTPConnection('localhost', self.stenograpi.port)
     client.request('GET', '/not-found')
     response = client.getresponse()
     self.assertEqual(response.status, 404)
示例#56
0
    def websocket_handshake(
        self,
        test_app,
        path="/",
        params=None,
        headers=None,
        subprotocols=None,
        timeout=1,
    ):
        """
        Runs a WebSocket handshake negotiation and returns the raw socket
        object & the selected subprotocol.

        You'll need to inject an accept or reject message before this
        to let it complete.
        """
        # Send it the request. We have to do this the long way to allow
        # duplicate headers.
        conn = HTTPConnection(test_app.host, test_app.port, timeout=timeout)
        # Make sure path is urlquoted and add any params
        path = parse.quote(path)
        if params:
            path += "?" + parse.urlencode(params, doseq=True)
        conn.putrequest("GET", path, skip_accept_encoding=True, skip_host=True)
        # Do WebSocket handshake headers + any other headers
        if headers is None:
            headers = []
        headers.extend([
            (b"Host", b"example.com"),
            (b"Upgrade", b"websocket"),
            (b"Connection", b"Upgrade"),
            (b"Sec-WebSocket-Key", b"x3JJHMbDL1EzLkh9GBhXDw=="),
            (b"Sec-WebSocket-Version", b"13"),
            (b"Origin", b"http://example.com"),
        ])
        if subprotocols:
            headers.append(
                (b"Sec-WebSocket-Protocol", ", ".join(subprotocols)))
        if headers:
            for header_name, header_value in headers:
                conn.putheader(header_name, header_value)
        conn.endheaders()
        # Read out the response
        try:
            response = conn.getresponse()
        except socket.timeout:
            # See if they left an exception for us to load
            test_app.get_received()
            raise RuntimeError(
                "Daphne timed out handling request, no exception found.")
        # Check we got a good response code
        if response.status != 101:
            raise RuntimeError(
                "WebSocket upgrade did not result in status code 101")
        # Prepare headers for subprotocol searching
        response_headers = dict(
            (n.lower(), v) for n, v in response.getheaders())
        response.read()
        assert not response.closed
        # Return the raw socket and any subprotocol
        return conn.sock, response_headers.get("sec-websocket-protocol", None)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from json import dumps, loads
# --------   for python 2
# from httplib import HTTPConnection
# -----------    for python 3
from http.client import HTTPConnection

# connect with REST server
connection = HTTPConnection('127.0.0.1', 80)
connection.connect()

# MAKE HTTP REQUEST
connection.request(
    'POST',
    '/add_list_of_commands',
    dumps({"ip": "192.168.2.254"}),
    {'Content-Type': 'application/json'},
)
print("Waiting for Server response:")
result = loads(connection.getresponse().read())
print(result)

# close the connection
connection.close()
示例#58
0
"""
"""
11.1 作为客户端与HTTP交互
"""
from urllib import request

# request.urlopen()
# 简单的实现GET,POST等
# 如果需要更加复杂的交互使用第三方库 requests
# 如果不希望使用第三方库 requests,还可以使用底层的http.client
from http.client import HTTPConnection
from urllib import parse

c = HTTPConnection('www.python.org', 80)
c.request('HEAD', '/index.html')
resp = c.getresponse()

print('Status', resp.status)
for n, v in resp.getheaders():
    print(n, v)
"""
11.2 创建TCP服务器
"""
"""
11.3 创建UDP服务器
"""
"""
11.4 通过CIDR地址生成对应的IP地址集
"""
# ipaddress模块
"""
示例#59
0
    try:
        i = l.index('=')
    except ValueError:
        log_and_exit("File corrupt:" + l)

    temperature = l[i + 1:-1]

except Exception as e:
    log_and_exit("File open failed:" + e)

obj = {
    "type": "temperature",
    #"time": datetime.datetime.now(), # not json serializable
    "token": token,
    "data": {
        "temperature": temperature
    }
}

# send the temperature to the server
# -----------------------------------------------------------
con = HTTPConnection(temphost, port)

headers = {"Content-Type": "application/json"}

body = json.dumps(obj)
print(body)
con.request("POST", "/temp", body, headers)
r = con.getresponse()
print(r.status, r.reason)
示例#60
0
    def run(self):
        o = self.scanner.requestObject
        #conn = HTTPConnection(o.hostname,o.port,timeout=self.scanner.spinTimeout.value())

        while not self.stopped():
            if self.scanner.indexFlag > self.scanner.total: break
            self.lock.acquire()
            self.scanner.indexFlag = self.scanner.indexFlag + self.batchSize
            self.lock.release()
            for index in range(self.scanner.indexFlag,
                               self.scanner.indexFlag + self.batchSize):
                if self.stopped(): break
                if index >= self.total:
                    self.stop()
                    break

                line = self.preparedURL[index]

                self.lock.acquire()
                self.scanner.count = self.scanner.count + 1
                self.lock.release()

                if not line.startswith('/'):
                    line = "/" + line
                url = o.path + line
                try:
                    conn = HTTPConnection(
                        o.hostname,
                        o.port,
                        timeout=self.scanner.spinTimeout.value())
                    conn.request("HEAD", url)
                    resp = conn.getresponse()
                    if (resp.status == 200 and self.scanner.cbx200.isChecked()) \
                        or (resp.status == 403 and self.scanner.cbx403.isChecked()) \
                        or (resp.status == 302 and self.scanner.cbx302.isChecked()):
                        self.scanner.emit(
                            SIGNAL("feedback"),
                            self.scanner.count * 100 / self.total,
                            self.scanner.count,
                            "<span style='color:green'>[%s]</span> <a href='%s'>%s</a>"
                            % (str(resp.status), o.scheme + "://" +
                               o.hostname + ":" + str(o.port) + url, o.scheme +
                               ":" + o.hostname + ":" + str(o.port) + url))

                except:
                    pass
                else:
                    conn.close()
                #except UnicodeEncodeError as ex:
                #    pass
                #except Exception as ex:
                #    pass
                #self.scanner.emit(SIGNAL("feedback"),0, 0, "<span style='color:red'>An error occured :</span>%s"%str(ex) )
                #self.scanner.emit(SIGNAL("feedback"),0, 0, "<span style='color:red'>Exceptions :</span><pre>%s</pre>" % str(traceback.format_exc()) )
                #self.scanner.startBtn.setDisabled(False)
                #else:
                #    conn.close()
                if index == self.total - 1:
                    self.stop()
                    self.scanner.emit(SIGNAL("feedback"), 100,
                                      self.scanner.count,
                                      "Scanning Completed!")
                    self.stop()
                    self.scanner.startBtn.setEnabled(True)
                    self.scanner.stopBtn.setEnabled(False)
                    break