Exemple #1
0
def checkAuth(usr):
	from http.client import HTTPConnection
	from urllib.parse import urlencode

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

	conn = HTTPConnection(settings.API_DOMAIN)
	conn.request('POST', '/' + 'get/interests/list', '', hdrs)
	res = conn.getresponse()
	data = '';
	if res.status == 401:
		data = res.read()
		data = str(data, encoding='utf-8')
		print('Wrong response status:')
		#print(' Command: {}'.format(url))
		print(' Status: {}'.format(res.status))
		print(' Data: {}'.format(data))
		data = '401'
	conn.close()
	return data
Exemple #2
0
 def wrap(self,content):
     self._LOGGING_ and self.logger.debug(f"[{self.name}] wrap: {len(content)} bytes")
     packedSotp = self.packSotp(content)
     conn = HTTPConnection(self.hostname, self.port,self.timeout)
     data_response, code_response = self.dispatchByMethod(conn, packedSotp)
     conn.close()
     self.inbox.put(self.messageToWrapper((data_response,code_response)))
def getWebXML(host):

    print('\ngetWebXML Called.\n') 

    Root = None

    print('Creating HTTP Connection to %s... ' % (host),end='')
    c = HTTPConnection(host)
    print('SUCCESS.')
    # print('\n',c,'\n')
    print('Trying to send request to host... ',end='')
    try:
        c.request("GET", "/home.cgi")
        print('SENT.')
    except TimeoutError:
        print('CONNECTION TIMEOUT.')
    except OSError as inst:
        print(inst)
    except CannotSendRequest:
        print('CANNOT SEND.')
    except:
        print('Unkown Error Occurred While Trying to Request Data.')
    else:
        print('Getting Reply... ')
        r = c.getresponse()
        print('Decoding... ')
        xml = r.read().decode("UTF-8")
        print('Closing HTTP Connection...')
        c.close()
        print('Parsing webpage...')
        Root = _fromstring(xml)
    print('\ngetWebElem Done.')
    return Root
Exemple #4
0
def run(url,params):
    httpClient = None
    data=None

    httpClient = HTTPConnection('192.168.106.92', 8880)

    headers = {'content-type': 'application/json','Accept-Language':'zh-CN'}  # post请求头需要有content-type
    json_str = json.dumps(params)

    httpClient.request('POST', url, json_str, headers)  # 这里可以不带key,直接用value



    response = httpClient.getresponse()
    print(response.getheaders())
    d=response.read().decode(encoding='utf-8')
    print(d)
    #print(zlib.decompress(data, 16+zlib.MAX_WBITS))
    data=json.loads(d, encoding='utf-8')


        #d=data.decode(encode='utf-8')

    print(data)
       # data = json.loads(response, encoding='utf-8')
        #f=json.loads(response.read())# 将获取到的内容转换为json类型数据
       # print(f['code'])
        #data = response.getcode()


    if httpClient:
        httpClient.close()
    return data
Exemple #5
0
 def send(self):
     """
     Sends the request to the :attr:`host`. Returns a
     :class:`PurgeResponse` or raises a :class:`PurgeError`.
     """
     log.info(self)
     headers = {
         self.header_mapping['domain']: self.domain or '.*',
         self.header_mapping['path']: self.path or '.*',
     }
     if self.soft:
         headers[self.header_mapping['soft']] = 'true'
     connection = HTTPConnection(*self.host, timeout=self.timeout)
     try:
         connection.request('PURGE', '/', headers=headers)
         response = connection.getresponse()
     except HTTPException as e:
         raise PurgeError('Connection to Varnish host failed: %s' % e)
     finally:
         connection.close()
     if response.status is not 200:
         raise PurgeError(response.reason)
     purge_respone = PurgeResponse(self, response)
     log.info(purge_respone)
     return purge_respone
Exemple #6
0
def uninstall_vl(vl_list, nsId):
    """
    Function description
    Parameters
    ----------
    param1: type
        param1 description
    Returns
    -------
    name: type
        return description
    """
    # ask mtp to deploy vl
    mtp_uri = "http://" + mtp_ip + ":" + mtp_port + mtp_base_path + "/abstract-network-resources"
    # connect to MTP and make the request
    header = {'Content-Type': 'application/json',
              'Accept': 'application/json'}
    body = {"interNfviPopConnnectivityIdList": [],
            "metaData": []}
    body["interNfviPopConnnectivityIdList"] = vl_list
    body["metaData"].append({"key": "ServiceId", "value": nsId})
    try:
        conn = HTTPConnection(mtp_ip, mtp_port)
        conn.request("DELETE", mtp_uri, dumps(body), header)
        rsp = conn.getresponse()
        deployed_vl_info = rsp.read()
        conn.close()
    except ConnectionRefusedError:
        # the MTP server is not running or the connection configuration is wrong
        log_queue.put(["ERROR", "the MTP server is not running or the connection configuration is wrong"])
class WebsiteConnection:
    def __init__(self,
                 address,
                 port=80,
                 user=None,
                 password=None,
                 **connection_kwargs):
        self.address = address
        self.port = port

        self.use_credentials = user is not None and password is not None

        if self.use_credentials:
            user_password = ("%s:%s" % (user, password)).encode()
            self.credentials = base64.b64encode(user_password).decode('ascii')
        else:
            self.credentials = ""

        self.connection = HTTPConnection("%s:%s" % (self.address, self.port),
                                         **connection_kwargs)

    def get_response(self, uri, content_type):
        headers = {
            'Content-type': content_type,
        }
        if self.use_credentials:
            headers['Authorization'] = 'Basic %s' % self.credentials

        self.connection.request("GET", uri, headers=headers)
        return self.connection.getresponse()

    def close(self):
        self.connection.close()
class SimpleTester(object):
    """ Specify kind of messages to be send.
        This let the user choose which kind of messages,
        each kind has specific properties to test LCD Screen.
    """
    def __init__(self, generator):
        self.httpconnection = HTTPConnection("192.168.1.3", 8000)
        self.generator = generator
        self.counter = 0

    def send_next(self):
        """ Send message using the specified generator to the deamon.

            Return the time to wait until sending next message.
        """
        self.counter += 1
        msg_json, wait = self.generator(self.counter)

        msg_json = json.dumps(msg_json)

        self.httpconnection.request("POST", "/",
                                    headers={'Content-Length': len(msg_json)})
        self.httpconnection.send(bytes(msg_json, 'UTF-8'))

        httpresponse = self.httpconnection.getresponse()
        print(httpresponse.msg)
        print(httpresponse.read())
        self.httpconnection.close()

        return wait
Exemple #9
0
 def __getitem__(self, ip):
     from http.client import HTTPConnection, HTTPException
     from urllib.parse import urlparse, urlencode
     import json
     parsed_url = urlparse(self.uri)
     connection = HTTPConnection(parsed_url.netloc, timeout=self.timeout)
     params = urlencode({
         'key': self.key,
         'format': 'JSON',
         'compact': 'Y',
         'ip': ip,
     })
     try:
         connection.request('GET', '%s?%s' % (parsed_url.path, params))
         response = connection.getresponse()
         if response.status is not 200:
             raise IPNotFound(ip, response.reason)
         response_body = response.read().decode('UTF-8')
     except HTTPException as e:
         raise IPNotFound(ip, e)
     finally:
         connection.close()
     response_obj = json.loads(response_body)
     if 'geolocation_data' in response_obj:
         return response_obj['geolocation_data']
     if 'query_status' in response_obj and 'query_status_description' \
             in response_obj['query_status']:
         cause = response_obj['query_status']['query_status_description']
     else:
         cause = 'API returned a not parsable response.'
     raise IPNotFound(ip, cause)
Exemple #10
0
def request_url(url, method, body=None, headers=None):
    if body is None:
        body = ""
    if headers is None:
        headers = {}

    if "//" not in url:
        url = "https://" + url
    o = urlsplit(url)

    if o.scheme == "https":
        from .system import get_config

        config = get_config()
        key_file = config.get("visstatus", "key_file")
        cert_file = config.get("visstatus", "cert_file")
        ca_file = config.get("visstatus", "ca_file") if config.has_option("visstatus", "ca_file") else None

        from . import httplibssl

        c = httplibssl.HTTPSClientAuthConnection(o.hostname, int(o.port or 443), key_file, cert_file, ca_file)
        # c = HTTPSConnection(o.hostname, int(o.port or 443), key_file, cert_file)
    else:
        c = HTTPConnection(o.hostname, int(o.port or 80))

    c.request(method, o.path + "?" + o.query, body, headers)
    response = c.getresponse()
    data = response.read()
    c.close()

    return response, data
Exemple #11
0
def restCall( host, path, spec ):
    APIG_MOCK = os.getenv('APIG_MOCK', "")
    if APIG_MOCK != "":
        return {"res_code": "00000", "res_message": APIG_MOCK }
        
    hConn=HTTPConnection(host)
    respBody = None
    try:
        data = json.dumps(spec)
        headers = {"Content-type": "application/json"}
        hConn.request('POST', path, data.encode('utf-8'), headers)
        logging.info(f"host: %s, path: %s, body: %s"%(host,path,data))
        resp = hConn.getresponse()
        data=resp.read()
        if data:
            respStr = data.decode('utf-8')
            logging.info(f"Rest api response code: %s, body: %s"%(resp.status, respStr))
            respBody = json.loads(respStr)
        hConn.close()
        if resp.status != 200:
            raise kopf.TemporaryError("Exception when calling rest api, return code: %s\n" % resp.status)
        return respBody
    except Exception as StrError:
        hConn.close()
        logging.warn("Exception when calling restful api: %s\n" % StrError)
        time.sleep(2)
Exemple #12
0
def submitTrack(track):
  if OPTIONS.noop:
    return False
  error = False
  if (track[5] == "L"):
    conn = HTTPConnection("ws.audioscrobbler.com")
    try:
      body = makeQueryBody({'track[0]': track[2], 'timestamp[0]': str(int(track[6])+TIMEDELAY), 'artist[0]': track[0], 'album[0]': track[1], 'trackNumber[0]': track[3], 'duration[0]': track[4], 'sk': SESSION_KEY, 'method': 'track.scrobble'})
      conn.request("POST", "/2.0/", body, {"Content-type": "application/x-www-form-urlencoded"})
      response =  conn.getresponse()
      if (response.status != 200):
        error = "%s error" % response.status
      else:
        try:
            data = ElementTree.fromstring(response.read())
            if (data.attrib['status'] != "ok"):
              error = data.find("error").text
            else:
              data = data.find("scrobbles").find("scrobble").find("ignoredMessage")
              if (data.attrib['code'] != 0):
                error = data.text
        except ElementTree.ParseError as e:
            print ("Warning: {}".format(e))
    finally:
      conn.close()
  return error
Exemple #13
0
    def test_keep_alive_on_connection_with_content_length(self):
        """
        See `test_closes_connection_without_content_length` for details. This
        is a follow up test, which ensure that we do not close the connection
        if not needed, hence allowing us to take advantage of keep-alive.
        """
        conn = HTTPConnection(LiveServerViews.server_thread.host,
                              LiveServerViews.server_thread.port)
        try:
            conn.request('GET',
                         '/example_view/',
                         headers={"Connection": "keep-alive"})
            response = conn.getresponse()
            self.assertFalse(response.will_close)
            self.assertEqual(response.read(), b'example view')
            self.assertEqual(response.status, 200)
            self.assertIsNone(response.getheader('Connection'))

            conn.request('GET',
                         '/example_view/',
                         headers={"Connection": "close"})
            response = conn.getresponse()
            self.assertFalse(response.will_close)
            self.assertEqual(response.read(), b'example view')
            self.assertEqual(response.status, 200)
            self.assertIsNone(response.getheader('Connection'))
        finally:
            conn.close()
Exemple #14
0
class Request(object):
    def __init__(self, method, resource, content_type='text/plain', **kwargs):
        auth = b64encode(CREDENTIALS.encode()).decode()
        self._headers = {
            'Authorization': 'Basic {0}'.format(auth),
            'Content-Type': content_type,
            'Accept': '*/*'
            }
        self._method = method
        self._href = resource
        self._kwargs = kwargs
        self._conn = None

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

    def __exit__(self, *args, **kwargs):
        if self._conn:
            self._conn.close()
        def retrieve_resources(server):
            http_conn = HTTPConnection(server.hostname, server.port)

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

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

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

                create_subresources = resource_body == expected_resources[0]

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

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

                http_conn.close()

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

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

                if not create_subresources:
                    continue

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

        subresource_body["resource_id"] = resource_id

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

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

        response = http_conn.getresponse()

        http_conn.close()

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

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

        self._locations_subresource_delete.insert(0, location)

        subresource_id = int(location.split("/")[-1])
        subresource_body["id"] = subresource_id
        def request_options(server):
            http_conn = HTTPConnection(server.hostname, server.port)

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

            http_conn.close()
Exemple #19
0
def _save_failed_test(data, expect, filename):
    from ..io import _make_png
    commit, error = run_subprocess(['git', 'rev-parse', 'HEAD'])
    name = filename.split('/')
    name.insert(-1, commit.strip())
    filename = '/'.join(name)
    host = 'data.vispy.org'

    # concatenate data, expect, and diff into a single image
    ds = data.shape
    es = expect.shape

    shape = (max(ds[0], es[0]) + 4, ds[1] + es[1] + 8 + max(ds[1], es[1]), 4)
    img = np.empty(shape, dtype=np.ubyte)
    img[..., :3] = 100
    img[..., 3] = 255

    img[2:2 + ds[0], 2:2 + ds[1], :ds[2]] = data
    img[2:2 + es[0], ds[1] + 4:ds[1] + 4 + es[1], :es[2]] = expect

    diff = make_diff_image(data, expect)
    img[2:2 + diff.shape[0], -diff.shape[1] - 2:-2] = diff

    png = _make_png(img)
    conn = HTTPConnection(host)
    req = urlencode({'name': filename, 'data': base64.b64encode(png)})
    conn.request('POST', '/upload.py', req)
    response = conn.getresponse().read()
    conn.close()
    print("\nImage comparison failed. Test result: %s %s   Expected result: "
          "%s %s" % (data.shape, data.dtype, expect.shape, expect.dtype))
    print("Uploaded to: \nhttp://%s/data/%s" % (host, filename))
    if not response.startswith(b'OK'):
        print("WARNING: Error uploading data to %s" % host)
        print(response)
Exemple #20
0
def uninstall_vl(vl):
    """
    Function description
    Parameters
    ----------
    param1: type
        param1 description
    Returns
    -------
    name: type
        return description
    """
    # ask mtp to deploy vl
    # read mtp properties
    config = RawConfigParser()
    config.read("../../mtp.properties")
    mtp_ip = config.get("MTP", "mtp.ip")
    mtp_port = config.get("MTP", "mtp.port")
    mtp_path = config.get("MTP", "mtp.path")
    mtp_uri = "http://" + mtp_ip + ":" + mtp_port + mtp_path + "/resources"
    # connect to MTP aznd make the request
    header = {'Content-Type': 'application/json', 'Accept': 'application/json'}
    body = {"interNfviPopConnnectivityId": vl, "metaData": []}
    try:
        conn = HTTPConnection(mtp_ip, mtp_port)
        conn.request("DELETE", mtp_uri, dumps(body), header)
        rsp = conn.getresponse()
        deployed_vl_info = rsp.read()
        conn.close()
    except ConnectionRefusedError:
        # the MTP server is not running or the connection configuration is wrong
        log_queue.put([
            "ERROR",
            "the MTP server is not running or the connection configuration is wrong"
        ])
 def translate(q):
     if not q:
         return None
     appid = "20180530000169197"
     secret_key = "wbwZhZfzgFwlh1EqVNaN"
     http_client = None
     url = "/api/trans/vip/translate"
     from_lang = "auto"
     to_lang = "zh"
     salt = random.randint(32768, 65536)
     sign = appid + q + str(salt) + secret_key
     m1 = hashlib.md5()
     m1.update(sign.encode())
     sign = m1.hexdigest()
     url = url + "?appid=" + appid + "&q=" + urllib.parse.quote(
         q) + "&from=" + from_lang + "&to=" + to_lang + "&salt=" + str(
             salt) + "&sign=" + sign
     try:
         http_client = HTTPConnection("api.fanyi.baidu.com")
         http_client.request("GET", url)
         response = json.loads(http_client.getresponse().read())
         if "error_code" in response:
             return None
         return response["trans_result"][0]["dst"]
     except Exception as e:
         print(e)
         return None
     finally:
         if http_client:
             http_client.close()
Exemple #22
0
def check_url_path(path, redirected=0):
    if redirected > MAX_REDIRECTION_ALLOWED:
        return False
    try:
        parse_result = urllib.parse.urlparse(path)
        server_name = parse_result.netloc
        urlpath = parse_result.path
        if not urlpath:
            # Just a server, as with a repo.
            with contextlib.closing(urllib.request.urlopen(path)) as res:
                code = res.getcode()
        else:
            # socket.gaierror could be raised,
            #   which is a child class of IOError
            conn = HTTPConnection(server_name, timeout=15)
            # Don't try to get the whole file:
            conn.request('HEAD', path)
            response = conn.getresponse()
            code = response.status
            conn.close()
        if code == 200:
            return True
        elif code == 301 or code == 302:
            for header in response.getheaders():
                if header[0] == 'location':
                    return check_url_path(header[1], redirected + 1)
        else:
            return False
    except (urllib.error.URLError, HTTPException, IOError, ValueError):
        return False
    return True
Exemple #23
0
class Client:
    #構構子
    def __init__(self):
        self.conn = None

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

    #關閉連線
    def closeConnection(self):
        self.conn.close()
Exemple #24
0
class HTTPGetter:
    def __init__(self, baseUrl, maxPending=10):
        self.baseUrl = baseUrl
        self.parsedBaseUrl = urlparse(baseUrl)
        self.maxPending = maxPending
        self.requests = []
        self.pendingRequests = []
        self.httpConnection = HTTPConnection(self.parsedBaseUrl.netloc)
        self.httpRequestHeaders = headers = {'Host':self.parsedBaseUrl.netloc,'Content-Length':0,'Connection':'Keep-Alive','User-Agent':'FlightGear terrasync.py'}

    def doGet(self, httpGetCallback):
        conn = self.httpConnection
        request = httpGetCallback
        self.httpConnection.request("GET", self.parsedBaseUrl.path + request.src, None, self.httpRequestHeaders)
        httpGetCallback.result = self.httpConnection.getresponse()
        httpGetCallback.callback()

    def get(self, httpGetCallback):

        try:
            self.doGet(httpGetCallback)
        except HTTPException:
            # try to reconnect once
            #print("reconnect")
            self.httpConnection.close()
            self.httpConnection.connect()
            self.doGet(httpGetCallback)
Exemple #25
0
class YQLQuery(object):
  RETRY_MAX = 3
  DELAY_TIME = timedelta(seconds = 3)

  def __init__(self):
    self.connection = HTTPConnection('query.yahooapis.com')
    self.retry_count = 0
    self.cool_down = None

  def execute(self, yql, token = None):
    if self.cool_down is not None:
        delta = datetime.now() - self.cool_down
        if delta < YQLQuery.DELAY_TIME:
            logging.debug("waiting for next request")
            time.sleep(YQLQuery.DELAY_TIME - delta)
    self.cool_down = datetime.now()
    logging.debug("sending request, %s" % yql)
    self.connection.request('GET', PUBLIC_API_URL + '?' + urlencode({ 'q': yql, 'format': 'json', 'env': DATATABLES_URL }))
    resp_content = self.connection.getresponse().read()
    try:
        return simplejson.loads(resp_content)
    except simplejson.JSONDecodeError as ex:
        logging.debug(ex)
        if self.retry_count < YQLQuery.RETRY_MAX:
            logging.debug("Retrying...")
            time.sleep(1)        
            self.retry_count += 1
            return self.execute(yql, token)
        else:
            logging.debug("Max retry_count is reached")
            return []

  def __del__(self):
    self.connection.close()
    def test_000_stats(self):
        client = HTTPConnection("127.0.0.1:%s" % self.http_listener11_port,
                                timeout=TIMEOUT)
        self._do_request(client, self.TESTS_11["GET"])
        self._do_request(client, self.TESTS_11["POST"])
        client.close()
        qd_manager = QdManager(self, address=self.INT_A.listener)
        stats = qd_manager.query('org.apache.qpid.dispatch.httpRequestInfo')
        self.assertEqual(len(stats), 2)
        for s in stats:
            self.assertEqual(s.get('requests'), 10)
            self.assertEqual(s.get('details').get('GET:400'), 1)
            self.assertEqual(s.get('details').get('GET:200'), 6)
            self.assertEqual(s.get('details').get('GET:204'), 1)
            self.assertEqual(s.get('details').get('POST:200'), 2)

        def assert_approximately_equal(a, b):
            self.assertTrue((abs(a - b) / a) < 0.1)

        if stats[0].get('direction') == 'out':
            self.assertEqual(stats[1].get('direction'), 'in')
            assert_approximately_equal(stats[0].get('bytesOut'), 1059)
            assert_approximately_equal(stats[0].get('bytesIn'), 8849)
            assert_approximately_equal(stats[1].get('bytesOut'), 8830)
            assert_approximately_equal(stats[1].get('bytesIn'), 1059)
        else:
            self.assertEqual(stats[0].get('direction'), 'in')
            self.assertEqual(stats[1].get('direction'), 'out')
            assert_approximately_equal(stats[0].get('bytesOut'), 8849)
            assert_approximately_equal(stats[0].get('bytesIn'), 1059)
            assert_approximately_equal(stats[1].get('bytesOut'), 1059)
            assert_approximately_equal(stats[1].get('bytesIn'), 8830)
Exemple #27
0
    def single(self, item):
        """ Downloads a file through HTTP protocol."""
        if logger.isEnabledFor(logging.INFO):
            logger.info("Downloading {}...".format(item.resource_uri))

        conn = HTTPConnection(self.host, self.port, timeout=30)
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("HTTPConnection created ({}:{})".format(self.host,
                                                                 self.port))
        conn.request("GET", item.resource_uri)
        r = conn.getresponse()
        item.expected_size = int(r.getheader("Content-Length").split(",")[0])
        try:
            with open(item.dst, 'wb') as f:
                while item.downloaded_bytes < item.expected_size:
                    buf = r.read(BUFFER_SIZE)
                    f.write(buf)
                    item.downloaded_bytes += len(buf)
                    if item.downloaded_bytes % (BUFFER_SIZE * 25) == 0 \
                            and logger.isEnabledFor(logging.DEBUG):
                        msg = "Downloaded: {}/{}"
                        logger.debug(msg.format(item.downloaded_bytes,
                                                item.expected_size))
        finally:
            conn.close()
        if logger.isEnabledFor(logging.INFO):
            logger.info("Downloaded {}.".format(item.resource_uri))
Exemple #28
0
def check_url_path(path, redirected=0):
    if redirected > MAX_REDIRECTION_ALLOWED:
        return False
    try:
        parse_result = urllib.parse.urlparse(path)
        server_name = parse_result.netloc
        urlpath = parse_result.path
        if not urlpath:
            # Just a server, as with a repo.
            with contextlib.closing(urllib.request.urlopen(path)) as res:
                code = res.getcode()
        else:
            # socket.gaierror could be raised,
            #   which is a child class of IOError
            conn = HTTPConnection(server_name, timeout=15)
            # Don't try to get the whole file:
            conn.request('HEAD', path)
            response = conn.getresponse()
            code = response.status
            conn.close()
        if code == 200:
            return True
        elif code == 301 or code == 302:
            for header in response.getheaders():
                if header[0] == 'location':
                    return check_url_path(header[1], redirected + 1)
        else:
            return False
    except (urllib.error.URLError, HTTPException, IOError, ValueError):
        return False
    return True
Exemple #29
0
    def test_closes_connection_without_content_length(self):
        """
        A HTTP 1.1 server is supposed to support keep-alive. Since our
        development server is rather simple we support it only in cases where
        we can detect a content length from the response. This should be doable
        for all simple views and streaming responses where an iterable with
        length of one is passed. The latter follows as result of `set_content_length`
        from https://github.com/python/cpython/blob/master/Lib/wsgiref/handlers.py.

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

            conn.request('GET', '/streaming_example_view/', headers={'Connection': 'close'})
            response = conn.getresponse()
            self.assertTrue(response.will_close)
            self.assertEqual(response.read(), b'Iamastream')
            self.assertEqual(response.status, 200)
            self.assertEqual(response.getheader('Connection'), 'close')
        finally:
            conn.close()
Exemple #30
0
def geoencode(city, country):
	msg = {
		'q': city+', '+country,
		'output': 'json',
		'oe': 'utf8',
		'sensor': 'true_or_false',
		'key': 'ABQIAAAAzT63NsNCcpw5Af6mLso2FxSgcUpOcSOIawgl-Zf9E7s32CuX-RQF5sRXdcMDlQa3cpL8L_S63UUpFA',
	}

	conn = HTTPConnection('maps.google.com')
	conn.request('GET', '/maps/geo?'+urlencode(msg))
	res = conn.getresponse()
	data = res.read()
	
	conn.close()
	data = json.loads(data.decode("utf-8"))
	
	lat, lng = 0, 0
	loc_name = city
	if data and 'Status' in data and 'code' in data['Status'] and data['Status']['code'] == 200 and 'Placemark' in data:		
		for p in data['Placemark']:
			if 'AddressDetails' in p and 'Country' in p['AddressDetails'] and 'CountryName' in p['AddressDetails']['Country'] \
			and p['AddressDetails']['Country']['CountryName'].lower() == country and 'Point' in p and 'coordinates' in p['Point']:
				lng = p['Point']['coordinates'][0]
				lat = p['Point']['coordinates'][1]
				break
	return lat, lng,loc_name
Exemple #31
0
def translate(q,fromLang = 'en'):
    httpClient = None
    myurl = '/api/trans/vip/translate'
    sign = appid+q+str(salt)+secretKey
    m1 = md5()
    m1.update(str(sign).encode(encoding, errors))
    sign = m1.hexdigest()
    myurl = myurl+'?appid='+appid+'&q='+quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign

    count=0
    while count<4:
        try:
            httpClient = HTTPConnection('api.fanyi.baidu.com')
            httpClient.request('GET', myurl)
            response = httpClient.getresponse()
            string = response.read().decode('utf-8')
            if string:
                if httpClient:
                    httpClient.close()
                json_obj = json.loads(string)
                time.sleep(0.4)
                return json_obj['trans_result'][0]['dst']
        except:
            time.sleep(0.1+random.random()*0.2)
            continue
        finally:
            if httpClient:
                httpClient.close()
    return None
Exemple #32
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)
Exemple #33
0
def upload_line():
    file = request.files['config_file'] if 'config_file' in request.files else None
    if file:
        filename = secure_filename(file.filename)
        upload_api = os.path.join(current_app.config['DAC_API_CONFIGS_LINES_URL'], request.form['line_no'])
        # print(upload_url)
        headers = {"Content-type": "application/x-www-form-urlencoded",
                   "Accept": "text/plain"}
        data = parse.urlencode({'file': file.read().decode('utf-8')})

        try:
            conn = HTTPConnection(current_app.config['DAC_HOST_URL'])
            conn.request("POST", upload_api, data, headers)
            resp = conn.getresponse()
            resp_data = json.loads(resp.read().decode('utf-8'))
            print(resp_data)
        except HTTPException as e:
            print("POST CONFIG FILE ERROR", e)
            resp_data = {}
        finally:
            conn.close()
        flash(resp_data)
    else:
        message = "Please select a file for upload."
        flash(message)
    return redirect(url_for('config.index'))
Exemple #34
0
def delete_alert(alert_id):
    """
    Parameters
    ----------
    operationId: dict
        Object for creating
    Returns
    -------
    name: type
        return alert
    """
    header = {'Accept': 'application/json'}
    monitoring_uri = "http://" + monitoring_platform_ip + ":" + monitoring_platform_port + monitoring_platform_base_path + "/alert/" + str(
        alert_id)
    try:
        conn = HTTPConnection(monitoring_platform_ip, monitoring_platform_port)
        conn.request("DELETE", monitoring_uri, headers=header)
        rsp = conn.getresponse()
        resources = rsp.read()
        resp_alert = resources.decode("utf-8")
        log_queue.put(["DEBUG", "Alert from Config Manager are:"])
        log_queue.put(["DEBUG", resp_alert])
        resp_alert = json.loads(resp_alert)
        log_queue.put(["DEBUG", "Alert from Config Manager are:"])
        log_queue.put(["DEBUG", json.dumps(resp_alert, indent=4)])
        conn.close()
    except ConnectionRefusedError:
        # the Config Manager is not running or the connection configuration is wrong
        log_queue.put([
            "ERROR",
            "the Config Manager is not running or the connection configuration is wrong"
        ])
Exemple #35
0
def get_alerts():
    """
    Parameters
    ----------
    Returns
    -------
    name: type
        return alerts
    """
    header = {'Accept': 'application/json'}
    monitoring_uri = "http://" + monitoring_platform_ip + ":" + monitoring_platform_port + monitoring_platform_base_path + "/alert"
    try:
        conn = HTTPConnection(monitoring_platform_ip, monitoring_platform_port)
        conn.request("GET", monitoring_uri, None, header)
        rsp = conn.getresponse()
        resources = rsp.read()
        alerts = resources.decode("utf-8")
        log_queue.put(["DEBUG", "Alerts from Config Manager are:"])
        log_queue.put(["DEBUG", alerts])
        alerts = json.loads(alerts)
        log_queue.put(["DEBUG", "Alerts from Config Manager are:"])
        log_queue.put(["DEBUG", json.dumps(alerts, indent=4)])
        conn.close()
    except ConnectionRefusedError:
        # the Config Manager is not running or the connection configuration is wrong
        log_queue.put([
            "ERROR",
            "the Config Manager is not running or the connection configuration is wrong"
        ])
    return alerts
Exemple #36
0
    def after_start_check(self) -> bool:
        """Check if defined URL returns expected status to a check request."""
        conn = HTTPConnection(self.host, self.port)
        try:
            body = urlencode(self.payload) if self.payload else None
            headers = self.headers if self.headers else {}
            conn.request(
                self.method,
                self.url.path,
                body,
                headers,
            )
            try:
                status = str(conn.getresponse().status)
            finally:
                conn.close()

            if status == self.status or self.status_re.match(status):
                return True
            return False

        except (HTTPException, socket.timeout, socket.error) as ex:
            LOG.debug(
                "Encounter %s while trying to check if service has started.", ex
            )
            return False
Exemple #37
0
    def retrieve_cats():
        print('Pre Retrieve Page')
        conn = HTTPConnection(settings.SCRAP_HOST)
        headers = settings.SCRAP_HEADERS.copy()
        headers.update({'Cookie': settings.SCRAP_COOKIE})
        conn.request("GET", "/area_soci_mip.asp", None, headers)
        response = conn.getresponse()
        data = response.read()
        conn.close()

        print('Page retrieved, analyzing')

        res = []
        soup = BeautifulSoup(data, 'html.parser')
        rows = soup.find('select', {'name': 'codmar'}).find_all('option')
        print('Processing %s entry' % len(rows))

        if len(rows) == 0:
            raise ValueError('no valid records on page')

        for r in rows:
            codice = r['value'].strip()
            if codice.isdigit():
                nome = r.text.strip()
                o = type('Dummy', (object,), {'codice': codice, 'nome': nome})
                res.append(o)
        else:
            print('a')

        return res
Exemple #38
0
def make_request_to_so_nbi(ns_id, scale_request):

    header = {'Content-Type': 'application/json', 'Accept': 'application/json'}

    scale_uri = "http://" + so_ip + ":" + so_port + so_scale_ns_base_path + "/" + ns_id + "/scale"

    try:
        conn = HTTPConnection(so_ip, so_port, timeout=10)
        conn.request("PUT",
                     scale_uri,
                     body=json.dumps(scale_request),
                     headers=header)
        rsp = conn.getresponse()
        resources = rsp.read()
        resp_scale = resources.decode("utf-8")
        log_queue.put(["DEBUG", "Request from SO on Scale are:"])
        log_queue.put(["DEBUG", scale_request])
        log_queue.put(["DEBUG", "Response from SO on Scale request are:"])
        log_queue.put(["DEBUG", resp_scale])
        resp_scale = json.loads(resp_scale)
        log_queue.put(["DEBUG", "Response from SO on Scale request are:"])
        log_queue.put(["DEBUG", json.dumps(resp_scale, indent=4)])
        conn.close()
    except ConnectionRefusedError:
        # the SO on Scale request returned wrong response
        log_queue.put(["ERROR", "SO on Scale request returned wrong response"])
Exemple #39
0
def run(url,params,token):
    httpClient = None
    data=None
    try:
        httpClient = HTTPConnection('192.168.106.92', 8880)
        if token=='':
            headers = {'content-type': 'application/json', 'Accept-Language': 'zh-CN'}  # post请求头需要有content-type
        else:
            headers = {'content-type': 'application/json;', 'Accept-Language': 'zh-CN','token':'%s'%(token)}  # 加入token



        json_str = json.dumps(params)

        httpClient.request('POST', url, json_str, headers)  # 这里可以不带key,直接用value
        response = httpClient.getresponse()
        #print(response.getheaders())
        data=json.loads(response.read().decode(encoding='utf-8'))# 将获取到的内容转换为json类型数据
       # data = json.loads(response, encoding='utf-8')
        #f=json.loads(response.read())# 将获取到的内容转换为json类型数据
       # print(f['code'])
        #data = response.getcode()

    except Exception:
        print("error")
    finally:
        if httpClient:
            httpClient.close()
        return data
Exemple #40
0
    def test_closes_connection_without_content_length(self):
        """
        A HTTP 1.1 server is supposed to support keep-alive. Since our
        development server is rather simple we support it only in cases where
        we can detect a content length from the response. This should be doable
        for all simple views and streaming responses where an iterable with
        length of one is passed. The latter follows as result of `set_content_length`
        from https://github.com/python/cpython/blob/master/Lib/wsgiref/handlers.py.

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

            conn.request('GET', '/streaming_example_view/', headers={'Connection': 'close'})
            response = conn.getresponse()
            self.assertTrue(response.will_close)
            self.assertEqual(response.read(), b'Iamastream')
            self.assertEqual(response.status, 200)
            self.assertEqual(response.getheader('Connection'), 'close')
        finally:
            conn.close()
Exemple #41
0
def do_task(id):
    global num

    hc = HTTPConnection('127.0.0.1:5000', timeout=3600)

    hc.request('GET', '/lock')

    resp = hc.getresponse()
    key = resp.read().decode()

    hc.close()

    print('--start task: {}--'.format(id))

    num += 1

    print(num)
    time.sleep(0.5)

    print('--  end task: {}--'.format(id))

    hc = HTTPConnection('127.0.0.1:5000')

    hc.request('PUT', '/lock?{}'.format(key))
    hc.close()
Exemple #42
0
class HTTPAdapter(BaseAdapter):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self._conn = None
        self._connected = False

    @property
    def connected(self):
        return self._connected

    @property
    def conn(self):
        return self._conn

    def connect(self):
        try:
            self._conn = HTTPConnection(self.host, self.port, timeout=10)
            self._connected = True
        except HTTPException:
            raise ConnectError(f'Could not connect to a {self.host}.')
        return self._conn

    def disconnect(self):
        try:
            self._conn.close()
            self._connected = False
        except HTTPException:
            raise ConnectError(f'Could not disconnect from a {self.host}.')
Exemple #43
0
 def test_closes_connection_without_content_length(self):
     """
     The server doesn't support keep-alive because Python's http.server
     module that it uses hangs if a Content-Length header isn't set (for
     example, if CommonMiddleware isn't enabled or if the response is a
     StreamingHttpResponse) (#28440 / https://bugs.python.org/issue31076).
     """
     conn = HTTPConnection(LiveServerViews.server_thread.host,
                           LiveServerViews.server_thread.port,
                           timeout=1)
     try:
         conn.request('GET',
                      '/example_view/',
                      headers={'Connection': 'keep-alive'})
         response = conn.getresponse().read()
         conn.request('GET',
                      '/example_view/',
                      headers={'Connection': 'close'})
         # macOS may give ConnectionResetError.
         with self.assertRaises((RemoteDisconnected, ConnectionResetError)):
             try:
                 conn.getresponse()
             except ConnectionAbortedError:
                 if sys.platform == 'win32':
                     self.skipTest(
                         'Ignore nondeterministic failure on Windows.')
     finally:
         conn.close()
     self.assertEqual(response, b'example view')
Exemple #44
0
def main():
    url = 'www.google.co.kr'
    conn = HTTPConnection(url)
    conn.request('GET', '/')

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

    data = r.read().decode(encoding='euc-kr')
    conn.close()

    print('\n>>>>>>> Fetch Image From ', url)
    parser = ImageParser()
    parser.feed(data)
    dataset = set(x for x in parser.result)

    #print('\n'.join(sorted(dataset)))




    for d in dataset:
        url = 'www.google.co.kr'
        conn = HTTPConnection(url)
        conn.request('GET', '/%s'%d)
        r = conn.getresponse()
        filename = d.replace('/','_')
        print(filename)
        with open(filename, 'wb') as outfile:  # 자동 클로즈시킴
            outfile.write(r.read())
            outfile.close()
    def run(self):
        attempts_counter = 0
        done = False

        try:
            url = urlparse(self.subscription.redfish['Destination'])
            json_str = self.event.serialize()

            while (attempts_counter < self.retry_attempts and not done):
                attempts_counter += 1

                try:
                    connection = HTTPConnection(url.hostname, port=url.port)
                    connection.request(
                        'POST', url.path, json_str, self.RESPONSE_HEADER)

                    done = True
                except Exception as e:
                    logging.exception(
                        'Could not dispatch event to {}. '
                        'Error: {}'.format(url.netloc, e))

                    time.sleep(self.retry_interval)
                finally:
                    connection.close()
        except Exception as e:
            logging.exception(
                'Error getting event and/or subscriber information: {}'
                .format(e))
Exemple #46
0
def register_ok(request):
    code = request.GET.get('code')
    if code:
        client_id = settings.OK_APP_ID
        client_secret = settings.OK_API_SECRET
        con = HTTPConnection('api.ok.ru')
        url = "/oauth/token.do?code={code}&client_id={client_id}&client_secret={client_secret}&redirect_uri=http://127.0.0.1:8000/register_activate/register_ok/?&grant_type=authorization_code".format(
            client_id=client_id, client_secret=client_secret, code=code)
        con.request('POST', url)
        result = con.getresponse()
        res = json.loads(result.read().decode('cp1251'))
        print(res)
        access_token = res['access_token']
        refresh_token = res['refresh_token']
        expires_in = res['expires_in']

        application_key = 'CBACIQHLEBABABABA'
        url = "/fb.do?method=friends.get&access_token={access_token}".format(
            application_key=application_key, access_token=access_token)
        con.request('POST', url)
        result = con.getresponse()
        res = json.loads(result.read().decode('cp1251'))
        print(res)

        print(type(res), access_token)
        con.close()
        return render(request, 'register_ok.html', {'code': code})
Exemple #47
0
class ShareHTTP(SharedStream):
    """Shared HTTP stream allows multiple objects to make requests on a single
    TCP connection. When the stream is closed, it will be reopened by the
    first thread to try.

    This object does not perform redirects.
    """

    scheme = "http"
    connection = None

    def _connect(self):
        """Establish HTTP Connection"""
        self.connection = HTTPConnection(*self.sig[1:], timeout=30)

    def request(self, msg):
        """Make an announce request to an HTTP tracker

        Argument should be the contents of a GET request, e.g. if the URL
        to be sent is <http://tracker.com:3669/announce?infohash=...>, then

        response, rawdata = stream.request('/announce?infohash=...')

        This function requests compressed responses and performs any
        decompression necessary.

        A tuple containing the HTTPResponse as its first value is returned;
        the second value depends on the response status code. For a 200
        status, a (uncompressed) bytestring is returned. For a (301, 302)
        status, the new location is returned as a string. On other errors,
        the raw response bytestring is returned, which may or may not
        contain a bencoded dictionary with an error message from the
        tracker.

        On errors, the connection is closed. It may be reopened, but this
        function makes no attempt to run multiple times.
        """
        with self.lock:
            if self.connection is None:
                self._connect()

            try:
                self.connection.request("GET", msg, headers={"User-Agent": VERSION, "Accept-Encoding": "gzip"})
                response = self.connection.getresponse()
                status = response.status
                if status == 200:
                    data = response.read()
                    if response.getheader("Content-Encoding", "").find("gzip") >= 0:
                        data = gzip.GzipFile(fileobj=io.BytesIO(data)).read()
                    return response, data
                elif status in (301, 302):
                    return response, response.getheader("Location")

                self.connection.close()
                self.connection = None
                return response, response.read()
            except (IOError, HTTPException):
                self.connection = None
                raise
Exemple #48
0
def http_request(host, port, method, path, headers=None):
    conn = HTTPConnection(host=host, port=port)
    try:
        conn.request(method, path, headers=headers or {})
        resp = conn.getresponse()
    finally:
        conn.close()
    return resp.status
Exemple #49
0
def get_external_ip():
    connection = HTTPConnection("ifconfig.me")
    connection.request("GET", "/ip")
    response = connection.getresponse()
    ip = response.read().decode("utf-8").strip()
    response.close()
    connection.close()
    return ip
Exemple #50
0
    def batch(self, calls):

        batch_data = []
        for call in calls:
            m = call.pop(0)
            batch_data.append({"jsonrpc": "2.0", "method": m, "params": call, "id": self.next_id})
            self.next_id += 1

        def encode_decimal(o):
            if isinstance(o, decimal.Decimal):
                return float(round(o, self.precision))
            raise TypeError(repr(o) + " is not JSON serializable")

        postdata = json.dumps(batch_data, default=encode_decimal)

        conn = HTTPConnection(self.host, self.port)

        try:

            conn.request(
                "POST",
                "",
                postdata,
                {
                    "Host": self.host,
                    "User-Agent": USER_AGENT,
                    "Authorization": self.auth,
                    "Content-Type": "application/json",
                },
            )

            response = conn.getresponse()

            if response is None:
                raise JSONRPCException({"code": -342, "message": "missing HTTP response from server"})

            if response.status is not 200:
                raise JSONRPCException({"code": -344, "message": str(response.status) + " " + response.reason})

            try:
                responses = json.loads(response.read().decode())
            except ValueError as e:
                raise JSONRPCException(str(e))

        finally:
            conn.close()

        results = []

        for response in responses:
            if response["error"] is not None:
                raise JSONRPCException(response["error"])
            elif "result" not in response:
                raise JSONRPCException({"code": -343, "message": "missing JSON-RPC result"})
            else:
                results.append(response["result"])

        return results
        def retrieve_ui(server):
            http_conn = HTTPConnection(server.hostname, server.port)
            http_conn.connect()

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

            self._response = http_conn.getresponse()

            http_conn.close()
 def rest_POST(self, content):
     params = urllib.parse.urlencode({'licenseID':self.api_key, 'content':content.encode('ascii', 'xmlcharrefreplace'), 'paramsXML':self._get_params_XML()})
     headers = {"Content-type":"application/x-www-form-urlencoded"}
     conn = HTTPConnection("api.opencalais.com:80")
     conn.request("POST", "/enlighten/rest/", params, headers)
     response = conn.getresponse()
     data = response.read()
     conn.close()
     return (data)
Exemple #53
0
class Client:

    host = None
    port = None

    connection = None

    M_GET  = 'GET'
    M_POST = 'POST'

    def __init__(self, host='127.0.0.1', port=8080, gpg_dir=None):

        self.host = host
        self.port = port
        self.gpg_dir = gpg_dir

        if not self.gpg_dir:
            self.gpg_dir = os.path.join(os.getenv('HOME'), '.hypernova', 'gpg')

        self._init_connection()
        self._init_gpg()

    def _init_connection(self):

        self._connection = HTTPConnection(self.host, self.port)

    def _init_gpg(self):

        self._gpg = GPG.get_gpg(gnupghome=self.gpg_dir)

    def query(self, params, client_fp, server_fp):

        if not isinstance(params, str):
            params = json.dumps(params)

        params = self._gpg.encrypt(params, server_fp, sign=client_fp)
        encrypted_params = str(params)

        if not params:
            raise ValueError('Invalid passphrase, or the server\'s key has ' \
                             'not been signed')

        http_headers = {'Content-Length': len(encrypted_params)}
        self._connection.request(self.M_GET, '/', body=encrypted_params,
                                 headers=http_headers)

        response = self._connection.getresponse()
        response_data = str(response.read(), 'UTF-8')

        response_data = self._gpg.decrypt(response_data)
        if response_data.fingerprint != server_fp:
            raise ValueError('Response was not signed')

        self._connection.close()

        return json.loads(str(response_data))
Exemple #54
0
 def test_yuzuql(self):
     conn = HTTPConnection(self.address, self.port)
     conn.request("GET",
                  "/sparql/?query=SELECT+%3Fs+WHERE+{%0D%0A++%3Fs+"
                  "rdfs%3Alabel+\"Beispiel\"%40de%0D%0A}+LIMIT+1",
                  headers={'Accept': ''})
     content = str(conn.getresponse().read())
     self.assertIn("\"value\": \"http://localhost:8080/data/example\"",
                   content)
     conn.close()
Exemple #55
0
 def stop(self):
     """
     Stop the running service: We can only do this by making a
     request to a special endpoint :P
     """
     if self._running:
         conn = HTTPConnection('localhost', self._port)
         conn.request('POST', self._shutdown_route)
         conn.close()
         self._running = False
Exemple #56
0
def fetch_single(baseurl,baseurl2,version,path,fetchdir,retrycount):
    parsedurl = urlparse(baseurl)
    parsedurl2 = urlparse(baseurl2)
    conn = HTTPConnection(parsedurl.hostname,port=parsedurl.port)
    conn2 = HTTPConnection(parsedurl2.hostname,port=parsedurl2.port)

    res = fetch(conn,conn2,retrycount,baseurl,fetchdir,version,path)
    conn.close()
    conn2.close()
    return res