예제 #1
0
파일: iloot.py 프로젝트: OverByThere/iloot
def probobuf_request(host, method, url, body, headers, msg=None):
    conn = HTTPSConnection(host)
    sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address)
    conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
    request = conn.request(method, url, body, headers)
    response = conn.getresponse()
    length = response.getheader("content-length")

    if length is None:
        length = 0
    else:
        length = int(length)

    data = response.read()

    while len(data) < length:
        d = response.read()
        data += d

    conn.close()
    if msg == None:
        return data

    res = msg()
    res.ParseFromString(data)
    return res
def get_status():
    conn = HTTPSConnection(host)
    conn.request("GET", "/15/status")
    response = conn.getresponse()
    status = json.loads(response.read(9999))
    conn.close()
    return status
예제 #3
0
파일: apiclient.py 프로젝트: awused/cyNaoko
 def _getDailymotionAPI(self, vid):
     self.logger.debug("Retrieving video information from the Dailymotion API.")
     con = HTTPSConnection("api.dailymotion.com", timeout=10)
     params = {"fields", "title,duration,allow_embed"}
     data = None
     try:
         try:
             con.request("GET", "/video/%s?fields=title,duration,allow_embed" % (vid))
             data = con.getresponse().read()
         except SSLError as e:
             # There is a bug in OpenSSL 1.0.1 which affects Python 2.7 on systems that rely on it.
             # Attempt to use curl as a fallback.
             # Curl must be installed for this to work.
             # This is the worst hack I have ever coded.
             # Since vid is a valid video id there is no risk of any attack.
             self.logger.warning("SSL Error, attempting to use curl as a fallback.")
             try:
                 data = subprocess.check_output(["curl", "-k", "-s", "-m 10",
                     "https://api.dailymotion.com/video/%s?fields=title,duration,allow_embed" % (vid)])
             except Exception as e:
                 self.logger.warning("Curl fallback failed.")
                 data = "SSL Failure"
                 raise e
         # Do this last and separately to avoid catching it elsewhere.
         data = json.loads(data)
     except Exception as e:
         # Many things can go wrong with an HTTP request or during JSON parsing
         self.logger.warning("Error retrieving Dailymotion API information.")
         self.logger.debug(e)
     finally:
         con.close()
         return data
예제 #4
0
  def query(self, function, method, params):
    """Send a GUI API request

      Args:
        config(ConfigParser): Config file input data
        function(string): Name of Booked REST API function
        method(string): POST or GET
        params(string): Arguments to REST function

      Returns:
        dict: contains Booked auth info
        JSON object: response from server
    """
    connection = HTTPSConnection(self.host, context=ssl._create_unverified_context())
    connection.connect()

    if not params:
      params = {}
    params['session_id'] = self.session_id

    url = "%s/%s" % (self.base_url, function)
    logging.debug("  Sending API call: " + url)
    data = self._run_query(connection, url, method, params)
    connection.close()
    return data
예제 #5
0
    def doLogin(self):
        """ Perform login, raise exception if something wrong """
        self.api_url = None
        self.auth_token = None
        uri = urlparse.urlparse(self.auth_url)
        headers = dict()
        headers['x-auth-user'] = self.api_user
        headers['x-auth-key'] = self.api_key

        conn = HTTPSConnection(uri.hostname, uri.port)
        conn.request('GET', self.auth_url, '', headers)
        response = conn.getresponse()
        buff = response.read()

        # 401 == Auth failed
        if response.status == 401:
            raise RackSpaceAuthenticationFailedException()

        # TODO: check codes and return more informative exceptions
        if response.status != 204:
            raise RackSpaceException()

        for header in response.getheaders():
            if header[0].lower() == "x-server-management-url":
                self.api_url = header[1]
            if header[0].lower() == "x-auth-token":
                self.auth_token = header[1]

        conn.close()

        if self.auth_token is None or self.api_url is None:
            # Invalid response
            raise RackSpaceException()
예제 #6
0
파일: prowl.py 프로젝트: Rafiot/Abusehelper
    def add(self, application, event=None, description=None, priority=0):

        if len(self.apikeys.split(",")) > 5:
            raise ValueError, "Too many apikeys!"

        if not event and not description:
            raise ValueError, "Missing event or description!"

        parameters = {"apikey": self.apikeys,
                      "application": encode(application),
                      "priority": priority}

        if event:
            parameters["event"] = encode(event)

        if description:
            parameters["description"] = description

            parameters["description"] = encode(description)

        if self.providerkey:
            parameters["providerkey"] = self.providerkey

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        connection = HTTPSConnection(self.host, self.port)
        connection.request("POST", "/publicapi/add",
                                urlencode(parameters), headers)

        response = connection.getresponse()
        connection.close()
        msg = "Sent: %s" % ( description )
        try:
            self._parse_response(response)
        except Exception, error:
            msg = "Could not send message: %s" % (error)
예제 #7
0
파일: CMSWeb.py 프로젝트: dmitrijus/cms-bot
class CMSWeb (object):
  def __init__ (self):
    self.URL_CMSWEB_BASE='cmsweb.cern.ch'
    self.URL_PHEDEX_BLOCKREPLICAS='/phedex/datasvc/json/prod/blockreplicas'
    self.URL_DBS_DATASETS='/dbs/prod/global/DBSReader/datasets'
    self.URL_DBS_FILES='/dbs/prod/global/DBSReader/files'
    self.URL_DBS_RUNS='/dbs/prod/global/DBSReader/runs'
    self.URL_DBS_BLOCKS='/dbs/prod/global/DBSReader/blocks'
    self.conn = HTTPSConnection(self.URL_CMSWEB_BASE, cert_file='/tmp/x509up_u{0}'.format(getuid()),  timeout=30)
    self.reply_cache = {}
    self.last_result = ""
    self.errors = 0

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

  def get_cmsweb_data(self, url):
    self.last_result = url
    if url in self.reply_cache: return True, self.reply_cache[url]
    msg =""
    try:
      self.conn.request('GET', url)
      msg = self.conn.getresponse()
      if msg.status!=200:
        self.errors = self.errors + 1
        print 'Result: {0} {1}: {2}'.format(msg.status, msg.reason, url)
        return False, {}
      self.reply_cache[url]=json.loads(msg.read())
      return True, self.reply_cache[url]
    except Exception, e:
      print "Error:", e, url
      self.errors = self.errors + 1
      return False, {}
def get_benchmark():
    conn = HTTPSConnection(host)
    conn.request("GET", "/15/benchmark/" + socket.gethostname())
    response = conn.getresponse()
    benchmark = json.loads(response.read(9999))
    conn.close()
    return benchmark
예제 #9
0
    def oauth_callback(self, params):

        if 'code' not in params:
            return None

        conn = HTTPSConnection("accounts.google.com")
        body = tools.encode_params({
            "grant_type":"authorization_code",
            "code":params['code'],
            "client_id":secrets.GMAIL_CLIENT_ID,
            "client_secret":secrets.GMAIL_CLIENT_SECRET,
            "redirect_uri":secrets.BASE_REDIRECT_URL + "gmail",
        })
        headers = {
            "Content-Type":"application/x-www-form-urlencoded",
        }
        conn.request("POST", "/o/oauth2/token", body, headers)

        resp = conn.getresponse()
        try:
            self.token = json.loads(resp.read())['access_token']
            self.is_auth = True
        except (KeyError, ValueError):
            return None

        conn.close()

        conn = HTTPSConnection("www.googleapis.com")
        conn.request("GET","/oauth2/v1/tokeninfo?alt=json&access_token="+self.token,"",{})
        resp = conn.getresponse()
        self.username = json.loads(resp.read())['email']
예제 #10
0
def getsid():
	"""Connetti al cpanel per ottenere l'id di sessione"""
	# "Basic" authentication encodes userid:password in base64. Note
	# that base64.encodestring adds some extra newlines/carriage-returns
	# to the end of the result. string.strip is a simple way to remove
	# these characters.
        print " - Fetching session id from CPanel for user %s" % userid
	global sid,psi
	auth = 'Basic ' + strip(encodestring(userid + ':' + passwd))
	conn = HTTPSConnection('cp.tophost.it')
	conn.putrequest('GET', '/dnsjump.php')
	conn.putheader('Authorization', auth )
	conn.putheader(ua[0],ua[1])
	conn.endheaders()
	r=conn.getresponse()
	if r.status!=200:
		print 'Connessione fallita: ',r.status,r.reason
		exit(1)
	psi=r.getheader('Set-Cookie').replace('PHPSESSID=','').replace('; path=/','')
	page=r.read()
	bound='<input type="hidden" name="sid" value="'
	s=page.find(bound); e=page.find('">',s)
	sid=page[s+len(bound):e]
	conn.close()
	return sid,psi
예제 #11
0
파일: dnscom.py 프로젝트: NewFuture/DNSPOD
def request(action, param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)
    params = signature(params)
    logger.info("%s : params:%s", action, params)

    if PROXY:
        conn = HTTPSConnection(PROXY)
        conn.set_tunnel(API_SITE, 443)
    else:
        conn = HTTPSConnection(API_SITE)

    conn.request(API_METHOD, '/api/' + action + '/', urllib.urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    result = response.read()
    conn.close()

    if response.status < 200 or response.status >= 300:
        raise Exception(result)
    else:
        data = json.loads(result.decode('utf8'))
        logger.debug('%s : result:%s', action, data)
        if data.get('code') != 0:
            raise Exception("api error:", data.get('message'))
        data = data.get('data')
        if data is None:
            raise Exception('response data is none')
        return data
예제 #12
0
파일: alidns.py 프로젝트: NewFuture/DNSPOD
def request(param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)
    params = signature(params)
    logger.debug("params:%s", params)

    if PROXY:
        conn = HTTPSConnection(PROXY)
        conn.set_tunnel(API_SITE, 443)
    else:
        conn = HTTPSConnection(API_SITE)

    conn.request(API_METHOD, '/', urllib.urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    data = response.read()
    conn.close()

    if response.status < 200 or response.status >= 300:
        logger.warn('%s : error:%s', params['Action'], data)       
        raise Exception(data)
    else:
        data = json.loads(data.decode('utf8'))
        logger.debug('%s : result:%s', params['Action'], data)
        return data
예제 #13
0
def get_update_time_5(site):
	# TO-DO: merge this function into get_update_time_2

	global HTTP_TIMEOUT, PAGE_SIZE

	# get site parameters
	host, path, type, tag, ssl = parse_url(site.url)

	httpcon = HTTPSConnection(host)
	httpcon.request('GET', path, {}, {})
	res = httpcon.getresponse()

	# look for tag in the first 4096 bytes
	body = res.read()
	index = body.find("</published><updated>")

	# TO-DO: look at the remaining bytes if necessary

	# if tag found, look for close tag
	if ( index >= 0 ):
		close_tag = "</updated>"
		close_tag_index = index + body[index:].find(close_tag)

	# if close tag found, get time representation
	if ( close_tag_index >= 0 ):
		time_str = body[index+len("</published><updated>"):close_tag_index]

		# parse time string and add 32400 secs to make JST
		time_update_uct = time.strptime(time_str[0:19], '%Y-%m-%dT%H:%M:%S')
		time_update = time.localtime(time.mktime(time_update_uct) + 32400)

		update_update_time(site, time_update)

	httpcon.close()
예제 #14
0
def getBestServer(servers):
	results = {}
	for server in servers:
		cum = []
		url = '%s/latency.txt' % os.path.dirname(server['url'])
		urlparts = urlparse(url)
		for i in range(0, 3):
			try:
				if urlparts[0] == 'https':
					h = HTTPSConnection(urlparts[1])
				else:
					h = HTTPConnection(urlparts[1])
				headers = {'User-Agent': user_agent}
				start = timeit.default_timer()
				h.request('GET', urlparts[2], headers=headers)
				r = h.getresponse()
				total = timeit.default_timer() - start
			except (HTTPError, URLError, socket.error):
				cum.append(3600)
				continue
			text = r.read(9)
			if int(r.status) == 200 and text == 'test=test'.encode():
				cum.append(total)
			else:
				cum.append(3600)
			h.close()
		avg = round(sum(cum) / 6 * 1000, 3)
		results[avg] = server
	fastest = sorted(results.keys())[0]
	best = results[fastest]
	best['latency'] = fastest
	return best
예제 #15
0
def get_latency(server):
    cum = []
    url = '%s/latency.txt' % os.path.dirname(server['url'])
    urlparts = urlparse(url)
    for i in range(0, 3):
        try:
            if urlparts[0] == 'https':
                h = HTTPSConnection(urlparts[1])
            else:
                h = HTTPConnection(urlparts[1])
            headers = {'User-Agent': user_agent}
            start = timeit.default_timer()
            h.request("GET", urlparts[2], headers=headers)
            r = h.getresponse()
            total = (timeit.default_timer() - start)
        except (HTTPError, URLError, socket.error):
            cum.append(3600)
            continue
        text = r.read(9)
        if int(r.status) == 200 and text == 'test=test'.encode():
            cum.append(total)
        else:
            cum.append(3600)
        h.close()
    avg = round((sum(cum) / 6) * 1000, 3)
    return avg
예제 #16
0
파일: dnspod.py 프로젝트: NewFuture/DNSPOD
def request(action, param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)

    params.update({'login_token': '***', 'format': 'json'})
    logger.info("%s : params:%s", action, params)
    params['login_token'] = "%s,%s" % (ID, TOKEN)

    if PROXY:
        conn = HTTPSConnection(PROXY)
        conn.set_tunnel(API_SITE, 443)
    else:
        conn = HTTPSConnection(API_SITE)

    conn.request(API_METHOD, '/' + action, urllib.urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    res = response.read()
    conn.close()

    if response.status < 200 or response.status >= 300:
        logger.warn('%s : error:%s', action, res) 
        raise Exception(res)
    else:
        data = json.loads(res.decode('utf8'))
        logger.debug('%s : result:%s', action, data)
        if not data:
            raise Exception("empty response")
        elif data.get("status", {}).get("code") == "1":
            return data
        else:
            raise Exception(data.get('status', {}))
예제 #17
0
파일: iloot.py 프로젝트: BlackBorland/iloot
def probobuf_request(host, method, url, body, headers, msg=None):
    conn = HTTPSConnection(host)
    sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address)
    conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
    request = conn.request(method, url, body, headers)
    response = conn.getresponse()
    length = response.getheader("content-length")

    if length is None:
        length = 0
    else:
        length = int(length)

    data = response.read()

    while len(data) < length:
        d = response.read()
        data += d

    conn.close()
    if msg == None:
        return data

    res = msg()
    res.ParseFromString(data)
    return res
예제 #18
0
def RechLatencySrv(servers):
    Tab = {}
    for SRV in servers:
        ListTempsDeReponse = []
        UrlEnCour = '%s/latency.txt' % os.path.dirname(SRV['url'])
        UrlCouper = urlparse(UrlEnCour)
        for i in range(0, 3):
            try:
                if UrlCouper[0] == 'https':
                    ConnectionUrl = HTTPSConnection(UrlCouper[1])
                else:
                    ConnectionUrl = HTTPConnection(UrlCouper[1])
                ConnectionHead = {'User-Agent': UserAgent}
                TempsDeRef = timeit.default_timer()
                ConnectionUrl.request("GET",
                                      UrlCouper[2],
                                      headers=ConnectionHead)
                RepConnectEnCour = ConnectionUrl.getresponse()
                TempsDeReponse = (timeit.default_timer() - TempsDeRef)
            except (HTTPError, URLError, socket.error):
                ListTempsDeReponse.append(3600)
                continue
            TxtPageLu = RepConnectEnCour.read(9)
            if int(RepConnectEnCour.status
                   ) == 200 and TxtPageLu == 'test=test'.encode():
                ListTempsDeReponse.append(TempsDeReponse)
            else:
                ListTempsDeReponse.append(3600)
            ConnectionUrl.close()
        ArondTempsRep = round((sum(ListTempsDeReponse) / 6) * 1000, 3)
        Tab[ArondTempsRep] = SRV
    ListSrvTrier = sorted(Tab.keys())[0]
    ListeRetLatency = Tab[ListSrvTrier]
    ListeRetLatency['latency'] = ListSrvTrier
    return ListeRetLatency
예제 #19
0
def request(param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)

    params.update({API.TOKEN_PARAM: '***'})
    info("%s/%s : %s", API.SITE, API.ACTION, params)
    params[API.TOKEN_PARAM] = Config.TOKEN

    if Config.PROXY:
        conn = HTTPSConnection(Config.PROXY)
        conn.set_tunnel(API.SITE, 443)
    else:
        conn = HTTPSConnection(API.SITE)

    conn.request(API.METHOD, '/' + API.ACTION, urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    res = response.read().decode('utf8')
    conn.close()

    if response.status < 200 or response.status >= 300:
        warning('%s : error[%d]:%s', API.ACTION, response.status, res)
        raise Exception(res)
    else:
        debug('%s : result:%s', API.ACTION, res)
        if not res:
            raise Exception("empty response")
        elif res[:5] == "nochg" or res[:4] == "good":  # No change or success
            return res
        else:
            raise Exception(res)
예제 #20
0
def send_request(server,
                 method,
                 path,
                 params,
                 secure=True,
                 accept_type='text/plain',
                 basic_auth=None,
                 ssl_skip_verify=False):
    headers = {
        'Content-type': 'application/x-www-form-urlencoded',
        'Accept': accept_type,
    }
    if basic_auth:
        headers['Authorization'] = 'Basic %s' % basic_auth
    try:
        params = urllib.urlencode(params)
    except:  # python 3
        params = urllib.parse.urlencode(params)
    if secure:
        if ssl_skip_verify:
            conn = HTTPSConnection(server,
                                   context=ssl._create_unverified_context())
        else:
            conn = HTTPSConnection(server)
    else:
        conn = HTTPConnection(server)
    conn.request(method, path, params, headers)
    response = conn.getresponse()
    data = response.read()
    conn.close()
    return (response.status, response.reason, data)
예제 #21
0
 def request(self, domain, path, method, data=None):
     if self.proxy_ip and self.proxy_port:
         connection = HTTPSConnection(self.proxy_ip, self.proxy_port, timeout=self.request_timeout)
         connection.set_tunnel(domain)
     else:
         connection = HTTPSConnection(domain, timeout=self.request_timeout)
     response_content = None
     try:
         headers = {'User-Agent': self.user_agent}
         if self.proxy_user and self.proxy_pass:
             base64_bytes = b64encode(
                 ("%s:%s" % (self.proxy_user, self.proxy_pass)).encode("ascii")
             ).decode("ascii")
             headers['Authorization'] = 'Basic %s' % base64_bytes
         connection.request(method, path, headers=headers, body=data)
         response = connection.getresponse()
         # In python3, defaultencoding is utf-8.
         # In python2, defaultencoding is ascii.
         response_content = response.read()
         try:
             # JSON
             return json.loads(response_content.decode('utf-8'))
         except:
             # XML
             return ET.fromstring(response_content)
     except ValueError as e:
         LOGGER.error("Value error (%s) in response_content: %s", e, response_content)
         raise APIResponseException(response_content)
     except (socket.timeout, ssl.SSLError) as e:
         LOGGER.error("Timed out request on %s, %s: %s", method, path, e)
         raise APIRequestTimedOutException("Timed out request: %s" % e)
     finally:
         connection.close()
예제 #22
0
    def _conectar_servidor(self, xml, service, xsd_retorno, test=False):
        server = test and self.servidor_homologacao or self.servidor

        caminho_temporario = u'/tmp/'
        key_file = caminho_temporario + uuid4().hex
        arq_tmp = open(key_file, 'w')
        arq_tmp.write(self._certificado.chave)
        arq_tmp.close()

        cert_file = caminho_temporario + uuid4().hex
        arq_tmp = open(cert_file, 'w')
        arq_tmp.write(self._certificado.certificado)
        arq_tmp.close()

        xml = self._soap(xml, service)

        connection = HTTPSConnection(server, key_file=key_file, cert_file=cert_file)
        result = self._soap_post(connection, xml, xsd_retorno, service)

        sucesso, erros, alertas = self._parse_result(result)

        if self._destino:
            arq = open(self._destino + '-rec.xml', 'w')
            arq.write(resp_xml_str.encode(u'utf-8'))
            arq.close()

        os.remove(key_file)
        os.remove(cert_file)
        connection.close()

        return (sucesso, result, alertas, erros)
예제 #23
0
def get_latency(server):
    cum = []
    url = '%s/latency.txt' % os.path.dirname(server['url'])
    urlparts = urlparse(url)
    for i in range(0, 3):
        try:
            if urlparts[0] == 'https':
                h = HTTPSConnection(urlparts[1])
            else:
                h = HTTPConnection(urlparts[1])
            headers = {'User-Agent': user_agent}
            start = timeit.default_timer()
            h.request("GET", urlparts[2], headers=headers)
            r = h.getresponse()
            total = (timeit.default_timer() - start)
        except (HTTPError, URLError, socket.error):
            cum.append(3600)
            continue
        text = r.read(9)
        if int(r.status) == 200 and text == 'test=test'.encode():
            cum.append(total)
        else:
            cum.append(3600)
        h.close()
    avg = round((sum(cum) / 6) * 1000, 3)
    return avg
예제 #24
0
def request(param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)
    params = signature(params)
    info("%s: %s", API.SITE, params)

    if Config.PROXY:
        conn = HTTPSConnection(Config.PROXY)
        conn.set_tunnel(API.SITE, 443)
    else:
        conn = HTTPSConnection(API.SITE)
    conn.request(API.METHOD, '/', urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    data = response.read().decode('utf8')
    conn.close()

    if response.status < 200 or response.status >= 300:
        warning('%s : error[%d]: %s', params['Action'], response.status, data)
        raise Exception(data)
    else:
        data = jsondecode(data)
        debug('%s : result:%s', params['Action'], data)
        return data
예제 #25
0
파일: dnspod.py 프로젝트: loftor-git/DDNS
def request(action, param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)

    params.update({'login_token': "%s,%s" % (ID, TOKEN), 'format': 'json'})
    log.debug("%s : params:%s", action, params)

    if PROXY:
        conn = HTTPSConnection(PROXY)
        conn.set_tunnel(API_SITE, 443)
    else:
        conn = HTTPSConnection(API_SITE)

    conn.request(API_METHOD, '/' + action, urllib.urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    res = response.read()
    conn.close()

    if response.status < 200 or response.status >= 300:
        raise Exception(res)
    else:
        data = json.loads(res.decode('utf8'))
        if not data:
            raise Exception("empty response")
        elif data.get("status", {}).get("code") == "1":
            return data
        else:
            raise Exception(data.get('status', {}))
예제 #26
0
def fetch_json(uri_path, http_method='GET', headers={}, query_params={},
               post_args={}):
    """
    Fetch some JSON from Trello
    """

    if http_method in ("POST", "PUT", "DELETE"):
        headers['Content-type'] = 'application/json'

    headers['Accept'] = 'application/json'
    domain, url, urlparams = build_url(uri_path, query_params)
    http_client = HTTPSConnection(domain)
    url = url + urlparams
    url = url.replace(' ', '')
    if http_method == "GET":
        http_client.request(http_method, url)
    elif http_method == "POST":
        http_client.request(http_method, url, json.dumps(post_args), headers)

    response = http_client.getresponse()
    content = response.read()
    http_client.close()

    # error checking
    if response.status == 401:
        raise Unauthorized(url, response)
    if response.status != 200:
        raise ResourceUnavailable(url, response)
    return json.loads(content)
예제 #27
0
파일: cp2zim.py 프로젝트: bensnyde/cp2zim
def http_query(url, port, username, password, querystr):
    conn = HTTPSConnection(url, port)
    conn.request('GET', querystr, headers={'Authorization':'Basic ' + b64encode(username+':'+password).decode('ascii')})
    response = conn.getresponse()
    data = response.read()
    conn.close()
    return data
예제 #28
0
    def send_request(self, message, use_ssl=False, issoap=False, parse=False):
        """Send the message to the SOAP server.

        Arguments:
        message: message string
        use_ssl: if set secure HTTPS connection will be used
        issoap: if not set the client will add a SOAP header to the
                message.
        parse: if true an object of etree Element with be returned
        """

        if use_ssl:
            conn = HTTPSConnection(self.host, self.port, self.key, self.cert)
        else:
            conn = HTTPConnection(self.host, self.port)

        conn.connect()
        headers = {'SOAPAction': '', 'Content-Type': 'text/xml'}
        if issoap == False:
            message = SoapClient.soapify_message(message)
        conn.request('POST', self.uri, message, headers)
        resp = conn.getresponse()
        response = resp.read()
        conn.close()
        
        tree = etree.fromstring(response)

        message = tree.xpath('soap:Body/nmwg:message', \
                         namespaces={'soap': \
                                     'http://schemas.xmlsoap.org/soap/envelope/', \
                                 'nmwg': 'http://ggf.org/ns/nmwg/base/2.0/'})
        if parse is True:
            return message[0]
        else:
            return etree.tostring(message[0], pretty_print=True)
예제 #29
0
def request(param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)
    params = signature(params)
    logger.debug("params:%s", params)

    if PROXY:
        conn = HTTPSConnection(PROXY)
        conn.set_tunnel(API_SITE, 443)
    else:
        conn = HTTPSConnection(API_SITE)

    conn.request(API_METHOD, '/', urllib.urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    data = response.read()
    conn.close()

    if response.status < 200 or response.status >= 300:
        logger.warn('%s : error:%s', action, data)
        raise Exception(data)
    else:
        data = json.loads(data.decode('utf8'))
        logger.debug('%s : result:%s', action, data)
        return data
예제 #30
0
def get_installations(integration):
    # FIXME(sileht): Need to be in github libs
    conn = HTTPSConnection("api.github.com")
    conn.request(
        method="GET",
        url="/app/installations",
        headers={
            "Authorization": "Bearer {}".format(integration.create_jwt()),
            "Accept": "application/vnd.github.machine-man-preview+json",
            "User-Agent": "PyGithub/Python"
        },
    )
    response = conn.getresponse()
    response_text = response.read()
    if six.PY3:
        response_text = response_text.decode('utf-8')
    conn.close()

    if response.status == 200:
        return ujson.loads(response_text)
    elif response.status == 403:
        raise GithubException.BadCredentialsException(status=response.status,
                                                      data=response_text)
    elif response.status == 404:
        raise GithubException.UnknownObjectException(status=response.status,
                                                     data=response_text)
    raise GithubException.GithubException(status=response.status,
                                          data=response_text)
예제 #31
0
def request(action, param=None, **params):
    """
    发送请求数据
    """
    if param:
        params.update(param)
    params = signature(params)
    info("%s : params:%s", action, params)

    if PROXY:
        conn = HTTPSConnection(PROXY)
        conn.set_tunnel(API_SITE, 443)
    else:
        conn = HTTPSConnection(API_SITE)

    conn.request(API_METHOD, '/api/' + action + '/', urlencode(params),
                 {"Content-type": "application/x-www-form-urlencoded"})
    response = conn.getresponse()
    result = response.read()
    conn.close()

    if response.status < 200 or response.status >= 300:
        raise Exception(result)
    else:
        data = jsondecode(result.decode('utf8'))
        debug('%s : result:%s', action, data)
        if data.get('code') != 0:
            raise Exception("api error:", data.get('message'))
        data = data.get('data')
        if data is None:
            raise Exception('response data is none')
        return data
예제 #32
0
    def _conectar_servidor(self, xml, service, xsd_retorno, test=False):
        server = test and self.servidor_homologacao or self.servidor

        caminho_temporario = u'/tmp/'
        key_file = caminho_temporario + uuid4().hex
        arq_tmp = open(key_file, 'w')
        arq_tmp.write(self._certificado.chave)
        arq_tmp.close()

        cert_file = caminho_temporario + uuid4().hex
        arq_tmp = open(cert_file, 'w')
        arq_tmp.write(self._certificado.certificado)
        arq_tmp.close()

        xml = self._soap(xml, service)

        connection = HTTPSConnection(server,
                                     key_file=key_file,
                                     cert_file=cert_file)
        result = self._soap_post(connection, xml, xsd_retorno, service)

        sucesso, erros, alertas = self._parse_result(result)

        if self._destino:
            arq = open(self._destino + '-rec.xml', 'w')
            arq.write(resp_xml_str.encode(u'utf-8'))
            arq.close()

        os.remove(key_file)
        os.remove(cert_file)
        connection.close()

        return (sucesso, result, alertas, erros)
예제 #33
0
파일: ircbot.py 프로젝트: Nitri0/tribus
def get_bug_information(bug_ids):
	bug_information = []
	http_connection = HTTPSConnection('bugs.launchpad.net')
	for bug_id in bug_ids:
		http_connection.request('GET', '/bugs/' + bug_id + '/+text')
		http_response = http_connection.getresponse()
		if http_response.status != 200:
			print "Error occured when fetching data"
			continue
		data = http_response.read()
		data = data.split('\n')
		bug = {}
		bug_title = bug_task = bug_status = bug_importance = None
		for line in data:
			if line.startswith('title:'):
				bug_title = line.split(' ', 1)[1]
			elif line.startswith('task:'):
				bug_task = line.split(' ', 1)[1]
			elif line.startswith('status:'):
				bug_status = line.split(' ', 1)[1]
			elif line.startswith('importance:'):
				bug_importance = line.split(' ', 1)[1]
			elif line.startswith('Content-Type'): break
		bug['id'] = bug_id
		bug['title'] = bug_title
		bug['task'] = bug_task
		bug['status'] = bug_status
		bug['importance'] = bug_importance
		bug_information.append(bug)
	http_connection.close()
	return bug_information
예제 #34
0
class Client:
    httpClient = None

    def __init__(self, token):
        self.token = token
        self.httpClient = HTTPSConnection(vs.HTTP_URL, vs.HTTP_PORT)

    def __del__(self):
        if self.httpClient is not None:
            self.httpClient.close()

    def encodepath(self, path):
        start = 0
        n = len(path)
        re = ''
        i = path.find('=', start)
        while i != -1:
            re += path[start:i + 1]
            start = i + 1
            i = path.find('&', start)
            if (i >= 0):
                for j in range(start, i):
                    if (path[j] > '~'):
                        re += urllib.quote(path[j])
                    else:
                        re += path[j]
                re += '&'
                start = i + 1
            else:
                for j in range(start, n):
                    if (path[j] > '~'):
                        re += urllib.quote(path[j])
                    else:
                        re += path[j]
                start = n
            i = path.find('=', start)
        return re

    def init(self, token):
        self.token = token

    def getData(self, path):
        result = None
        path = '/data/v1' + path
        path = self.encodepath(path)
        try:
            self.httpClient.request(
                'GET', path, headers={"Authorization": "Bearer " + self.token})
            response = self.httpClient.getresponse()
            if response.status == vs.HTTP_OK:
                result = response.read()
            else:
                result = response.read()
            if (path.find('.csv?') != -1):
                result = result.decode('GBK').encode('utf-8')
            return response.status, result
        except Exception as e:
            raise e
        return -1, result
예제 #35
0
def get_product_details(asin):
    url = '/api/v1/asin/' + asin
    conn = HTTPSConnection('api.barcodable.com')
    conn.request('GET', url)
    response = conn.getresponse()
    data = response.read()
    conn.close()
    return data
def get_test():
    conn = HTTPSConnection(host)
    conn.request("GET", "/15/test")
    response = conn.getresponse()
    test = json.loads(response.read(9999))
    conn.close()
    print_test(test)
    return test
예제 #37
0
def login(app, redirect):
    url = LOGIN_URL % (quote(app, ''), quote(redirect, ''))
    con = HTTPSConnection(LOGIN_HOST, LOGIN_PORT, cert_file = CERT_FILE)
    con.request('GET', url, headers = DEFAULT_HEADERS)
    res = con.getresponse()
    location = res.getheader('Location')
    con.close()
    return location
예제 #38
0
def login(app, redirect):
    url = LOGIN_URL % (quote(app, ''), quote(redirect, ''))
    con = HTTPSConnection(LOGIN_HOST, LOGIN_PORT, cert_file=CERT_FILE)
    con.request('GET', url, headers=DEFAULT_HEADERS)
    res = con.getresponse()
    location = res.getheader('Location')
    con.close()
    return location
def post_result(result):
    conn = HTTPSConnection(host)
    conn.request("POST", "/15/submit", json.dumps(result))
    response = conn.getresponse()
    test = json.loads(response.read(9999))
    conn.close()
    print_test(test)
    return test
def get_weights():
    conn = HTTPSConnection(host)
    conn.request("GET", "/15/weights")
    response = conn.getresponse()
    weights = json.loads(response.read(9999))
    conn.close()
    print("Fetched weights from server: " + str(weights))
    return weights
예제 #41
0
파일: hetzner.py 프로젝트: pajju/hetzner-1
class RobotConnection(object):
    def __init__(self, user, passwd):
        self.user = user
        self.passwd = passwd
        self.conn = HTTPSConnection(ROBOT_HOST)

    def _request(self, method, path, data, headers, retry=1):
        self.conn.request(method.upper(), path, data, headers)
        try:
            return self.conn.getresponse()
        except BadStatusLine:
            # XXX: Sometimes, the API server seems to have a problem with
            # keepalives.
            if retry <= 0:
                raise

            self.conn.close()
            self.conn.connect()
            return self._request(method, path, data, headers, retry - 1)

    def request(self, method, path, data=None):
        if data is not None:
            data = urlencode(data)

        auth = 'Basic {0}'.format(
            b64encode("{0}:{1}".format(self.user, self.passwd))
        )

        headers = {'Authorization': auth}

        if data is not None:
            headers['Content-Type'] = 'application/x-www-form-urlencoded'

        response = self._request(method, path, data, headers)
        data = json.loads(response.read())

        if 200 <= response.status < 300:
            return data
        else:
            error = data.get('error', None)
            if error is None:
                raise RobotError("Unknown error: {0}".format(data))
            else:
                err = "{0} - {1}".format(error['status'], error['message'])
                if error['missing'] is not None:
                    err += ", missing input: {0}".format(
                        ', '.join(error['missing'])
                    )
                if error['invalid'] is not None:
                    err += ", invalid input: {0}".format(
                        ', '.join(error['invalid'])
                    )
                raise RobotError(err)

    get = lambda s, p: s.request('GET', p)
    post = lambda s, p, d: s.request('POST', p, d)
    put = lambda s, p, d: s.request('PUT', p, d)
    delete = lambda s, p, d: s.request('DELETE', p, d)
예제 #42
0
        def get_best_server(self, servers=None):
            """Perform a speedtest.net "ping" to determine which speedtest.net
            server has the lowest latency
            """

            if not servers:
                if not self.closest:
                    servers = self.get_closest_servers()
                servers = self.closest

            results = {}
            for server in servers:
                cum = []
                url = os.path.dirname(server['url'])
                urlparts = urlparse('%s/latency.txt' % url)
                printer('%s %s/latency.txt' % ('GET', url), debug=True)
                for _ in range(0, 3):
                    try:
                        if urlparts[0] == 'https':
                            h = HTTPSConnection(urlparts[1])
                        else:
                            h = HTTPConnection(urlparts[1])
                        headers = {'User-Agent': USER_AGENT}
                        start = timeit.default_timer()
                        h.request("GET", urlparts[2], headers=headers)
                        r = h.getresponse()
                        total = (timeit.default_timer() - start)
                    except HTTP_ERRORS:
                        e = get_exception()
                        printer('%r' % e, debug=True)
                        cum.append(3600)
                        continue

                    text = r.read(9)
                    if int(r.status) == 200 and text == 'test=test'.encode():
                        cum.append(total)
                    else:
                        cum.append(3600)
                    h.close()

                avg = round((sum(cum) / 6) * 1000.0, 3)
                results[avg] = server

            try:
                fastest = sorted(results.keys())[0]
            except IndexError:
                raise SpeedtestBestServerFailure(
                    'Unable to connect to servers to '
                    'test latency.')
            best = results[fastest]
            best['latency'] = fastest

            self.results.ping = fastest
            self.results.server = best

            self.best.update(best)
            printer(best, debug=True)
            return best
예제 #43
0
def do_query(query):
    """executes webapi query"""

    conn = HTTPSConnection(infoblox_host)
    auth_header = 'Basic %s' % (':'.join([username, password]).encode('Base64').strip('\r\n'))
    conn.request(query[0], '/wapi/v1.4/' + query[1], query[2], {'Authorization': auth_header, 'Content-Type': 'application/x-www-form-urlencoded'})
    result = json.loads(conn.getresponse().read())
    conn.close()
    return result
예제 #44
0
    def get_best_server(self, servers=None):
        """Perform a speedtest.net "ping" to determine which speedtest.net
        server has the lowest latency
        """

        if not servers:
            if not self.closest:
                servers = self.get_closest_servers()
            servers = self.closest

        results = {}
        for server in servers:
            cum = []
            url = os.path.dirname(server['url'])
            urlparts = urlparse('%s/latency.txt' % url)
            printer('%s %s/latency.txt' % ('GET', url), debug=True)
            for _ in range(0, 3):
                try:
                    if urlparts[0] == 'https':
                        h = HTTPSConnection(urlparts[1])
                    else:
                        h = HTTPConnection(urlparts[1])
                    headers = {'User-Agent': USER_AGENT}
                    start = timeit.default_timer()
                    h.request("GET", urlparts[2], headers=headers)
                    r = h.getresponse()
                    total = (timeit.default_timer() - start)
                except HTTP_ERRORS:
                    e = get_exception()
                    printer('%r' % e, debug=True)
                    cum.append(3600)
                    continue

                text = r.read(9)
                if int(r.status) == 200 and text == 'test=test'.encode():
                    cum.append(total)
                else:
                    cum.append(3600)
                h.close()

            avg = round((sum(cum) / 6) * 1000.0, 3)
            results[avg] = server

        try:
            fastest = sorted(results.keys())[0]
        except IndexError:
            raise SpeedtestBestServerFailure('Unable to connect to servers to '
                                             'test latency.')
        best = results[fastest]
        best['latency'] = fastest

        self.results.ping = fastest
        self.results.server = best

        self.best.update(best)
        printer(best, debug=True)
        return best
예제 #45
0
파일: api.py 프로젝트: AHAMED750/reddit
 def make_request(self):
     u = urlparse(g.authorizenetapi)
     conn = HTTPSConnection(u.hostname, u.port)
     conn.request("POST", u.path, self.toXML().encode('utf-8'),
                  {"Content-type": "text/xml"})
     res = conn.getresponse()
     res = self.handle_response(res.read())
     conn.close()
     return res
예제 #46
0
파일: main.py 프로젝트: isaach/kivy-scrumy
 def delete(self, instance):
     if self.selectedSticky.parent is not None:
         conn = HTTPSConnection('scrumy.com')
         conn.request('DELETE', '/api/tasks/%s' % self.selectedSticky.task.id, headers=self.headers)
         response = conn.getresponse()
         conn.close()
         if response.status != 200:
             print('Error: Delete response was %s %s' % (response.status, response.reason))
             return
         self.refresh()
예제 #47
0
 def make_request(self):
     u = urlparse(g.authorizenetapi)
     conn = HTTPSConnection(u.hostname, u.port)
     conn.request("POST", u.path,
                  self.toXML().encode('utf-8'),
                  {"Content-type": "text/xml"})
     res = conn.getresponse()
     res = self.handle_response(res.read())
     conn.close()
     return res
예제 #48
0
파일: network.py 프로젝트: shoosen/ibid
    def _request(self, url, method):
        scheme, host = urlparse(url)[:2]
        scheme = scheme.lower()
        proxies = getproxies_environment()
        if scheme in proxies:
            scheme, host = urlparse(proxies[scheme])[:2]
            scheme = scheme.lower()

        kwargs = {}
        if version_info[1] >= 6:
            kwargs['timeout'] = self.timeout
        else:
            socket.setdefaulttimeout(self.timeout)

        if scheme == "https":
            conn = HTTPSConnection(host, **kwargs)
        else:
            conn = HTTPConnection(host, **kwargs)

        headers = {}
        if method == 'GET':
            headers['Range'] = 'bytes=0-%s' % self.max_size

        try:
            try:
                conn.request(method.upper(),
                             url_to_bytestring(url),
                             headers=headers)
                response = conn.getresponse()
                data = response.read(self.max_size)
                conn.close()
            except socket.error, e:
                raise HTTPException(e.message or e.args[1])
        finally:
            if version_info[1] < 6:
                socket.setdefaulttimeout(None)

        contenttype = response.getheader('Content-Type', None)
        if contenttype:
            match = re.search('^charset=([a-zA-Z0-9-]+)', contenttype)
            try:
                if match:
                    data = data.decode(match.group(1))
                elif contenttype.startswith('text/'):
                    data = data.decode('utf-8')
            except UnicodeDecodeError:
                guessed = detect(data)
                if guessed['confidence'] > 0.5:
                    charset = guessed['encoding']
                    # Common guessing mistake:
                    if charset.startswith('ISO-8859') and '\x92' in data:
                        charset = 'windows-1252'
                    data = unicode(data, charset, errors='replace')

        return response.status, response.reason, data, response.getheaders()
예제 #49
0
  def authenticate(self):
    logging.info("Authenticating to Cloud GUI")
    connection = HTTPSConnection(
      self.host, context=ssl._create_unverified_context())
    connection.connect()

    creds = {"username": self.username, "password": self.password}
    auth_url = "%s/signIn.py" % self.base_url
    session = self._run_query(connection, auth_url, "POST", creds)
    self.session_id = session["session_id"]
    connection.close()
예제 #50
0
파일: api.py 프로젝트: nitzle/reddit
 def make_request(self):
     u = urlparse(g.authorizenetapi)
     try:
         conn = HTTPSConnection(u.hostname, u.port)
         conn.request("POST", u.path, self.toXML(), {"Content-type": "text/xml"})
         res = conn.getresponse()
         res = self.handle_response(res.read())
         conn.close()
         return res
     except socket.error:
         return False
예제 #51
0
def requestHarvest(day_of_year, year):
    conn = HTTPSConnection(host)
    conn.request("GET", "/daily/%d/%d" % (day_of_year, year), None,
                 harvestApiHeaders)
    raw_response = conn.getresponse()
    body = raw_response.read().decode('utf-8')
    conn.close()
    if raw_response.status > 206:
        print("Unexpected response %d, %s" % (raw_response.status, body))
        exit()
    return json.loads(body)
예제 #52
0
def sendMessage(id, msgStr):
    try:
        conn = HTTPSConnection(url)
        conn.request(
            "GET", bot + "sendMessage?chat_id=" + str(id) + "&text=" + msgStr +
            "&disable_web_page_preview=true")
        res = conn.getresponse().read()
        conn.close()
    except:
        print "Msg send fail"
        return
예제 #53
0
def get_website_source(add=''):
    if add == '':
        raise NameError('Have no website address yet')
    conn = HTTPSConnection(add)
    conn.request('GET', '/the-thao-c101.html')
    r = conn.getresponse()
    if str(r.status) == '200':
        return r.read()
    else:
        return ''
    conn.close()
예제 #54
0
    def _do_request(self, http_method, path, body=None, parameters=None):
        """
        Generic HTTP/S connection method.

        """

        full_path = self._session_base_path + path

        #If any parameters were given, tack them on to the end of the path.
        if parameters:
            full_path += '?' + urlencode(parameters)

        conn = None
        response = None
        try:
            #TO DO: Rewrite to facilitate connection pooling.
            if self._secure:
                conn = HTTPSConnection(self._host)
            else:
                conn = HTTPConnection(self._host)

            try:
                conn.request(http_method, full_path, body, self._headers)
            except:
                raise TembooError(
                    'An error occurred connecting to the Temboo server. Verify that your Temboo Account Name is correct, and that you have a functioning network connection'
                )

            response = conn.getresponse()
            body = response.read().decode('utf-8')

            #Any 200-series response means success.
            if 200 <= response.status < 300:
                return json.loads(body)

            #401 errors are appkeyname/appkey errors
            if response.status == 401:
                msg = json.loads(body)['error']
                raise TembooCredentialError(msg)

            #404 errors are "object not found" (or permissions errors)
            #NOTE: Malformed URIs can result in a 404, too, but the
            #body text won't be a JSON string.
            if response.status == 404 and body.startswith("{"):
                msg = json.loads(body)['error']
                raise TembooObjectNotAccessibleError(msg, path)

            #Any response < 200 or >= 300 means an error.
            msg = 'Bad HTTP response code. ({0})'.format(response.status)
            raise TembooHTTPError(msg, response.status, response.reason, body)

        finally:
            if conn is not None:
                conn.close()
예제 #55
0
파일: ipn.py 프로젝트: rmurt/reddit
def _google_checkout_post(url, params):
    u = urlparse("%s%s" % (url, g.GOOGLE_ID))
    conn = HTTPSConnection(u.hostname, u.port)
    auth = base64.encodestring("%s:%s" % (g.GOOGLE_ID, g.GOOGLE_KEY))[:-1]
    headers = {"Authorization": "Basic %s" % auth, "Content-type": 'text/xml; charset="UTF-8"'}

    conn.request("POST", u.path, params, headers)
    response = conn.getresponse().read()
    conn.close()

    return BeautifulStoneSoup(response)
예제 #56
0
    def __query(self, resource, params):
        params['output'] = 'json'
        params = urllib.urlencode(params)

        conn = HTTPSConnection(API_ENDPOINT)
        conn.request('GET', 'https://%s/%s?%s' % (API_ENDPOINT, resource, params), headers=self.authHeader)
        response = conn.getresponse()
        data = response.read()
        conn.close()

        return data
예제 #57
0
파일: network.py 프로젝트: GertBurger/ibid
    def _request(self, url, method):
        scheme, host = urlparse(url)[:2]
        scheme = scheme.lower()
        proxies = getproxies_environment()
        if scheme in proxies:
            scheme, host = urlparse(proxies[scheme])[:2]
            scheme = scheme.lower()

        kwargs = {}
        if version_info[1] >= 6:
            kwargs["timeout"] = self.timeout
        else:
            socket.setdefaulttimeout(self.timeout)

        if scheme == "https":
            conn = HTTPSConnection(host, **kwargs)
        else:
            conn = HTTPConnection(host, **kwargs)

        headers = {}
        if method == "GET":
            headers["Range"] = "bytes=0-%s" % self.max_size

        try:
            try:
                conn.request(method.upper(), iri_to_uri(url), headers=headers)
                response = conn.getresponse()
                data = response.read(self.max_size)
                conn.close()
            except socket.error, e:
                raise HTTPException(e.message or e.args[1])
        finally:
            if version_info[1] < 6:
                socket.setdefaulttimeout(None)

        contenttype = response.getheader("Content-Type", None)
        if contenttype:
            match = re.search("^charset=([a-zA-Z0-9-]+)", contenttype)
            try:
                if match:
                    data = data.decode(match.group(1))
                elif contenttype.startswith("text/"):
                    data = data.decode("utf-8")
            except UnicodeDecodeError:
                guessed = detect(data)
                if guessed["confidence"] > 0.5:
                    charset = guessed["encoding"]
                    # Common guessing mistake:
                    if charset.startswith("ISO-8859") and "\x92" in data:
                        charset = "windows-1252"
                    data = unicode(data, charset, errors="replace")

        return response.status, response.reason, data, response.getheaders()
예제 #58
0
파일: api.py 프로젝트: DanHoerst/reddit
 def make_request(self):
     u = urlparse(g.authorizenetapi)
     try:
         conn = HTTPSConnection(u.hostname, u.port)
         conn.request("POST", u.path, self.toXML(),
                      {"Content-type": "text/xml"})
         res = conn.getresponse()
         res = self.handle_response(res.read())
         conn.close()
         return res
     except socket.error:
         return False
예제 #59
0
파일: ipn.py 프로젝트: siadat/reddit
def _google_checkout_post(url, params):
    u = urlparse("%s%s" % (url, g.GOOGLE_ID))
    conn = HTTPSConnection(u.hostname, u.port)
    auth = base64.encodestring('%s:%s' % (g.GOOGLE_ID, g.GOOGLE_KEY))[:-1]
    headers = {"Authorization": "Basic %s" % auth,
               "Content-type": "text/xml; charset=\"UTF-8\""}

    conn.request("POST", u.path, params, headers)
    response = conn.getresponse().read()
    conn.close()

    return BeautifulStoneSoup(response)
예제 #60
0
파일: session.py 프로젝트: mikecpp/python
    def _do_request(self, http_method, path, body=None, parameters=None):
        """
        Generic HTTP/S connection method.

        """

        full_path = self._session_base_path + path

        #If any parameters were given, tack them on to the end of the path.
        if parameters:
            full_path += '?' + urlencode(parameters)

        conn = None
        response = None
        try:
            #TO DO: Rewrite to facilitate connection pooling.
            if self._secure:
                conn = HTTPSConnection(self._host)
            else:
                conn = HTTPConnection(self._host)

            try:
                conn.request(http_method, full_path, body, self._headers)
            except:
                raise TembooError('An error occurred connecting to the Temboo server. Verify that your Temboo Account Name is correct, and that you have a functioning network connection')

            response = conn.getresponse()
            body = response.read().decode('utf-8')

            #Any 200-series response means success.
            if 200 <= response.status < 300:
                return json.loads(body)

            #401 errors are appkeyname/appkey errors
            if response.status == 401:
                msg = json.loads(body)['error']
                raise TembooCredentialError(msg)
        
            #404 errors are "object not found" (or permissions errors)
            #NOTE: Malformed URIs can result in a 404, too, but the 
            #body text won't be a JSON string.
            if response.status == 404 and body.startswith("{"):
                msg = json.loads(body)['error']
                raise TembooObjectNotAccessibleError(msg, path)

            #Any response < 200 or >= 300 means an error.
            msg = 'Bad HTTP response code. ({0})'.format(response.status)
            raise TembooHTTPError(msg, response.status, response.reason, body)

        finally:
            if conn is not None:
                conn.close()