예제 #1
0
    def _run_request(self,
                     method,
                     url,
                     body=None,
                     progress=0,
                     content_type="text/xml"):
        url = urlparse.urljoin(self.url, url)
        purl = urlparse.urlparse(url)
        if purl.scheme != "https":
            raise ValueError("Unsupported url scheme: %r" % (purl.scheme, ))
        if ":" in purl.netloc:
            host, port = purl.netloc.split(":", 1)
            port = int(port)
        else:
            host = purl.netloc
            port = None
        url = purl.path
        if purl.query:
            url += "?" + query
        headers = {}
        if body:
            headers["Content-Type"] = content_type

        try_no_auth = 0

        if not try_no_auth and not self.username:
            raise HTTPError(0, "Need a username")

        try:
            self.msg("connecting")
            conn = httplib.HTTPSConnection(host, port)
            #            conn.set_debuglevel(10)

            if try_no_auth:
                self.request(conn, method, url, body, headers, progress)
                self.msg("waiting for status")
                response = conn.getresponse()

            if not try_no_auth or (response.status == httplib.UNAUTHORIZED
                                   and self.username):
                if try_no_auth:
                    conn.close()
                    self.msg("re-connecting")
                    conn = httplib.HTTPSConnection(host, port)


#                    conn.set_debuglevel(10)

                creds = self.username + ":" + self.password
                headers["Authorization"] = "Basic " + \
                        base64.b64encode(bytes(creds, "utf8")).decode("utf8")
                # ^ Seems to be broken in python3 (even the raw
                # documentation examples don't run for base64)
                self.request(conn, method, url, body, headers, progress)
                self.msg("waiting for status")
                response = conn.getresponse()

            if response.status == httplib.OK:
                self.msg("reading response")
                sys.stderr.flush()
                response_body = response.read()
            else:
                err = response.read()
                raise HTTPError(
                    response.status,
                    "%03i: %s (%s)" % (response.status, response.reason, err),
                    err)
        finally:
            conn.close()
        return response_body
예제 #2
0
 def test_attributes(self):
     # simple test to check it's storing the timeout
     h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30)
     self.assertEqual(h.timeout, 30)
예제 #3
0
def validate_domains(dom_str_list):
    """ Validate a domain string, returns a tuple of (invalid_domain_list, valid_domain_list) or (None, None) if Google APIs are down """
    invalid_domain_list = []
    almost_valid_domain_list = []
    # check domains agaist simple stuff
    for dom_str in dom_str_list:
        # domains can be no longer than 256 characters in length
        # really nice because this prevents a bunch of regex ddos attacks
        # also check against regex
        if len(dom_str) > 256 or len(dom_str) < 4 or DOM_VAL_PAT.match(dom_str) is None:
            invalid_domain_list.append(dom_str)
        # else it's almost a valid domain
        else:
            almost_valid_domain_list.append(dom_str)
    # we still can't trust the internet though, so we should
    # check the domain against Google's safebrowsing API
    con = None
    data = None
    try:
        con = client.HTTPSConnection(SAFE_BROW_DOM, context=ssl.create_default_context())
        con.request("POST", SAFE_BROW_PATH, body=create_safe_brow_from_domains(almost_valid_domain_list), headers=SAFE_BROW_HEADERS)
        # get the response
        res = con.getresponse()
        if res.status != 200:
            print("Returned status code: " + str(res.status))
            return (None, None)
        # data!
        databytes = res.read()
        print(databytes)
        data = json.loads(databytes)
    except Exception:
        # if there's an error, return error
        print("Error!")
        print(sys.exc_info()[0])
        print(sys.exc_info()[1])
        print(sys.exc_info()[2])
        return (None, None)
    finally:
        # make sure to close the connection!
        if con is not None:
            con.close()
    # if there are no matches in the data (there probably aren't?), return all the domains!
    if "matches" not in data:
        return (invalid_domain_list, almost_valid_domain_list)
    # else we got a fat uh oh
    matches = data["matches"]
    valid_domain_list = []
    # sort domains by threat and not threat
    for d in almost_valid_domain_list:
        # by checking if the domain is a substring of the url
        matched = False
        for m in matches:
            if d in m["threat"]["url"]:
                invalid_domain_list.append(d)
                matched = True
                break
        # if it's not, add to valid list!
        if not matched:
            valid_domain_list.append(d)
    # now I'm ready
    return (invalid_domain_list, valid_domain_list)
예제 #4
0
 def _https_connection(self, netloc, reconnect=False):
     if netloc not in self.https or reconnect:
         self.https[netloc] = httplib.HTTPSConnection(
             netloc, timeout=http_timeouts.get(netloc))
     return self.https[netloc]
예제 #5
0
    def action_wechat(self, program, action_type, msg, check_status):
        """
        微信通知
        :param program:
        :param action_type:
        :param msg:
        :param check_status:
        :return:
        """
        host = "qyapi.weixin.qq.com"

        corpid = self.wechat_config.get('corpid')
        secret = self.wechat_config.get('secret')
        agentid = self.wechat_config.get('agentid')
        touser = self.wechat_config.get('touser')
        toparty = self.wechat_config.get('toparty')
        totag = self.wechat_config.get('totag')

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

        access_token_url = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format(
            id=corpid, crt=secret)
        try:
            httpClient = httplib.HTTPSConnection(host, timeout=10)
            httpClient.request("GET", access_token_url, headers=headers)
            response = httpClient.getresponse()
            token = json.loads(response.read())['access_token']
        except Exception as e:
            self.log(program, '[Action: wechat] get token error %s' % e)
            return False
        finally:
            if httpClient:
                httpClient.close()

        send_url = '/cgi-bin/message/send?access_token={token}'.format(
            token=token)

        ip = ""
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            s.connect(('8.8.8.8', 80))
            ip = s.getsockname()[0]
        except Exception as e:
            self.log(program, '[Action: wechat] get ip error %s' % e)
        finally:
            s.close()

        hostname = platform.node().split('.')[0]
        system_platform = platform.platform()

        curr_dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        if check_status == 'success':
            title = "<font color=\"info\">[Supervisor] %s Health check successful</font>" % program
        else:
            title = "<font color=\"warning\">[Supervisor] %s Health check failed</font>" % program

        content = "{title}\n \
        > **详情信息**\n \
        > DataTime: {curr_dt}\n \
        > Program: <font color=\"warning\">{program}</font>\n \
        > IP: {ip}\n \
        > Hostname: {hostname}\n \
        > Platfrom: {platfrom}\n \
        > Action: {action}\n \
        > Msg: {msg}".format(title=title,
                             curr_dt=curr_dt,
                             program=program,
                             ip=ip,
                             hostname=hostname,
                             platfrom=system_platform,
                             action=action_type,
                             msg=msg)

        data = {
            "msgtype": 'markdown',
            "agentid": agentid,
            "markdown": {
                'content': content
            },
            "safe": 0
        }

        if touser:
            data['touser'] = touser
        if toparty:
            data['toparty'] = toparty
        if toparty:
            data['totag'] = totag

        try:
            httpClient = httplib.HTTPSConnection(host, timeout=10)
            httpClient.request("POST",
                               send_url,
                               json.dumps(data),
                               headers=headers)
            response = httpClient.getresponse()
            result = json.loads(response.read())
            if result['errcode'] != 0:
                self.log(program, '[Action: wechat] send faild %s' % result)
                return False
        except Exception as e:
            self.log(program, '[Action: wechat] send error %s' % e)
            return False
        finally:
            if httpClient:
                httpClient.close()

        self.log(program, '[Action: wechat] send success')
        return True
예제 #6
0
파일: upload.py 프로젝트: luw630/ddzserver
    def upload_file(self, command, pyversion, filename):
        # Sign if requested
        if self.sign:
            gpg_args = ["gpg", "--detach-sign", "-a", filename]
            if self.identity:
                gpg_args[2:2] = ["--local-user", self.identity]
            spawn(gpg_args, dry_run=self.dry_run)

        # Fill in the data - send all the meta-data in case we need to
        # register a new release
        f = open(filename, 'rb')
        try:
            content = f.read()
        finally:
            f.close()
        meta = self.distribution.metadata
        data = {
            # action
            ':action': 'file_upload',
            'protcol_version': '1',

            # identify release
            'name': meta.get_name(),
            'version': meta.get_version(),

            # file content
            'content': (os.path.basename(filename), content),
            'filetype': command,
            'pyversion': pyversion,
            'md5_digest': md5(content).hexdigest(),

            # additional meta-data
            'metadata_version': '1.0',
            'summary': meta.get_description(),
            'home_page': meta.get_url(),
            'author': meta.get_contact(),
            'author_email': meta.get_contact_email(),
            'license': meta.get_licence(),
            'description': meta.get_long_description(),
            'keywords': meta.get_keywords(),
            'platform': meta.get_platforms(),
            'classifiers': meta.get_classifiers(),
            'download_url': meta.get_download_url(),
            # PEP 314
            'provides': meta.get_provides(),
            'requires': meta.get_requires(),
            'obsoletes': meta.get_obsoletes(),
        }
        comment = ''
        if command == 'bdist_rpm':
            dist, version, id = platform.dist()
            if dist:
                comment = 'built for %s %s' % (dist, version)
        elif command == 'bdist_dumb':
            comment = 'built for %s' % platform.platform(terse=1)
        data['comment'] = comment

        if self.sign:
            data['gpg_signature'] = (os.path.basename(filename) + ".asc",
                                     open(filename + ".asc", "rb").read())

        # set up the authentication
        user_pass = (self.username + ":" + self.password).encode('ascii')
        # The exact encoding of the authentication string is debated.
        # Anyway PyPI only accepts ascii for both username or password.
        auth = "Basic " + standard_b64encode(user_pass).decode('ascii')

        # Build up the MIME payload for the POST data
        boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
        sep_boundary = b'\n--' + boundary.encode('ascii')
        end_boundary = sep_boundary + b'--'
        body = io.BytesIO()
        for key, value in data.items():
            title = '\nContent-Disposition: form-data; name="%s"' % key
            # handle multiple entries for the same name
            if type(value) != type([]):
                value = [value]
            for value in value:
                if type(value) is tuple:
                    title += '; filename="%s"' % value[0]
                    value = value[1]
                else:
                    value = str(value).encode('utf-8')
                body.write(sep_boundary)
                body.write(title.encode('utf-8'))
                body.write(b"\n\n")
                body.write(value)
                if value and value[-1:] == b'\r':
                    body.write(b'\n')  # write an extra newline (lurve Macs)
        body.write(end_boundary)
        body.write(b"\n")
        body = body.getvalue()

        self.announce("Submitting %s to %s" % (filename, self.repository),
                      log.INFO)

        # build the Request
        # We can't use urllib since we need to send the Basic
        # auth right with the first request
        # TODO(jhylton): Can we fix urllib?
        schema, netloc, url, params, query, fragments = \
            urllib.parse.urlparse(self.repository)
        assert not params and not query and not fragments
        if schema == 'http':
            http = httpclient.HTTPConnection(netloc)
        elif schema == 'https':
            http = httpclient.HTTPSConnection(netloc)
        else:
            raise AssertionError("unsupported schema " + schema)

        data = ''
        loglevel = log.INFO
        try:
            http.connect()
            http.putrequest("POST", url)
            http.putheader('Content-type',
                           'multipart/form-data; boundary=%s' % boundary)
            http.putheader('Content-length', str(len(body)))
            http.putheader('Authorization', auth)
            http.endheaders()
            http.send(body)
        except socket.error as e:
            self.announce(str(e), log.ERROR)
            return

        r = http.getresponse()
        if r.status == 200:
            self.announce('Server response (%s): %s' % (r.status, r.reason),
                          log.INFO)
        else:
            self.announce('Upload failed (%s): %s' % (r.status, r.reason),
                          log.ERROR)
        if self.show_response:
            msg = '\n'.join(('-' * 75, r.read(), '-' * 75))
            self.announce(msg, log.INFO)
예제 #7
0
 def open(self):
     self.__http = http_client.HTTPSConnection(self.host, self.port)
예제 #8
0
def load(hostname, url, schema, table, verbose=False, debug=False):
    if verbose: show("begin")

    row = makerow()
    dboperator.columns(row, debug)

    if verbose: show("empty %s.%s" % (schema, table))
    dboperator.empty(schema, table, debug)

    httpconn = httplib.HTTPSConnection(hostname)
    show("load securely from " + hostname + url)

    # get list of oids
    reqheaders = {'Caller-Id': '1.2.246.562.10.2013112012294919827487.vipunen'}
    httpconn.request('GET', url, headers=reqheaders)
    #httpconn.request('GET', url)
    rr = httpconn.getresponse()
    jj = json.loads(rr.read())
    cnt = 0
    rows = []
    for ii in jj["result"]:
        cnt += 1
        # show some sign of being alive
        if cnt % 100 == 0:
            sys.stdout.write('.')
            sys.stdout.flush()
        if cnt % 1000 == 0:
            show("-- %d" % (cnt))

        url = "/tarjonta-service/rest/v1/hakukohde/%s?populateAdditionalKomotoFields=true" % (
            ii["oid"])

        try:
            httpconn.request('GET', url, headers=reqheaders)
            r = httpconn.getresponse()
            j = json.loads(r.read())
        except ValueError as e:
            show("-- %d -- could not load %s" % (cnt, ii["oid"]))
        else:
            if j["status"] == "NOT FOUND":
                continue
            if j["status"] == "OK":
                i = j["result"]
                row = makerow()

                for col in row:
                    row[col] = None if col not in i else i[col]
                    if type(row[col]) is list:
                        row[col] = ''.join(map(str, json.dumps(row[col])))

            if verbose: show("%d -- %s" % (cnt, row["oid"]))
            if debug: print(row)
            rows.append(row)
            if cnt % 5000 == 0:
                dboperator.insertMany(hostname + url, schema, table, rows)
                #dboperator.insert(hostname+url,schema,table,row,debug)
                rows = []
        #TÄHÄN VIELÄ INSERT dataset[] #< 5000
    dboperator.insertMany(hostname + url, schema, table, rows)
    show("Total rows: %d" % (cnt))

    if verbose: show("ready")
예제 #9
0
__maintainer__ = "Ralph-Gordon Paul"
__email__ = "*****@*****.**"
__status__ = "Production"

# read settings
__settings__ = xbmcaddon.Addon("script.traktutilities")
__language__ = __settings__.getLocalizedString

apikey = '0a698a20b222d0b8637298f6920bf03a'
username = __settings__.getSetting("username")
pwd = sha.new(__settings__.getSetting("password")).hexdigest()
debug = __settings__.getSetting("debug")
https = __settings__.getSetting('https')

if (https == 'true'):
    conn = httplib.HTTPSConnection('api.trakt.tv')
else:
    conn = httplib.HTTPConnection('api.trakt.tv')

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


# list watchlist movies
def showWatchlistMovies():

    movies = getWatchlistMoviesFromTrakt()

    if movies == None:  # movies = None => there was an error
    conn.request('GET', url, headers={'User-Agent': u'Random Word Correlation Release Namer 0.1'})
    response = conn.getresponse()
    data = response.read().decode('UTF-8')
    data = json.loads(data)
    for page in data['query']['random']:
        test_word = page['title'].lower()
        if test_word.startswith(word[0].lower()):
            return page['title']
    return False

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print(u"Usage: release_name.py <word>")
        sys.exit()
    word = sys.argv[1]
    conn = httplib.HTTPSConnection(host)
    c = 0
    while True:
        c += 1
        print("Try {}".format(c))
        page = parse_random_list(conn, host, url, word)
        if page:
            print(u"Found page: {}".format(page))
            print(u"Possible release names:")
            print(u"\t- {}-{}".format(page.split()[0].lower(), word.lower()))
            print(u"\t- {}-{}".format(word.lower(), page.split()[0].lower()))
            break

        # We don't want to spam wikipedia too much
        if c % 10 == 0:
            time.sleep(10)
예제 #11
0
    def send_sms(self, body_text=False, send_to=False, keep_history=True):
        # conn = http.client.HTTPSConnection("api.infobip.com")
        infobip_config = self.env['fl.infobip.sms'].search([], limit=1)
        if infobip_config and infobip_config.base_url and infobip_config.username and infobip_config.password:
            conn = client.HTTPSConnection(infobip_config.base_url)

            payload = "{\"from\":\"InfoSMS\",\"to\":\"%s\",\"text\":\"%s\"}" % (
                send_to, body_text.encode('utf-8').decode('latin-1', 'ignore'))
            data_str = infobip_config.username + ':' + infobip_config.password
            data_str = "%s:%s" % (infobip_config.username,
                                  infobip_config.password)
            data_bytes = data_str.encode("utf-8")
            base_64 = base64.b64encode(data_bytes)
            base_64 = str(base_64, 'utf-8')
            auth = "Basic %s" % (base_64)

            headers = {
                'authorization': auth,
                'content-type': "application/json",
                'accept': "application/json"
            }
            # _logger.info("\nHeader : %s",headers)
            conn.request("POST", "/sms/1/text/single", payload, headers)
            res = conn.getresponse()
            data = res.read()

            res = literal_eval(data.decode("utf-8"))
            print(res)

            #             send_sms_client = send_single_textual_sms(Configuration(infobip_config.username, infobip_config.password))
            #
            #             request = SMSTextualRequest()
            #             request.text = body_text or ''
            #             request.to = [send_to]
            #             response = send_sms_client.execute(request)
            #
            #             print(response)

            if keep_history:
                state = "draft"
                if "requestError" in res:
                    state = "fail"
                elif "messages" in res:
                    tmp_res = res.get("messages")[0]
                    if tmp_res and tmp_res["status"][
                            "name"] == "MESSAGE_ACCEPTED":
                        state = "sent"
                    elif tmp_res and tmp_res["status"][
                            "name"] == "PENDING_ENROUTE":
                        _logger.info("\n\nsent : %s",
                                     tmp_res["status"]["name"])
                        state = "sent"
                    elif tmp_res and tmp_res["status"][
                            "name"] == "REJECTED_PREFIX_MISSING":
                        state = "reject"
                    elif tmp_res and tmp_res["status"][
                            "name"] == "REJECTED_DESTINATION":
                        state = "reject"

                self.env['fl.infobip.sms.history'].create({
                    "send_to": send_to,
                    "send_body": body_text,
                    "state": state,
                    "api_response": res
                })

        else:
            raise ValidationError(
                _('Infobip SMS configuration missing. You can find inside Settings > Technical > Infobip SMS > Configuration'
                  ))
예제 #12
0
파일: kanan.py 프로젝트: lishid/kanan
def get_login_passport(creds):
    # Get something unique (not important as far as I can tell)
    cmd_result = run(['wmic', 'csproduct', 'get', 'uuid'], stdout=PIPE)
    cmd_result_str = StringIO(cmd_result.stdout.decode('utf-8'))

    # skip the first line
    cmd_result_str.readline()

    # Grab UUID
    uuid = cmd_result_str.readline().strip()

    username = creds[0]
    password = creds[1]
    # Ask for username/password.
    if username == '':
        username = input("Username: "******"Password: ")

    # Immediately convert it.
    password = hexlify(sha512(bytes(password,
                                    'utf-8')).digest()).decode('utf-8')

    # First request.
    headers = {
        'User-Agent': 'NexonLauncher.nxl-17.04.01-290-621f8e0',
        'Content-Type': 'application/json'
    }
    body = {
        'id': username,
        'password': password,
        'auto_login': False,
        'client_id': '7853644408',
        'scope': 'us.launcher.all',
        'device_id': uuid
    }
    body_str = dumps(body)
    connection = client.HTTPSConnection('accounts.nexon.net', 443)

    connection.request('POST',
                       '/account/login/launcher',
                       body=body_str,
                       headers=headers)

    response = loads(connection.getresponse().read().decode('utf-8'))
    b64_token = b64encode(bytes(response['access_token'],
                                'utf-8')).decode('utf-8')

    # Second request.
    headers = {
        'User-Agent': 'NexonLauncher.nxl-17.04.01-290-621f8e0',
        'Cookie':
        'nxtk=' + response['access_token'] + ';domain=.nexon.net;path=/;',
        'Authorization': 'bearer ' + b64_token
    }
    connection = client.HTTPSConnection('api.nexon.io', 443)

    connection.request('GET', '/users/me/passport', headers=headers)
    response = loads(connection.getresponse().read().decode('utf-8'))

    # Return the passport.
    return response['passport']
예제 #13
0
def makeConnection(scheme, host):
    if forceHttp or scheme.upper() == 'HTTP':
        return httplib.HTTPConnection(host)
    return httplib.HTTPSConnection(host)
예제 #14
0
def get_serving_endpoint(model, project=None):
    """

    Args:
        model:
        project:

    Returns:

    """

    endpoint = os.environ['REST_ENDPOINT']

    if 'http' in endpoint:
        last_index = endpoint.rfind('/')
        endpoint = endpoint[last_index + 1:]

    host_port_pair = endpoint.split(':')

    #hardcode disabled for now
    os.environ['SSL_ENABLED'] = 'false'

    if os.environ['SSL_ENABLED'] == 'true':
        connection = http.HTTPSConnection(str(host_port_pair[0]),
                                          int(host_port_pair[1]))
    else:
        connection = http.HTTPConnection(str(host_port_pair[0]),
                                         int(host_port_pair[1]))

    headers = {'Content-type': 'application/json'}

    material_passwd = os.getcwd() + '/material_passwd'

    if not os.path.exists(material_passwd):
        raise AssertionError(
            'material_passwd is not present in current working directory')

    with open(material_passwd) as f:
        keyStorePwd = f.read()

    k_certificate = os.getcwd() + '/k_certificate'

    if not os.path.exists(material_passwd):
        raise AssertionError(
            'k_certificate is not present in current working directory')

    with open(k_certificate) as f:
        keyStore = f.read()
        keyStore = base64.b64encode(keyStore)

    if not project:
        project = hdfs.project_name()

    json_contents = {
        'project': project,
        'model': model,
        'keyStorePwd': keyStorePwd,
        'keyStore': keyStore
    }

    json_embeddable = json.dumps(json_contents)

    connection.request('POST', '/hopsworks-api/api/appservice/tfserving',
                       json_embeddable, headers)

    response = connection.getresponse()
    respBody = response.read()
    responseObject = json.loads(respBody)

    host = responseObject['host']
    port = responseObject['port']

    return str(host) + ':' + str(port)
예제 #15
0
파일: fetch.py 프로젝트: gbrammer/mastquery
def directDownload(tab,
                   inst_products=DEFAULT_PRODUCTS,
                   path='./',
                   skip_existing=True):

    import sys
    import os
    import time
    import re
    import json

    try:  # Python 3.x
        from urllib.parse import quote as urlencode
        from urllib.request import urlretrieve
    except ImportError:  # Python 2.x
        from urllib import pathname2url as urlencode
        from urllib import urlretrieve

    try:  # Python 3.x
        import http.client as httplib
    except ImportError:  # Python 2.x
        import httplib

    # collect the data products
    #result = getCaomProducts()
    #data = result['data']

    # setting up the https connection
    server = 'mast.stsci.edu'
    conn = httplib.HTTPSConnection(server)

    # dowload the first products
    for i in range(len(tab)):
        products = inst_products[tab[i]['instrument_name']]
        for prod in products:
            ext = '_{0}.fits'.format(prod.lower())

            # make file path
            #outPath = "mastFiles/HST/"+tab[i]['obs_id']
            #if not os.path.exists(outPath):
            #    os.makedirs(outPath)
            outPath = os.path.join(
                path, tab[i]['productFilename'].split('_')[0] + ext)

            if os.path.exists(outPath) & skip_existing:
                print('File {0} exists.'.format(outPath))
                continue

            # Download the data
            uri = tab[i]['dataURI'].split('_')[0] + ext
            conn.request("GET", "/api/v0/download/file?uri=" + uri)
            resp = conn.getresponse()
            fileContent = resp.read()

            # save to file
            with open(outPath, 'wb') as FLE:
                FLE.write(fileContent)

            # check for file
            if not os.path.isfile(outPath):
                print("ERROR: " + outPath + " failed to download.")
            else:
                print("COMPLETE: ", outPath)

    conn.close()
예제 #16
0
import socket
import logging
from typing import Optional, Union

from netCDF4 import Dataset
import numpy as np

logging.basicConfig(level=logging.DEBUG,
                    format="%(levelname)-9s %(name)s.%(funcName)s:%(lineno)d > %(message)s")
logger = logging.getLogger(__name__)

url = r"https://opendap.co-ops.nos.noaa.gov/thredds/dodsC/NOAA/GOMOFS/MODELS/201901/nos.gomofs.regulargrid.n003.20190115.t00z.nc"

try:
    p = parse.urlparse(url)
    conn = client.HTTPSConnection(p.netloc)
    conn.request('HEAD', p.path)
    resp = conn.getresponse()
    conn.close()
    logger.debug("passed url: %s -> %s" % (url, resp.status))

except socket.error as e:
    logger.warning("while checking %s, %s" % (url, e))
    exit(-1)

file_temp = Dataset(url)
_lat = file_temp.variables['Latitude'][:]
_lon = file_temp.variables['Longitude'][:]

logger.debug("latitude: %s" % _lat[-1, -1])
logger.debug("longitude: %s" % _lon[-1, -1])
예제 #17
0
def get():
    conn = client.HTTPSConnection('www.some.ru', timeout=3)
    try:
        conn.request('GET', '/')
    finally:
        conn.close()
예제 #18
0
                                      "app_code":"{YOUR_APP_CODE}"}),"",
                   {"Accept":"application/json"})
    commerces = []
    resp = connection.getresponse()
    if resp.status == client.OK:
        jsonData = json.JSONDecoder().decode(bytes.decode(resp.read()))
        for commerce in jsonData["results"]["items"]:
            commerces.append(commerce)
            #print("- " + commerce["title"] + " : (" + str(commerce["position"][1]) + ', ' + str(commerce["position"][0]) + ')')
    else:
        print("Erreur requête : " + str(resp.status))
    return commerces

print("Lignes de métro de Lyon et commerces proches d'une des extrémités des lignes")

connection = client.HTTPSConnection("download.data.grandlyon.com")

#https://download.data.grandlyon.com/wfs/rdata?SERVICE=WFS&VERSION=2.0.0&request=GetFeature&typename=tcl_sytral.tcllignemf&outputFormat=application/json; subtype=geojson&SRSNAME=EPSG:4326&startIndex=0
connection.request("GET", "/wfs/rdata?" +
                   parse.urlencode({"SERVICE": "WFS",
                                    "VERSION": "2.0.0",
                                    "request": "GetFeature",
                                    "typename": "tcl_sytral.tcllignemf",
                                    "SRSNAME": "EPSG:4326",
                                    "outputFormat": "geojson",
                                    "startIndex": "0"}), "",
                   {"Accept": "application/json"})

resp = connection.getresponse()
print("HTTP Rest : download.data.grandlyon.com, résultat " + str(resp.status))
예제 #19
0
    def __init__(self,
                 service_url=None,
                 service_port=8332,
                 btc_conf_file=None,
                 timeout=HTTP_TIMEOUT,
                 _connection=None):
        """Low-level JSON-RPC proxy

        Unlike Proxy no conversion is done from the raw JSON objects.
        """

        if service_url is None:
            # Figure out the path to the bitcoin.conf file
            if btc_conf_file is None:
                if platform.system() == 'Darwin':
                    btc_conf_file = os.path.join(os.environ['APPDATA'],
                                                 'Bitcoin')
                elif platform.system() == 'Windows':
                    btc_conf_file = os.path.expanduser(
                        '~/Library/Application Support/Bitcoin/')
                else:
                    btc_conf_file = os.path.expanduser('~/.bitcoin')
                btc_conf_file = os.path.join(btc_conf_file, 'bitcoin.conf')

            # Extract contents of bitcoin.conf to build service_url
            with open(btc_conf_file, 'r') as fd:
                conf = {}
                for line in fd.readlines():
                    if '#' in line:
                        line = line[:line.index('#')]
                    if '=' not in line:
                        continue
                    k, v = line.split('=', 1)
                    conf[k.strip()] = v.strip()

                conf['rpcport'] = int(conf.get('rpcport', service_port))
                conf['rpcssl'] = conf.get('rpcssl', '0')

                if conf['rpcssl'].lower() in ('0', 'false'):
                    conf['rpcssl'] = False
                elif conf['rpcssl'].lower() in ('1', 'true'):
                    conf['rpcssl'] = True
                else:
                    raise ValueError('Unknown rpcssl value %r' %
                                     conf['rpcssl'])

                service_url = (
                    '%s://%s:%s@localhost:%d' %
                    ('https' if conf['rpcssl'] else 'http', conf['rpcuser'],
                     conf['rpcpassword'], conf['rpcport']))

        self.__service_url = service_url
        self.__url = urlparse.urlparse(service_url)
        if self.__url.port is None:
            port = 80
        else:
            port = self.__url.port
        self.__id_count = 0
        authpair = "%s:%s" % (self.__url.username, self.__url.password)
        authpair = authpair.encode('utf8')
        self.__auth_header = b"Basic " + base64.b64encode(authpair)

        if _connection:
            # Callables re-use the connection of the original proxy
            self.__conn = _connection
        elif self.__url.scheme == 'https':
            self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
                                                  None, None, False, timeout)
        else:
            self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
                                                 False, timeout)
예제 #20
0
import sys
if (3, 0) <= sys.version_info < (4, 0):
    import http.client as httplib
elif (2, 6) <= sys.version_info < (3, 0):
    import httplib

headers = {
    "Authorization": "Bearer TOKENHERE",
    "Content-Type": "application/json"
}

for i in range(len(spainListusers)):
    params = {
        "shared_folder_id": "123456",
        "member": {
            ".tag": "email",
            "email": spainListusers[i]
        },
        "access_level": {
            ".tag": "viewer"
        }
    }
    c = httplib.HTTPSConnection("api.dropboxapi.com")
    c.request("POST", "/2/sharing/update_folder_member", json.dumps(params),
              headers)
    r = c.getresponse()
    print(str(r.status) + " " + r.read() + "for email: " + spainListusers[i])
###  API responses, first if an error occurs for reporting and second to show the new doc URL

###  API responses, first if an error occurs for reporting and second to show the new doc URL
    def _make_request(self,
                      path,
                      method=_HTTP_METHOD_GET,
                      params=None,
                      headers=None,
                      body=None):
        """ Places an https request and returns the decoded JSON response object.

        Args:
            path: Path portion of url to retrieve,
                e.g. "/api/asset-store-tools/metadata/0.json"
            method: Http method to use as static flag above,
                e.g. _HTTP_METHOD_GET.
            params: Any additional params to include with request.
            headers: Any additional headers to include with request.
            body: Form body for PUT requests.

        Returns:
            JSON-decoded response data.

        Raises:
            RequestFailedError if response is not found.
        """
        params = dict(params) if params else {}
        headers = dict(headers) if headers else {}

        if not self.session_id:
            self._login()

        if self.username:
            params['user'] = self.username
        if self.password:
            params['pass'] = self.password
        params['unityversion'] = self.unity_version
        params['toolversion'] = self.tools_version
        params['xunitysession'] = self.session_id

        headers['Accept'] = 'application/json'

        encoded_params = parse.urlencode(params)

        try:
            connection = client.HTTPSConnection(self.server)
            if method == _HTTP_METHOD_GET:
                connection.request('GET',
                                   self._encode_path_and_params(path, params),
                                   headers=headers)
            elif method == _HTTP_METHOD_POST:
                headers['Content-Type'] = 'application/x-www-form-urlencoded'
                connection.request('POST',
                                   path,
                                   parse.urlencode(params),
                                   headers=headers)
            elif method == _HTTP_METHOD_PUT:
                connection.request('PUT',
                                   self._encode_path_and_params(path, params),
                                   body,
                                   headers=headers)
            else:
                raise ValueError("Invalid http method provided.")
            response = connection.getresponse()
            if response.status > client.FOUND:
                print("Response: {}".format(response.read()))
                raise Exception("Error making http request: {} {}".format(
                    response.status, response.reason))

            response_ob = json.load(response)
        finally:
            connection.close()

        return response_ob
예제 #22
0
 def httpGet(self, url, resource, params=''):
     conn = httplib.HTTPSConnection(url, timeout=10)
     conn.request("GET", resource + '/' + params)
     response = conn.getresponse()
     data = response.read().decode('utf-8')
     return json.loads(data)
예제 #23
0
    def dest_restapi(self, st, doctype, resourceid, message_body):
        global response, data
        if doctype not in ['inca', 'nagios']:
            self.logger.debug(
                'exchange=%s, routing_key=%s, size=%s dest=DROP' %
                (doctype, resourceid, len(message_body)))
            return

        if doctype in ['inca']:
            data = json.loads(message_body)
            if 'rep:report' in data:
                self.logger.debug(
                    'exchange=%s, routing_key=%s, size=%s discarding old format'
                    % (doctype, resourceid, len(message_body)))
                return

            try:
                resourceid = data['TestResult']['Associations']['ResourceID']
            except:
                self.logger.error(
                    'exchange=%s, routing_key=%s, size=%s missing Associations->ResourceID'
                    % (doctype, resourceid, len(message_body)))
                return

        elif doctype in ['nagios']:
            data = json.loads(message_body)
            try:
                resourceid = data['TestResult']['Associations']['ResourceID']
            except:
                self.logger.error(
                    'exchange=%s, routing_key=%s, size=%s missing Associations->ResourceID'
                    % (doctype, resourceid, len(message_body)))
                return

        headers = {
            'Content-type':
            'application/json',
            'Authorization':
            'Basic %s' % base64.standard_b64encode(
                (self.config['API_USERID'] + ':' +
                 self.config['API_PASSWORD']).encode()).decode()
        }
        url = '/monitoring-provider-api/v1/process/doctype/%s/resourceid/%s/' % (
            doctype, resourceid)
        if self.dest['host'] not in ['localhost', '127.0.0.1'
                                     ] and self.dest['port'] != '8000':
            url = '/wh1' + url
        (host, port) = (self.dest['host'], self.dest['port'])
        retries = 0
        while retries < 100:
            try:
                if self.dest['port'] == '443':
                    #                ssl_con = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, capath='/etc/grid-security/certificates/')
                    #                ssl_con.load_default_certs()
                    #                ssl_con.load_cert_chain('certkey.pem')
                    ssl_con = ssl._create_unverified_context(check_hostname=False, \
                                                             certfile=self.config['X509_CERT'], keyfile=self.config['X509_KEY'])
                    conn = httplib.HTTPSConnection(host, port, context=ssl_con)
                else:
                    conn = httplib.HTTPConnection(host, port)
                self.logger.debug('POST %s' % url)
                conn.request('POST', url, message_body, headers)
                response = conn.getresponse()
                self.logger.info(
                    'RESP exchange=%s, routing_key=%s, size=%s dest=POST http_response=status(%s)/reason(%s)'
                    % (doctype, resourceid, len(message_body), response.status,
                       response.reason))
                data = response.read()
                conn.close()
                retries = 0  # Success, reset retries
                break
            except (socket.error) as e:
                retries += 1
                sleepminutes = 2 * retries
                self.logger.error('Exception socket.error to %s:%s; sleeping %s/minutes before retrying' % \
                                  (host, port, sleepminutes))
                sleep(sleepminutes * 60)
            except (httplib.BadStatusLine) as e:
                retries += 1
                sleepminutes = 2 * retries
                self.logger.error('Exception httplib.BadStatusLine to %s:%s; sleeping %s/minutes before retrying' % \
                                  (host, port, sleepminutes))
                sleep(sleepminutes * 60)

        if response.status in [400, 403]:
            self.logger.error('response=%s' % data)
            return
        try:
            obj = json.loads(data)
        except ValueError as e:
            self.logger.error('API response not in expected format (%s)' % e)
예제 #24
0
        def application(environ, start_response):
            headers = list(EnvironHeaders(environ).items())
            headers[:] = [
                (k, v)
                for k, v in headers
                if not is_hop_by_hop_header(k)
                and k.lower() not in ("content-length", "host")
            ]
            headers.append(("Connection", "close"))

            if opts["host"] == "<auto>":
                headers.append(("Host", target.ascii_host))
            elif opts["host"] is None:
                headers.append(("Host", environ["HTTP_HOST"]))
            else:
                headers.append(("Host", opts["host"]))

            headers.extend(opts["headers"].items())
            remote_path = path

            if opts["remove_prefix"]:
                remote_path = "%s/%s" % (
                    target.path.rstrip("/"),
                    remote_path[len(prefix) :].lstrip("/"),
                )

            content_length = environ.get("CONTENT_LENGTH")
            chunked = False

            if content_length not in ("", None):
                headers.append(("Content-Length", content_length))
            elif content_length is not None:
                headers.append(("Transfer-Encoding", "chunked"))
                chunked = True

            try:
                if target.scheme == "http":
                    con = client.HTTPConnection(
                        target.ascii_host, target.port or 80, timeout=self.timeout
                    )
                elif target.scheme == "https":
                    con = client.HTTPSConnection(
                        target.ascii_host,
                        target.port or 443,
                        timeout=self.timeout,
                        context=opts["ssl_context"],
                    )
                else:
                    raise RuntimeError(
                        "Target scheme must be 'http' or 'https', got '{}'.".format(
                            target.scheme
                        )
                    )

                con.connect()
                remote_url = url_quote(remote_path)
                querystring = environ["QUERY_STRING"]

                if querystring:
                    remote_url = remote_url + "?" + querystring

                con.putrequest(environ["REQUEST_METHOD"], remote_url, skip_host=True)

                for k, v in headers:
                    if k.lower() == "connection":
                        v = "close"

                    con.putheader(k, v)

                con.endheaders()
                stream = get_input_stream(environ)

                while 1:
                    data = stream.read(self.chunk_size)

                    if not data:
                        break

                    if chunked:
                        con.send(b"%x\r\n%s\r\n" % (len(data), data))
                    else:
                        con.send(data)

                resp = con.getresponse()
            except socket.error:
                from ..exceptions import BadGateway

                return BadGateway()(environ, start_response)

            start_response(
                "%d %s" % (resp.status, resp.reason),
                [
                    (k.title(), v)
                    for k, v in resp.getheaders()
                    if not is_hop_by_hop_header(k)
                ],
            )

            def read():
                while 1:
                    try:
                        data = resp.read(self.chunk_size)
                    except socket.error:
                        break

                    if not data:
                        break

                    yield data

            return read()
예제 #25
0
 def testHTTPSConnectionSourceAddress(self):
     self.conn = client.HTTPSConnection(HOST, self.port,
             source_address=('', self.source_port))
예제 #26
0
    def _do_request(self,
                    method=None,
                    path=None,
                    query=None,
                    body=None,
                    headers=None):
        method = method or 'GET'

        if query:
            path = path + '?' + urlencode(query)
        if body:
            body = ensure_binary(body)

        if self.debug:
            print('[Request] {0} {1}://{2}:{3}{4}'.format(
                method, ensure_str(self.protocol), ensure_str(self.host),
                str(self.port), ensure_str(path)))
            print('[Request Headers]\n{0}'.format('\n'.join(
                ['{0}: {1}'.format(k, v) for k, v in (headers or {}).items()])
                                                  or '<EMPTY>'))
            if method.upper() != 'GET':
                print('[Request Body]\n{0}'.format(
                    ensure_str(body or '') or '<EMPTY>'))

        resp_status_code = 0
        resp_raw_data = None
        resp_data = None
        if not self.dry_run:
            conn = None
            if self.protocol == 'https':
                conn = httplib.HTTPSConnection(self.host,
                                               port=self.port,
                                               timeout=self.timeout)
            else:
                conn = httplib.HTTPConnection(self.host,
                                              port=self.port,
                                              timeout=self.timeout)

            try:
                conn.request(method, path, body=body, headers=headers)
            except Exception as e:
                raise Exception('Send request `{} {}:{}{}` failed: {}'.format(
                    method, self.host, self.port, path, str(e)))

            resp = conn.getresponse()

            resp_status_code = resp.status
            resp_raw_data = resp.read()

            resp_content_type = resp.getheader('Content-Type')
            if isinstance(resp_content_type, string_types):
                resp_content_type = resp_content_type.split(';')[0].strip()

            resp_data = resp_raw_data
            if resp_content_type == 'application/json':
                resp_data = json.loads(ensure_str(resp_raw_data))

            if self.debug:
                output = '\n[Response Status Code] {0}\n[Response Body] {1}'.format(
                    resp_status_code,
                    ensure_str(resp_raw_data or '') or '<EMPTY>')

                color = 'green'
                if resp_status_code >= 400:
                    color = 'red'

                print(colored(output, color))

        if resp_status_code >= 400:
            e = Exception(resp_status_code, resp_data)
            raise e

        return resp_status_code, resp_data
예제 #27
0
        'include': ['whois', 'api'],
        'exclude': ['.win']
    },
    'mode': 'purchase',
    'sinceDate': '2018-07-14'
}


def print_response(txt):
    response_json = json.loads(txt)
    print(json.dumps(response_json, indent=4, sort_keys=True))


headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'X-Authentication-Token': api_key
}

conn = http.HTTPSConnection('registrant-alert.whoisxmlapi.com')

conn.request('POST', '/api/v2', json.dumps(payload_basic), headers)
text = conn.getresponse().read().decode('utf8')
print('Basic:')
print_response(text)

conn.request('POST', '/api/v2', json.dumps(payload_advanced), headers)
text = conn.getresponse().read().decode('utf8')
print('Advanced:')
print_response(text)
예제 #28
0
    def _http(self,
              method,
              url,
              x_headers=None,
              body=None,
              file=None,
              content_type=None):
        '''
        HTTP Request to the specified API endpoint
        @param method string
        @param x_headers list
        @param body dict json
        @return TacytResponse
        '''

        auth_headers = None
        json_body = None

        try:
            # Try to use the new Python3 HTTP library if available
            import http.client as http
            import urllib.parse as urllib
        except:
            # Must be using Python2 so use the appropriate library
            import httplib as http
            import urllib

        if Auth.API_PROXY != None:
            if Auth.API_HTTPS:
                conn = http.HTTPSConnection(Auth.API_PROXY,
                                            Auth.API_PROXY_PORT)
                conn.set_tunnel(Version.API_HOST, Auth.API_PORT)
            else:
                conn = http.HTTPConnection(Auth.API_PROXY, Auth.API_PROXY_PORT)
                url = "http://" + Version.API_HOST + url
        else:
            if Auth.API_HTTPS:
                conn = http.HTTPSConnection(Version.API_HOST, Auth.API_PORT)
            else:
                conn = http.HTTPConnection(Version.API_HOST, Auth.API_PORT)

        if self.HTTP_METHOD_GET == method or self.HTTP_METHOD_DELETE == method:
            auth_headers = self.authentication_headers(method, url, x_headers,
                                                       None, None)
            conn.request(method, url, headers=auth_headers)

        elif self.HTTP_METHOD_POST == method or self.HTTP_METHOD_PUT == method:

            if body is not None and file is None:
                json_body = json.dumps(body)
                auth_headers = self.authentication_headers_with_body(
                    method, url, x_headers, json_body, None)
                auth_headers[self.HTTP_HEADER_CONTENT_TYPE] = content_type

        try:

            all_headers = auth_headers

            if body is not None:
                conn.request(method=method,
                             url=url,
                             body=json_body,
                             headers=all_headers)

            res = conn.getresponse()
            response_data = res.read().decode('utf8')

            conn.close()
            ret = Response(json_string=response_data)

        except Exception as e:
            print("Exception")
            print(e)
            print(repr(e))
            ret = None

        return ret
 def get_connection_secure(self, verbose):
     return httplib.HTTPSConnection(self.__connHost, self.__connPortSsl)
예제 #30
0
파일: swish.py 프로젝트: jacob22/accounting
 def getConnection(self, host, timeout=300):
     return httplib.HTTPSConnection(host,
                                    key_file=self.key,
                                    cert_file=self.cert)