def add(request):
    instance = Person(verified=False, ip="127.0.0.1",
                      last_updated=datetime.datetime.utcfromtimestamp(0))
    form = AddPersonForm(request.POST or None, instance=instance)
    if form.is_valid():
        person = form.save(commit=False)
        ssl_context = ssl.create_default_context()
        ssl_context.check_hostname = False
        ssl_context.verify_mode = ssl.CERT_NONE
        host = resolve_location(person)
        connection = HTTPSConnection(host=host, port=8080,
                                     context=ssl_context)
        connection.request("GET", reverse("directory:info"))
        response = connection.getresponse()
        data = JSONParser().parse(response)
        cert  = connection.sock.getpeercert()
        connection.close()
        serializer = PublicPersonSerializer(person, data=data)
        cn = None
        #for key, value in cert["subject"]:
        #    if key == "commonName":
        #        cn = value
        #        break
        #if cn == person.location and serializer.is_valid():
        if serializer.is_valid():
            serializer.save()
        return HttpResponseRedirect(reverse("directory:index"))
    context = {"form": form, "section": "directory"}
    return render(request, "person_form.html", context)
Beispiel #2
0
    def filter(self):
        url = "https://stream.twitter.com/1.1/statuses/filter.json"
        query = {}
        querystring =""
        if self.track:
            query["track"] = self.track
            querystring += "track="+quote(self.track)

        if self.locations:
            query["locations"] = self.locations
            if querystring:
                querystring += "&"
            querystring += "locations="+quote(self.locations)

        self.start_time = int(time.time())
        running = True
        while running:
            try:
                auth_header = self.generate_authorization_header("POST",url,query)
                logging.getLogger("twitstream").debug("calling: https://stream.twitter.com/1.1/statuses/filter.json?"+querystring)
                conn = HTTPSConnection("stream.twitter.com")
                conn.request("POST","/1.1/statuses/filter.json?"+querystring,"",{'User-agent':'Mozilla/5.0','Authorization':auth_header})
                running = self.stream(conn)
            except Exception as ex:
                logging.getLogger("twitstream").error(str(ex))
def _is_online(domain, sub_path, response_status, response_reason):
    conn = HTTPSConnection(domain, timeout=1)
    conn.request("HEAD", sub_path)
    response = conn.getresponse()
    conn.close()

    return (response.status == response_status) and (response.reason == response_reason)
def submit_xml_request(xml_file):
    '''
    Submit the xml request to MAST.

    Parameters:
        xml_file : string
            The xml request string.

    Returns:
        response : httplib object
            The xml request submission result.
    '''
    home = os.environ.get("HOME")
    user = os.environ.get("USER")

    signer = SignStsciRequest()
    request_xml_str = signer.signRequest("{0}/.ssh/privkey.pem".format(home),
                                         xml_file)
    params = urllib.urlencode({
        "dadshost": "dmsops1.stsci.edu",
        "dadsport": 4703,
        "mission": "HST",
        "request": request_xml_str})
    headers = {"Accept": "text/html",
               "User-Agent": "{0}PythonScript".format(user)}
    req = HTTPSConnection("archive.stsci.edu")
    req.request("POST", "/cgi-bin/dads.cgi", params, headers)
    response = req.getresponse().read()

    return response
    def get_yandex_translate(self, searching_text, to_language):
        try:
            quoted_text = quote(u"" + searching_text + "")
            req = '/api/v1.5/tr.json/translate?' \
                  'key=trnsl.1.1.20150629T151613Z.82119cbb3415f490.ba44aa922a8b28e549144e0e2fa6249c7508358e' \
                  '&text=' + quoted_text \
                  + "&lang=" + to_language \
                  + '&options=1'

            conn = HTTPSConnection('translate.yandex.net')
            conn.request("GET", req)
            rez = conn.getresponse()
            data = rez.read()
            conn.close()

            if rez.status == 200:
                ddata = data.decode("utf-8")
                j = simplejson.loads(ddata, )
                tr_text = ''.join(j['text'])
                print(tr_text)
                return tr_text
            else:
                print("Something is wrong: " + str(rez.status) + " status code")
                print(data)

        except HTTPException as err:
            print(err)
    def send(self, type, text, raw):
        message_time = time.time()
        message_timestamp = time.ctime(message_time)

        if check_time_restriction(self.starttime, self.endtime):
            if self.suppress_timestamp == False:
                self.msg_to_send = text[:10000].encode('utf8') + " Message Sent at: " + message_timestamp
            else:
                self.msg_to_send = text[:10000].encode('utf8')

            notify_data = {
                'apikey': self.api_key,
                'application': self.app_name,
                'event': self.event,
                'description': self.msg_to_send,
                'priority': self.priority
            }

            if sys.version_info >= (2,7,9):
                http_handler = HTTPSConnection(PROWL_URL, context=ssl._create_unverified_context())
            else:
                http_handler = HTTPSConnection(PROWL_URL)

            http_handler.request(PROWL_METHOD, PROWL_PATH, headers=self.headers,body=urlencode(notify_data))

            http_response = http_handler.getresponse()

            if http_response.status == 200:
                return True
            else:
                current_app.logger.info('Event Prowl Notification Failed: {0}'. format(http_response.reason))
                raise Exception('Prowl Notification Failed: {0}' . format(http_response.reason))
Beispiel #7
0
    def _send(self, message):
        """
        Sends the specified PushoverMessage object via the Pushover API.
        """

        kwargs = message.get()
        kwargs['token'] = self.token

        assert 'message' in kwargs
        assert self.token is not None

        if not 'user' in kwargs:
            if self.set_user is not None:
                kwargs['user'] = self.user_token
                if self.user_device is not None:
                    kwargs['device'] = self.user_device
            else:
                kwargs['user'] = os.environ['PUSHOVER_USER']

        data = urlencode(kwargs)
        conn = HTTPSConnection(Pushover.PUSHOVER_SERVER)
        conn.request("POST", Pushover.PUSHOVER_ENDPOINT, data,
                     Pushover.PUSHOVER_CONTENT_TYPE)
        output = conn.getresponse().read().decode('utf-8')
        data = json.loads(output)

        if data['status'] != 1:
            raise PushoverError(output)
        else:
            return True
Beispiel #8
0
def getRemoteDBModifiedTS():
    """
    Performs a HEAD request to get the Last-Modified date-time
    of a database dump file and parses it into a UNIX timestamp.
    """
    debug_msg = "Unable to get timestamp of remote database dump - {0}"
    logging.info("Getting timestamp of database dump at '%s'", HOST + PATH)
    try:
        # Removing the scheme from the URL
        conn = HTTPSConnection(HOST[8:], timeout=TIMEOUT)
        conn.request("HEAD", PATH)
    except gaierror as e:
        logging.debug(debug_msg.format("Cannot connect to '%s', error: %s"), HOST + PATH, e)
        exit(1)

    rsp = conn.getresponse()

    if rsp.status != 200:
        logging.debug(debug_msg.format("Server responded with: %d %s"), rsp.status, rsp.reason)
        exit(1)

    last_modified = rsp.getheader("last-modified", None)
    if last_modified is None:
        logging.debug(debug_msg.format("Response doesnt include Last-Modified Header"))
        exit(1)

    last_m_dt = datetime.strptime(last_modified.split(", ")[1], "%d %b %Y %H:%M:%S %Z")
    return timegm(last_m_dt.timetuple())
def push(event, msg):
  global config

  data = {'apikey': config["API_KEY"],
          'application': __application__,
          'event': event,
          'description': msg,
          'priority': 0}

  h = Https(config["API_DOMAIN"])

  h.request("POST",
            "/publicapi/add",
            headers={'Content-type': "application/x-www-form-urlencoded",
                     'User-Agent': __application__ + '/' + str(__version__)
                     },
            body=urlencode(data))

  response = h.getresponse()
  request_status = response.status

  if request_status == 200:
    return True
  else:
    return False
def submit_xml_request(xml_request):
    """Submit the XML request to the MAST archive.

    Parameters
    ----------
    xml_request : string
        The request XML string.

    Returns
    -------
    submission_results : httplib object
        The XML request submission results.
    """

    user = os.environ.get("USER")
    home = os.environ.get("HOME")

    signer = SignStsciRequest()
    request_xml_str = signer.signRequest('{0}/.ssh/privkey.pem'.format(home), xml_request)
    params = urlencode({
        'dadshost': get_settings()['dads_host'],
        'dadsport': 4703,
        'mission':'HST',
        'request': request_xml_str})
    headers = {"Accept": "text/html", "User-Agent":"{0}PythonScript".format(user)}
    req = HTTPSConnection(get_settings()['archive'])
    req.request("POST", "/cgi-bin/dads.cgi", params, headers)
    response = req.getresponse().read()
    req.close()

    return response
Beispiel #11
0
	def post_mp_request(self,data,files,extra_path=""):
		# Create the multipart request body
		buffer_fname = tempfile.mktemp()
		buffer_fp = open(buffer_fname,'wb')
		mpw = MultiPartWriter(buffer_fp)
		mpw.add_attachment(data.encode('utf-8'),'application/json, charset=UTF-8','body')
		for cid,fp in files.items():
			mpw.add_attachment(fp,'application/octet-stream',cid)
		mpw.done()
		buffer_fp.close()
		headers = {
			"Content-type": b'multipart/related; boundary=' + mpw.boundary,
			"Accept": b'application/json,multipart/related'}
		if self.scheme.lower()=='https':
			conn = HTTPSConnection(self.hostname,self.port)
		else:
			conn = HTTPConnection(self.hostname,self.port)
		req_path = self.path + '/' + extra_path
		buffer_fp = open(buffer_fname,'rb')
		conn.request("POST", req_path, buffer_fp, headers)
		buffer_fp.close()
		jsonwsp_response = self.parse_response(conn.getresponse())
		conn.close()
		os.unlink(buffer_fname)
		return jsonwsp_response
Beispiel #12
0
    def query(self, endpoint, arguments=None):
        if not endpoint.startswith('/'):
            endpoint = '/' + endpoint
        if not endpoint.endswith(ApiClient.ENDPOINT_EXT):
            endpoint += ApiClient.ENDPOINT_EXT
        if arguments is None:
            arguments = {}

        query_string = '?'.join([endpoint, urlencode(arguments)])

        result = self.cache.get(query_string)

        if result is not None:
            return objectify.fromstring(result)

        connection = HTTPSConnection(ApiClient.API_HOST)
        connection.request('GET', query_string)
        response = connection.getresponse()
        if response.status != 200:
            raise ApiError('Error response: ' + str(response.status))
        result = response.read()
        result_object = objectify.fromstring(result)
        cached_until = datetime.strptime(result_object.cachedUntil.text,
                                         ApiClient.TIMESTAMP_FORMAT)
        self.cache.set(query_string, result, cached_until)
        return result_object
Beispiel #13
0
def ping_url(protocol, url):
    url = to_string(url)
    protocol = to_string(protocol).lower()

    url_parts = url.split('/', 1)
    host = url_parts[0]
    if len(url_parts) == 1:
        path = '/'
    else:
        path = '/%s' % url_parts[1]

    if protocol == 'https':
        connection = HTTPSConnection(host)
    elif protocol == 'http':
        connection = HTTPConnection(host)
    else:
        raise ValueError('url', 'Invalid protocol %s. Use only http or https.' % protocol)

    valid_url = False
    try:
        connection.request('HEAD', path)
        response = connection.getresponse()
    except Exception:
        pass
    else:
        if response.status != 404:
            valid_url = True
    finally:
        connection.close()
        return valid_url
Beispiel #14
0
    def _send_post(self, url, payload={}):
        payload = copy.copy(payload) # avoid modifying the original dict

        # add some stuff to the payload
        payload['nonce'] = int(time.time()*1e6)

        body = urllib.parse.urlencode(payload).encode() # convert to bytes object

        sig = hmac.new(base64.b64decode(self._secret), body, hashlib.sha512).digest()
        sig_b64 = base64.b64encode(sig)

        headers = {
            'bitfloor-key': self._key,
            'bitfloor-sign': sig_b64,
            'bitfloor-passphrase': self._passphrase,
            'bitfloor-version': self._api_version,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': len(body)
        }

        conn = HTTPConn(self._host, self._order_port)
        conn.request("POST", url, body, headers)
        resp = conn.getresponse()
        s = resp.read().decode() # comes as a bytes object, decode to a string
        conn.close()
        return json.loads(s)
Beispiel #15
0
 def do_request(self, method, url, params = {}):
     final_url = url
     if method == 'GET' and params:
         final_url += '?' + urlencode(params)
         request_body = None
     elif (method == 'POST' or method == 'PUT') and params:
         request_body = urlencode(params)
     else:
         request_body = None
     print("Starting {method} {final_url}".format(method = method, final_url = final_url))
     if self.use_ssl:
         con = HTTPSConnection(self.base_url)
     else:
         con = HTTPConnection(self.base_url)
     headers = {}
     headers["Content-Type"] = "application/x-www-form-urlencoded"
     headers["ISV_API_KEY"] = self.key
     headers["ISV_API_SECRET"] = self.secret
     headers["LCNS_DISABLE_SIGN"] = "true"
     con.request(method, final_url, request_body, headers)
     res = con.getresponse()
     body = res.read()
     status = res.status
     if self.debug:
         debug_message = ""
         debug_message += "################################\n"
         debug_message += "REQUEST: {method} {uri}\n".format(method = method.upper(), uri = self.request_url(final_url) )
         if params and ( method == 'POST' or method == 'GET' ):
             debug_message += "PARAMETERS: {params}\n".format(params = params)
         debug_message += "RESPONSE CODE: {status}\n".format(status = status)
         debug_message += "BODY:\n{body}\n".format(body = body)
         debug_message += "================================\n"
         print(debug_message)
     return {'body': body, 'status': status}
Beispiel #16
0
    def send(self, type, text):
        message_time = time.time()
        message_timestamp = time.ctime(message_time)

        if check_time_restriction(self.starttime, self.endtime):
            self.msg_to_send = text[:10000].encode('utf8') + " Message Sent at: " + message_timestamp
            self.event = NMA_EVENT.encode('utf8')
            self.content_type = NMA_CONTENT_TYPE

            notify_data = {
                'application': self.app_name,
                'description': self.msg_to_send,
                'event': self.event,
                'priority': self.priority,
                'content-type': self.content_type,
                'apikey': self.api_key
            }

            headers = { 'User-Agent': NMA_USER_AGENT }
            headers['Content-type'] = NMA_HEADER_CONTENT_TYPE
            http_handler = HTTPSConnection(NMA_URL)
            http_handler.request(NMA_METHOD, NMA_PATH, urlencode(notify_data), headers)

            http_response = http_handler.getresponse()

            try:
                res = self._parse_response(http_response.read())
            except Exception as e:
                res = {
                    'type': 'NMA Notify Error',
                    'code': 800,
                    'message': str(e)
                }
                current_app.logger.info('Event NotifyMyAndroid Notification Failed: {0}'.format(str(e)))
                raise Exception('NotifyMyAndroid Failed: {0}' . format(str(e)))
Beispiel #17
0
def ensure_no_open_tickets(version):
    version = "v%s" % version
    conn = HTTPSConnection("api.github.com")
    try:
        log("Checking for open tickets on Github for version %s" % version)
        log("Check if node is available")
        conn.request(
            "GET",
            "/repos/elasticsearch/elasticsearch/issues?state=open&labels=%s" % version,
            headers={"User-Agent": "Elasticsearch version checker"},
        )
        res = conn.getresponse()
        if res.status == 200:
            issues = json.loads(res.read().decode("utf-8"))
            if issues:
                urls = []
                for issue in issues:
                    urls.append(issue["url"])
                raise RuntimeError("Found open issues  for release version %s see - %s" % (version, urls))
            else:
                log("No open issues found for version %s" % version)
        else:
            raise RuntimeError("Failed to fetch issue list from Github for release version %s" % version)
    except socket.error as e:
        log("Failed to fetch issue list from Github for release version %s' % version - Exception: [%s]" % (version, e))
        # that is ok it might not be there yet
    finally:
        conn.close()
Beispiel #18
0
    def _url_retrieve(self, url, outfd, reporthook, binary):
        #Like the one in urllib. Unlike urllib.retrieve url_retrieve
        #can be interrupted. KeyboardInterrupt exception is raised when
        #interrupted.
        count = 0
        blockSize = 1024 * 8
        parsed_url = urlparse(url)
        host = parsed_url.netloc
        try:
            connection = HTTPSConnection(host, timeout=15)
            headers = { "Accept-Encoding": "identity", "Host": host, "User-Agent": "Python/3" }
            connection.request("GET", parsed_url.path, headers=headers)
            urlobj = connection.getresponse()
            assert urlobj.getcode() == 200

            totalSize = int(urlobj.info()['content-length'])

            while not self._is_aborted():
                data = urlobj.read(blockSize)
                count += 1
                if not data:
                    break
                if not binary:
                    data = data.decode("utf-8")
                outfd.write(data)
                ui_thread_do(reporthook, count, blockSize, totalSize)
        except Exception as e:
            raise e
Beispiel #19
0
 def send(self, message):
     """
     Sends a specified message with id "message" or as object.
     """
     # Only send message if message is instance of PushoverMessage
     if type(message) is PushoverMessage:
         # get dict contaning "message" and any other key-values declared
         # by the "set" method.
         data = message.get()
         data['token'] = self.token
         # define which user to send notification to if any
         if self.user is not None:
             data['user'] = self.user_token
             # define which device to send notification to if any
             if self.user_device is not None:
                 data['device'] = self.user_device
         # initialise HTTPS connection
         conn = HTTPSConnection(self.PUSHOVER_SERVER)
         # send HTTPS request
         conn.request(
             "POST",
             self.PUSHOVER_ENDPOINT,
             urlencode(data),
             self.PUSHOVER_CONTENT_TYPE
         )
         # get response form server
         response = conn.getresponse().read().decode('utf-8')
         data = json.loads(response)
         # check response for error
         if data['status'] != 1:
             raise PushoverError(response)
         else:
             return True
     else:
         raise PushoverError("Wrong type passed to Pushover.send()!")
Beispiel #20
0
    def callapi(self, method, path, args):
        # Set headers.
        headers = {
            'User-Agent': USER_AGENT
        }

        # Set content type if we want to post the data.
        if method == "POST":
            headers['Content-type'] = "application/x-www-form-urlencoded"

        # Create the HTTPSConnection and do the api call.
        http_handler = HTTPSConnection(API_SERVER)
        http_handler.request(method, path, urlencode(args), headers)

        # Get the response.
        resp = http_handler.getresponse()

        try:
            # Return the API-Response.
            return self._parse_reponse_json(resp.read())

        except Exception as e:
            # Return response could not be parsed.
            return {
                'type':    "RapidPush parse error",
                'code':    600,
                'message': str(e)
            }
Beispiel #21
0
    def call(self, method='', params=None):
        """ Call T411 API """
        if params is not None:
            params = urlencode(params)
        
        headers = HTTP_HEADERS
        if method != 'auth':
            headers['Authorization'] = self._token

        conn = HTTPSConnection(API_URL)
        conn.request('POST', '/%s' % method, body=params, headers=headers)
        r = conn.getresponse()
        if r.status == HTTP_OK:
            #import pdb; pdb.set_trace()
            rt = r.read()
            try:
                pass
                rt = rt.decode('utf-8')     
                conn.close()
            except:
                conn.close()
            return rt
        else:
            conn.close()
            return False
Beispiel #22
0
 def httpsGet(self, url):
     Log.d("GET " + url)
     url = self.completeUrl(url)
     headers = self.createHeaders()
     conn = HTTPSConnection(self.server)
     conn.request("GET", url, None, headers)
     self.lastResponse = self.parseResponse(conn.getresponse())
     return self.lastResponse
def normalise_package(name):
    """
    """
    http = HTTPSConnection('pypi.python.org')
    http.request('HEAD', '/pypi/%s/' % name)
    r = http.getresponse()
    if r.status not in (200, 301):
        raise ValueError(r.reason)
    return r.getheader('location', name).split('/')[-1]
Beispiel #24
0
 def _send_get(self, url, payload={}):
     body = urllib.parse.urlencode(payload)
     conn = HTTPConn(self._host, self._data_port)
     conn.request("GET", url, body)
     resp = conn.getresponse()
     s = resp.read().decode() # comes as a bytes-type object, decode to string
     conn.close()
     print(s)
     return json.loads(s)
Beispiel #25
0
    def retrieve_apikey(self, providerkey=None, token=None):
        """
        Get an API key from a registration token retrieved in retrieve/token.
        The user must have approved your request first, or you will get an
        error response.

        The parameters are :
        - providerkey (required) : your provider API key.
        - token (required): the token returned from retrieve_token.

        This returns a dictionary such as:
        {'apikey': u'16b776682332cf11102b67d6db215821f2c233a3',
         'code': u'200',
         'remaining': u'999',
         'resetdate': u'1299535575'}
        """

        h = Https(API_DOMAIN)

        data = {'apikey': self.apikey}

        if providerkey is not None:
            data['providerkey'] = providerkey
        else:
            raise Exception("Provider Key is required for retrieving API key")

        if token is not None:
            data['token'] = token
        else:
            raise Exception("Token is required for retrieving API key.\
                             Call retrieve_token to request it.")

        h.request("GET",
                  "/publicapi/retrieve/apikey?" + urlencode(data),
                  headers=self.headers)

        request = h.getresponse()
        request_status = request.status

        if request_status == 200:
            dom = minidom.parseString(request.read())
            code = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('code')
            remaining = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('remaining')
            resetdate = dom.getElementsByTagName('prowl')[0].\
                            getElementsByTagName('success')[0].\
                            getAttribute('resetdate')
            users_api_key = dom.getElementsByTagName('prowl')[0].\
                                getElementsByTagName('retrieve')[0].\
                                getAttribute('apikey')
            return dict(apikey=users_api_key, code=code, remaining=remaining,
                        resetdate=resetdate)
        else:
            self._relay_error(request_status)
Beispiel #26
0
 def request(self, method, url, body=None):
     connection = HTTPSConnection('saucelabs.com')
     connection.request(method, url, body, headers=self.headers)
     response = connection.getresponse()
     json_data = response.read()
     connection.close()
     if response.status != 200:
         raise Exception('%s: %s.\nSauce Status NOT OK' %
                         (response.status, response.reason))
     return json_data
Beispiel #27
0
    def __init__(self, vidID):
        conn = HTTPSConnection("www.youtube.com")

        conn.request("GET", "/watch?v={}".format(vidID))

        stream = conn.getresponse()
        if stream.getheader("Content-Encoding") is not None:
            stream = GzipFile(fileobj=stream)
        self.data = str(stream.read(), "utf-8")

        conn.close()
Beispiel #28
0
def lookForAddress(loc):
    print("Google GEOCODE Query")
    conn = HTTPSConnection("maps.googleapis.com")
    parametres = "latlng="+loc.latlng()+"&sensor=false&key="+commons.googleAPIKey
    conn.request("GET", "/maps/api/geocode/xml?"+parametres)
    response = conn.getresponse()
    data = response.read()
    dom = parseString(data)
    for d in dom.getElementsByTagNameNS("*", 'formatted_address'):
         loc.desc = d.firstChild.data
         return
Beispiel #29
0
def check_updated_certs(_address, _port, certhashlist, newhash=None, timeout=config.default_timeout, connect_timeout=config.connect_timeout, traversefunc=None):
    update_list = []
    if None in [_address, _port]:
        logging.error("address or port empty")
        return None
    addr, _port = url_to_ipv6(_address, _port)
    cont = default_sslcont()
    con = HTTPSConnection(addr, _port, context=cont, timeout=connect_timeout)
    try:
        con.connect()
    except (ConnectionRefusedError, socket.timeout):
        if not traversefunc:
            logging.warning("Connection failed")
            return None
        # try_traverse does not work here, scnreqest creates loop
        con.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
        con.sock.bind(('', 0))
        traversefunc(("", con.sock.getsockname()[1]))
        con.sock.settimeout(connect_timeout)
        for count in range(0, config.traverse_retries):
            try:
                con.sock.connect((addr, _port))
                break
            except Exception:
                pass
        else:
            logging.warning("traversal failed")
            return None
    con.sock.settimeout(timeout)
    oldhash = dhash(ssl.DER_cert_to_PEM_cert(con.sock.getpeercert(True)).strip().rstrip())
    if newhash and newhash != oldhash:
        return None
    oldsslcont = con.sock.context
    for _hash, _security in certhashlist:
        con.request("POST", "/usebroken/{hash}".format(hash=_hash), headers=cert_update_header)
        con.sock = con.sock.unwrap()
        con.sock = cont.wrap_socket(con.sock, server_side=False)
        con.sock.do_handshake()
        brokensslcert = ssl.DER_cert_to_PEM_cert(con.sock.getpeercert(True)).strip().rstrip()
        con.sock = con.sock.unwrap()
        # without next line the connection would be unencrypted now
        con.sock = oldsslcont.wrap_socket(con.sock, server_side=False)
        # con.sock.do_handshake()
        ret = con.getresponse()
        if ret.status != 200:
            logging.info("checking cert failed, code: %s, reason: %s", ret.status, ret.reason)
            continue
        if con.sock and oldhash != dhash(ssl.DER_cert_to_PEM_cert(con.sock.getpeercert(True)).strip().rstrip()):
            logging.error("certificate switch detected, stop checking")
            break
        if dhash(brokensslcert) == _hash:
            update_list.append((_hash, _security))
    con.close()
    return update_list
Beispiel #30
0
 def request_token(self):
     connection = HTTPSConnection(self._host)
     request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, 
         http_url='https://' + self._host + '/oauth/request_token')
     request.sign_request(self._signature_method, self._consumer, None)
     connection.request(request.http_method, request.to_url())
     resp = connection.getresponse()
     if resp.status == 200: 
         self._oauth_token = oauth.OAuthToken.from_string(resp.read().decode())
         return {'key': self._oauth_token.key, 'secret': self._oauth_token.secret}
     else:
         raise InsTwitterResponseError(resp.code, resp.read().decode()) 
Beispiel #31
0
def Image_DayMovie(TitleText):
    global DayMovieDoc
    global MovieChart
    global ChartFlag

    ImgURL = []
    image = []

    for i in range(0, 5):
        if ChartFlag == True:
            server = "openapi.naver.com"
            client_id = "iEV22cE2b1ZJnyGDYXtc"
            client_secret = "xJB_PnOFmw"
            text = urllib.parse.quote(TitleText[i])

            conn = HTTPSConnection(server)
            conn.request(
                "GET",
                "/v1/search/movie.xml?query=" + text + "&display=10&start=1",
                None, {
                    "X-Naver-Client-Id": client_id,
                    "X-Naver-Client-Secret": client_secret
                })

            req = conn.getresponse()
            if int(req.status) == 200:
                ImageDoc = parseString(req.read())
                ImgURL.append(getJpgURL(ImageDoc, TitleText[i]))

        else:
            server = "openapi.naver.com"
            client_id = "iEV22cE2b1ZJnyGDYXtc"
            client_secret = "xJB_PnOFmw"
            text = urllib.parse.quote(TitleText[i + 5])

            conn = HTTPSConnection(server)
            conn.request(
                "GET",
                "/v1/search/movie.xml?query=" + text + "&display=10&start=1",
                None, {
                    "X-Naver-Client-Id": client_id,
                    "X-Naver-Client-Secret": client_secret
                })

            req = conn.getresponse()
            if int(req.status) == 200:
                ImageDoc = parseString(req.read())
                ImgURL.append(getJpgURL(ImageDoc, TitleText[i + 5]))

    for i in range(0, len(ImgURL)):
        #print(ImgURL[i])
        if ImgURL[i] != None:
            with urllib.request.urlopen(ImgURL[i]) as u:
                raw_data = u.read()
            tmp = Image.open(BytesIO(raw_data))
            tmp = tmp.resize((160, 208), Image.ANTIALIAS)
            tmp = ImageTk.PhotoImage(tmp)
            image.append(tmp)

            JPG = Label(MovieChart, image=image[i], bg="Black")
            JPG.place(x=55 + (180 * i), y=100)
        else:
            image.append(None)

    MovieChart.mainloop()
Beispiel #32
0
class MatomoUser:
    """
    Utils for trying an automatic connection to matomo
    whenver a admin is loggin into django's admin interface.

    Matomo and django admin sharing the same username/password
    to perform this.
    """
    SITE = "matomo"
    def __init__(self, username, password):
        self.nonce = None
        self.user = username
        self.password = password
        self._cookie = SimpleCookie()

        #SECURITY ISSUE:
        # - certificate not checked, can lead to man in the middle attack
        self.connection = HTTPSConnection(self.SITE,
                context=ssl.SSLContext())
        self._retrieve_session_cookie()
        self.login()

    def request(self, method, url, body=None, headers={}, read=False):
        """
        Hook to perform request with http.client.
        """
        #Try to add session cookie if already available.
        try:
            headers['Cookie'] = self.matomo_session
        except KeyError:
            pass

        #Format post data.
        if type(body) is dict:
            body = urllib.urlencode(body)

        #Add website root if needed
        if url[0] != "/":
            url = "/" + url

        #Send the request to website
        self.connection.request(method, url, \
                body=body, headers=headers)

        #Return the request response
        response = self.connection.getresponse()
        if read:
            response.read()
        return response

    def _retrieve_session_cookie(self):
        """
        Use to retrieve default data to login.
        Retrieve the MATOMO_SESSID cookie and the
        hidden form input login_form_nonce.
        """
        response = self.request("GET", "/")
        html_page = response.read().decode()

        if response.status != 200:
            raise MatomoUserException("Unable to connect to matomo site")
        #Retrieve <form> nonce of matomo
        pattern = "<input .*login_form_nonce.*?>"
        input_tag = re.search(pattern, html_page)
        if input_tag is None:
            raise MatomoUserException("login_form_nonce not found.")
        #Retrieve form nonce value
        parser = NonceExtractor()
        parser.feed(input_tag.group(0))
        self.nonce = parser.nonce
        self._cookie.load(response.headers['Set-Cookie'])

    def login(self):
        """
        Login user to matomo.
        """
        post_data = {
                "form_login" : self.user,
                "form_password" : self.password,
                **self.nonce,
                }
        headers = {
                "Content-Type" : "application/x-www-form-urlencoded",
                }
        response = self.request("POST", "/?module=Login", \
                post_data, headers, read=True)
        self._cookie.load(response.headers['Set-Cookie'])

    @property
    def matomo_session(self):
        """
        Get current MATOMO_SESSID cookie formated for http
        request
        """
        return "MATOMO_SESSID={}".format(self.cookie)

    @property
    def cookie(self):
        return self._cookie['MATOMO_SESSID'].value
        return False

    print(out)


#This sets up the https connection
c = HTTPSConnection("658fa002trial.it-cpitrial.cfapps.us10.hana.ondemand.com")
#we need to base 64 encode it
#and then decode it to acsii as python 3 stores it as a byte string
#username = sys.argv[1]
#password = sys.argv[2]
userAndPass = base64.b64encode(b"$(username):$(password)").decode("ascii")
#userAndPass = base64.b64encode(bytes(username + ':' + password, "utf-8")).decode("ascii")
headers = {'Authorization': 'Basic %s' % userAndPass, 'X-CSRF-Token': 'Fetch'}
#then connect
c.request('GET', '/api/v1/MessageProcessingLogs', headers=headers)
#get the response back
res = c.getresponse()
res.read()
xsrftoken = res.getheader("X-CSRF-Token")
print("token fetch status:" + str(res.getcode()))
myHeaders = {}
myCookie = res.getheader("Set-Cookie")
#print(res.getheaders())
if myCookie is not None:
    myCookie = myCookie.split(";")[0]
    myHeaders = {
        'Authorization': 'Basic %s' % userAndPass,
        'X-CSRF-Token': xsrftoken,
        "Content-Type": "application/json",
        "Cookie": myCookie
Beispiel #34
0
    "consider escaping characters on your password in case you have login problems..."
)
#This sets up the https connection
c = HTTPSConnection("658fa002trial.it-cpitrial.cfapps.us10.hana.ondemand.com")
#we need to base 64 encode it
#and then decode it to acsii as python 3 stores it as a byte string
userAndPass = base64.b64encode(bytes(username + ':' + password,
                                     "utf-8")).decode("ascii")
myHeaders = {
    'Authorization': 'Basic %s' % userAndPass,
    'X-CSRF-Token': 'Fetch'
}

#Get all iflow ids of a package
path = "/api/v1/IntegrationPackages('AzureExamples')/IntegrationDesigntimeArtifacts?$format=json"
c.request('GET', path, headers=myHeaders)

res = c.getresponse()
out = json.loads(res.read())["d"]["results"]
for iflow in out:
    print("download " + iflow["Id"])

    individualPath = iflow["__metadata"]["media_src"].split(":443")[1]
    iFlowName = iflow["Id"]
    c.request('GET', individualPath, headers=myHeaders)
    res = c.getresponse()
    #unzip
    localPath = Path().resolve().parent / ("src/main/groovy") / (iFlowName)

    # Check whether the specified path is an existing file
    isDir = os.path.isdir(localPath)
Beispiel #35
0
	"jql": "filter=AWO-CN1-Sprint-Task",
	            "startAt": 0,
            "maxResults" : 500,
            "fields": [
                "key",
                "customfield_12866",
                "customfield_10242",
                "subtasks",
                "aggregatetimeoriginalestimate",
                "aggregateprogress",
                "customfield_12551"
            ]
}
'''

c.request('POST', '/rest/api/2/search', headers=headers, body=body)
#get the response back
print(headers)
res = c.getresponse()
# at this point you could check the status etc
# this gets the page text
data = res.read()

d=json.loads(data)

for issue_raw in d["issues"]:
    print(issue_raw["key"])

# objects = ijson.items(data, 'issues.fields.item')
#
# column=list(objects)
Beispiel #36
0
def threads():
    c = HTTPSConnection("engine.freerice.com")
    #we need to base 64 encode it 
    #and then decode it to acsii as python 3 stores it as abyte string#POST https://engine.freerice.com/games/8166e015-f435-4ce8-b1c7-48d2cfd0759b/answer HTTP/1.1
    question_id = '3f361880-805d-55ee-98b4-cb3a28ca2028'
    
    answer = '10'
    first = True
    resource = '/games/dbb11f54-6d72-43f9-b628-6a061aad4922/answer'
    number = 0
    while True:

        if first:
            body = '{"answer":"a' + answer + '","question":"' + question_id + '","user":"******"}'
            #then connect
            #resource = '/games/33343f63-3db9-4d58-8d44-6560aee481b9/answer'
        else:
            body = '{"category":"66f2a9aa-bac2-5919-997d-2d17825c1837","level":1}'
            #resource = '/games/33343f63-3db9-4d58-8d44-6560aee481b9/answer'
        headers = {'Host': 'engine.freerice.com',\
            'Connection': 'keep-alive',\
            'Content-Length': str(len(body)),\
            'Accept': 'application/json',\
            'Origin': 'https://beta.freerice.com',\
            'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NjE3NDQyOTEsImV4cCI6MTU2NDMzNjI5MSwidXVpZCI6IjYxYjkzN2NiLWVkNzYtNDBhMy1hNzliLTkxZTU2MTkxNDE0MiJ9.q0NRXE58uwFzgr_O5PBSIqQ3QMFVYuyop11tph9sjMM',\
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',\
            'Content-Type': 'application/json',\
            #'Referer: https':'beta.freerice.com',\
            'Accept-Encoding': 'gzip, deflate, br',\
            'Accept-Language': 'en-US,en;q=0.9',}
        try:
            c.request('PATCH', resource, body=body, headers=headers)
        except:
            c = HTTPSConnection("engine.freerice.com")
            continue
        #get the response back
        res = c.getresponse()
        # at this point you could check the status etc
        # this gets the page text
        data = res.read()
        print(type(data))

        x = dict(json.loads(data))


        try:
            question_id = x["data"]["attributes"]["question_id"]
            print(question_id)
            question = x["data"]["attributes"]["question"]["text"]
            question = question.replace("x", "*")
            answer = str(eval(question))
            print(question + " = " + answer)
            print("Done with problem " + str(number))
            first = True
            number += 1
        except KeyError:
            first = False
            print('\n')
            print(x)
            messagebox.showerror()
            break
        if int(number) > 30000:
            raise Exception()
def open(readonly):
    # Parse out the username from the output_conn URL.
    parsed = urlparse(params['output_conn'])
    username = parsed.username or "admin@internal"

    # Read the password from file.
    with builtins.open(params['output_password'], 'r') as fp:
        password = fp.read()
    password = password.rstrip()

    # Connect to the server.
    connection = sdk.Connection(
        url=params['output_conn'],
        username=username,
        password=password,
        ca_file=params['rhv_cafile'],
        log=logging.getLogger(),
        insecure=params['insecure'],
    )

    system_service = connection.system_service()

    # Create the disk.
    disks_service = system_service.disks_service()
    if params['disk_format'] == "raw":
        disk_format = types.DiskFormat.RAW
    else:
        disk_format = types.DiskFormat.COW
    disk = disks_service.add(disk=types.Disk(
        name=params['disk_name'],
        description="Uploaded by virt-v2v",
        format=disk_format,
        initial_size=params['disk_size'],
        provisioned_size=params['disk_size'],
        # XXX Ignores params['output_sparse'].
        # Handling this properly will be complex, see:
        # https://www.redhat.com/archives/libguestfs/2018-March/msg00177.html
        sparse=True,
        storage_domains=[types.StorageDomain(name=params['output_storage'], )],
    ))

    # Wait till the disk is up, as the transfer can't start if the
    # disk is locked:
    disk_service = disks_service.disk_service(disk.id)
    debug("disk.id = %r" % disk.id)

    endt = time.time() + timeout
    while True:
        time.sleep(5)
        disk = disk_service.get()
        if disk.status == types.DiskStatus.OK:
            break
        if time.time() > endt:
            raise RuntimeError("timed out waiting for disk to become unlocked")

    # Get a reference to the transfer service.
    transfers_service = system_service.image_transfers_service()

    # Create a new image transfer, using the local host is possible.
    host = find_host(connection) if params['rhv_direct'] else None
    transfer = transfers_service.add(
        types.ImageTransfer(
            disk=types.Disk(id=disk.id),
            host=host,
            inactivity_timeout=3600,
        ))
    debug("transfer.id = %r" % transfer.id)

    # Get a reference to the created transfer service.
    transfer_service = transfers_service.image_transfer_service(transfer.id)

    # After adding a new transfer for the disk, the transfer's status
    # will be INITIALIZING.  Wait until the init phase is over. The
    # actual transfer can start when its status is "Transferring".
    endt = time.time() + timeout
    while True:
        time.sleep(5)
        transfer = transfer_service.get()
        if transfer.phase != types.ImageTransferPhase.INITIALIZING:
            break
        if time.time() > endt:
            raise RuntimeError("timed out waiting for transfer status " +
                               "!= INITIALIZING")

    # Now we have permission to start the transfer.
    if params['rhv_direct']:
        if transfer.transfer_url is None:
            raise RuntimeError("direct upload to host not supported, " +
                               "requires ovirt-engine >= 4.2 and only works " +
                               "when virt-v2v is run within the oVirt/RHV " +
                               "environment, eg. on an oVirt node.")
        destination_url = urlparse(transfer.transfer_url)
    else:
        destination_url = urlparse(transfer.proxy_url)

    context = ssl.create_default_context()
    context.load_verify_locations(cafile=params['rhv_cafile'])

    http = HTTPSConnection(destination_url.hostname,
                           destination_url.port,
                           context=context)

    # The first request is to fetch the features of the server.

    # Authentication was needed only for GET and PUT requests when
    # communicating with old imageio-proxy.
    needs_auth = not params['rhv_direct']

    can_flush = False
    can_trim = False
    can_zero = False
    unix_socket = None

    http.request("OPTIONS", destination_url.path)
    r = http.getresponse()
    data = r.read()

    if r.status == 200:
        # New imageio never needs authentication.
        needs_auth = False

        j = json.loads(data)
        can_flush = "flush" in j['features']
        can_trim = "trim" in j['features']
        can_zero = "zero" in j['features']
        unix_socket = j.get('unix_socket')

    # Old imageio servers returned either 405 Method Not Allowed or
    # 204 No Content (with an empty body).  If we see that we leave
    # all the features as False and they will be emulated.
    elif r.status == 405 or r.status == 204:
        pass

    else:
        raise RuntimeError("could not use OPTIONS request: %d: %s" %
                           (r.status, r.reason))

    debug("imageio features: flush=%r trim=%r zero=%r unix_socket=%r" %
          (can_flush, can_trim, can_zero, unix_socket))

    # If we are connected to imageio on the local host and the
    # transfer features a unix_socket then we can reconnect to that.
    if host is not None and unix_socket is not None:
        try:
            http = UnixHTTPConnection(unix_socket)
        except Exception as e:
            # Very unlikely failure, but we can recover by using the https
            # connection.
            debug("cannot create unix socket connection, using https: %s" % e)
        else:
            debug("optimizing connection using unix socket %r" % unix_socket)

    # Save everything we need to make requests in the handle.
    return {
        'can_flush': can_flush,
        'can_trim': can_trim,
        'can_zero': can_zero,
        'connection': connection,
        'disk': disk,
        'disk_service': disk_service,
        'failed': False,
        'highestwrite': 0,
        'http': http,
        'needs_auth': needs_auth,
        'path': destination_url.path,
        'transfer': transfer,
        'transfer_service': transfer_service,
    }
Beispiel #38
0
from http.client import HTTPSConnection
from base64 import b64encode

print("Starting module")
#This sets up the https connection
c = HTTPSConnection("jive.northgatearinso.com")
#we need to base 64 encode it
#and then decode it to acsii as python 3 stores it as a byte string
userAndPass = b64encode(b"joseenriquep:dppizza1983%.").decode("ascii")
headers = {'Authorization': 'Basic %s' % userAndPass}
#then connect
c.request('GET', '/', headers=headers)
#get the response back
res = c.getresponse()
# at this point you could check the status etc
# this gets the page text
data = res.read()
print("Printing data")
print(data)
print("Status code")
print(res.status)
print("Header")
print(res.getheaders())
Beispiel #39
0
def replicate_manifest_list(
    image: ImageName,
    endpoint: str,
    *,
    auth_header_dest: Dict[str, str] = None,
    auth_header_src: Dict[str, str] = None,
    ssl_context_dest: SSLContext = None,
    ssl_context_src: SSLContext = None,
):
    """
    Helper function as docker-py cannot operate on manifest lists.

    Args:
        image: The name of the docker image to be replicated.
        endpoint: Endpoint of the docker registry into which to replicate the image.
        auth_header_dest: HTTP basic authentication header to using when connecting to the service.
        auth_header_src: HTTP basic authentication header to using when connecting to the service.
        ssl_context_dest:
            SSL context referencing the trusted root CA certificated to used when negotiating the TLS connection.
        ssl_context_src:
            SSL context referencing the trusted root CA certificated to used when negotiating the TLS connection.
    """
    media_type = "application/vnd.docker.distribution.manifest.list.v2+json"

    # Note: This cannot be imported above, as it causes a circular import!
    from . import __version__  # pylint: disable=import-outside-toplevel

    user_agent = f"pytest-docker-registry-fixtures/{__version__}"

    https_connection = HTTPSConnection(context=ssl_context_src,
                                       host=image.endpoint)
    identifier = image.digest if image.digest else image.tag  # Prefer digest
    https_connection.request(
        "GET",
        url=f"/v2/{image.image}/manifests/{identifier}",
        headers={
            "Accept": media_type,
            "User-Agent": user_agent,
            **auth_header_src
        },
    )
    response = https_connection.getresponse()
    assert response.status == 200
    assert response.headers["Content-Type"] == media_type
    if image.digest:
        assert response.headers["Docker-Content-Digest"] == image.digest
    manifest = response.read()

    https_connection = HTTPSConnection(context=ssl_context_dest, host=endpoint)
    identifier = image.tag if image.tag else image.digest  # Prefer tag
    https_connection.request(
        "PUT",
        url=f"/v2/{image.image}/manifests/{identifier}",
        headers={
            "Content-Type": media_type,
            "User-Agent": user_agent,
            **auth_header_dest,
        },
        body=manifest,
    )
    assert https_connection.getresponse().status == 201
Beispiel #40
0
def get_raw_data():
    apikey = get_apikey()
    c = HTTPSConnection('clocked.io')
    c.request('GET', '/time', headers={'apikey': apikey})
    return c.getresponse().read().decode('utf8')
Beispiel #41
0
class RequestHelper:
    """
    Encapsulates the recurring logic for sending out requests to the
    Moodle-System.
    """
    def __init__(self,
                 moodle_domain: str,
                 moodle_path: str = '/',
                 token: str = '',
                 skip_verify_cert=False):
        """
        Opens a connection to the Moodle system
        """
        if skip_verify_cert:
            context = ssl._create_unverified_context()
        else:
            context = ssl._create_default_https_context()
        self.connection = HTTPSConnection(moodle_domain, context=context)

        self.token = token
        self.moodle_domain = moodle_domain
        self.moodle_path = moodle_path

        RequestHelper.stdHeader = {
            # 'Cookie': 'cookie1=' + cookie1,
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64)' +
            ' AppleWebKit/537.36 (KHTML, like Gecko)' +
            ' Chrome/78.0.3904.108 Safari/537.36',
            'Content-Type':
            'application/x-www-form-urlencoded'
            # copied straight out of Chrome
        }

    def post_REST(self, function: str, data: {str: str} = None) -> object:
        """
        Sends a POST request to the REST endpoint of the Moodle system
        @param function: The Web service function to be called.
        @param data: The optional data is added to the POST body.
        @return: The Json response returned by the Moodle system, already
        checked for errors.
        """

        if (self.token is None):
            raise ValueError('The required Token is not set!')

        data_urlencoded = self._get_POST_DATA(function, self.token, data)
        url = self._get_REST_POST_URL(self.moodle_path, function)

        # uncomment this print to debug requested post-urls
        # print(url)

        # uncomment this print to debug posted data
        # print(data_urlencoded)

        self.connection.request('POST',
                                url,
                                body=data_urlencoded,
                                headers=self.stdHeader)

        response = self.connection.getresponse()
        return self._initial_parse(response)

    @staticmethod
    def _get_REST_POST_URL(moodle_path: str, function: str) -> str:
        """
        Generates an url for a REST-POST request
        @params: The necessary parameters for a REST URL
        @return: A formatted url
        """
        url = (('%swebservice/rest/server.php?moodlewsrestformat=json&' %
                (moodle_path)) + ('wsfunction=%s' % (function)))

        return url

    @staticmethod
    def _get_POST_DATA(function: str, token: str, data_obj: str) -> str:
        """
        Generates the data for a REST-POST request
        @params: The necessary parameters for a REST URL
        @return: A url-encoded data string
        """
        data = {
            'moodlewssettingfilter': 'true',
            'moodlewssettingfileurl': 'true'
        }

        if data_obj is not None:
            data.update(data_obj)

        data.update({'wsfunction': function, 'wstoken': token})

        return urllib.parse.urlencode(data)

    def get_login(self, data: {str: str}) -> object:
        """
        Sends a POST request to the login endpoint of the Moodle system to
        obtain a token in Json format.
        @param data: The data is inserted into the Post-Body as arguments. This
        should contain the logon data.
        @return: The json response returned by the Moodle System, already
        checked for errors.
        """

        self.connection.request('POST',
                                '%slogin/token.php' % (self.moodle_path),
                                body=urllib.parse.urlencode(data),
                                headers=self.stdHeader)

        response = self.connection.getresponse()
        return self._initial_parse(response)

    def _initial_parse(self, response) -> object:
        """
        The first time parsing the result of a REST request.
        It is checked for known errors.
        @param response: The json response of the moodle system
        @return: The paresed json object
        """

        # Normaly Moodle answer with response 200
        if (response.getcode() != 200):
            raise RuntimeError(
                'An Unexpected Error happened on side of the Moodle System!' +
                (' Status-Code: %s' % str(response.getcode())) +
                ('\nHeader: %s' % (response.getheaders())) +
                ('\nResponse: %s' % (response.read())))

        # Try to parse the json
        try:
            response_extracted = json.loads(response.read())
        except ValueError as error:
            raise RuntimeError('An Unexpected Error occurred while trying' +
                               ' to parse the json response! Moodle' +
                               ' response: %s.\nError: %s' %
                               (response.read(), error))
        # Check for known erorrs
        if ("error" in response_extracted):
            error = response_extracted.get("error", "")
            errorcode = response_extracted.get("errorcode", "")
            stacktrace = response_extracted.get("stacktrace", "")
            debuginfo = response_extracted.get("debuginfo", "")
            reproductionlink = response_extracted.get("reproductionlink", "")

            raise RequestRejectedError(
                'The Moodle System rejected the Request.' +
                (' Details: %s (Errorcode: %s, ' % (error, errorcode)) +
                ('Stacktrace: %s, Debuginfo: %s, Reproductionlink: %s)' %
                 (stacktrace, debuginfo, reproductionlink)))

        if ("exception" in response_extracted):
            exception = response_extracted.get("exception", "")
            errorcode = response_extracted.get("errorcode", "")
            message = response_extracted.get("message", "")

            raise RequestRejectedError(
                'The Moodle System rejected the Request.' +
                ' Details: %s (Errorcode: %s, Message: %s)' %
                (exception, errorcode, message))

        return response_extracted
Beispiel #42
0
 def request(self, method, url, body=None, headers={}):
     self.request_length = 0
     if self.has_proxy:
         self.set_tunnel(self.request_host, 443)
     headers.setdefault("Host", self.request_host)
     HTTPSConnection.request(self, method, url, body, headers)
Beispiel #43
0
import json
import base64
import datetime
import uuid
import copy
from http.client import HTTPSConnection, HTTPConnection

device_id = "2c5b7596-cc75-4a2e-b42d-a75a64c85028"
device_token = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIn0.oRIgJ4aprPbFCGjnsSfA_9NnD1yDE2LLmXJeh53ZsPAsyMuy2N8J_tnbF_-MIU-VbNpI0An1yAngZf-KDWAxLIB_ZD-8Ss0w.vw62TETlUhZXhOxIlopb9A.RNAbQan-56XuBewGobuI-7qImsThE9RkhnHWBvaO5uPALK_sruzIAitApcx-FfC4gUVjx21ju6A5ubGk41Bjzg.Fn7F4o3ulos0z41pVMCEBzO6FIqQm-kv3HXF5N7t87o"

headers = {'Content-Type': 'application/json', 'Device-Token': device_token}

c = HTTPSConnection("api.iowt.robotika.co.uk")
c.request('POST',
          '/check/status',
          json.dumps({"device_id": device_id}),
          headers=headers)
res = c.getresponse()
data = res.read().decode('utf-8')

print(str(res.status) + " -- " + data)
Beispiel #44
0
 def api(self, req):
     c = HTTPSConnection(self.URL)
     c.request('GET', req, headers=self.headers)
     return c.getresponse().read().decode('utf-8', errors='ignore')
Beispiel #45
0
class ssl_worker(pp_thread):
        global pp_config
        session_timeout = int(pp_config['thread_ssl_timeout'])

        def __init__(self, key_val, manager, info = '', delay = 0):
                pp_thread.__init__(self, info)
                self.lock_close = Lock()
                self.flag_closed= False
                self.info       = info
                self.delay      = delay
                self.manager    = manager
                self.event_proc = Event()
                self.arg        = None
                self.handler    = None
                self.host_ip    = key_val['host_ip']
                self.host_name  = key_val['host_name']
                self.group      = key_val['group']
                self.timeout    = key_val['timeout'] if 'timeout' in key_val else None

        def close(self):
                if self.handler != None:
                        try:
                                self.handler.close()
                        except:
                                print_exc()
                        finally:
                                self.handler = None

        def main(self):
                if self.delay != 0 : sleep(self.delay)
                while True:
                        self.handler = HTTPSConnection(self.host_ip, timeout = self.timeout)
                        self.handler._http_vsn = 10
                        self.handler._http_vsn_str = 'HTTP/1.0'
                        try:
                                self.handler.connect()
                        except  TimeoutError:
                                self.close()
                                continue
                        except:
                                print_exc()
                                continue
                        break
                self.manager.feedback('connected', self.group, self)
                ev = self.event_proc.wait(self.session_timeout)
                self.lock_close.acquire()
                self.flag_closed = True
                self.lock_close.release()
                if self.flag_stop == True:
                        self.close()
                        return
                if ev != True :
                        self.manager.feedback('timeout', self.group, self)
                if self.arg == None:
                        self.close()
                        return
                self.do_proc(self.arg)

        def put(self, arg):
                self.lock_close.acquire()
                if self.flag_closed != True:
                        self.arg = arg
                        self.lock_close.release()
                else:
                        self.lock_close.release()
                        return False
                self.event_proc.set()
                return True

        def do_proc(self, arg): pass

        def pyget(self, req, headers = {}):
                try:
                        self.handler.request('GET', req, headers = headers)
                except:
                        self.close()
                        self.manager.feedback('err_write', self.group, self)
                        print_exc()
                        return None
                try:
                        ack  = self.handler.getresponse()
                        body = ack.read()
                except:
                        self.close()
                        self.manager.feedback('err_read', self.group, self)
                        print_exc()
                        return None
                #--------------------------------------------------------
                self.close()
                self.manager.feedback('done', self.group, self)

                key_val = {}
                key_val['body']    = body
                key_val['head']    = str(ack.msg)
                key_val['status']  = ack.status

                return key_val
 def get_access_token(self) -> str:
     conn = HTTPSConnection(self.auth_url)
     conn.request("POST", "/api/token", self.auth_params, self.auth_headers)
     response = conn.getresponse()
     return json.loads(response.read().decode("utf-8"))["access_token"]
Beispiel #47
0
    def _new_conn(self):
        # Performs the NTLM handshake that secures the connection. The socket
        # must be kept open while requests are performed.
        self.num_connections += 1
        log.debug('Starting NTLM HTTPS connection no. %d: https://%s%s',
                  self.num_connections, self.host, self.authurl)

        headers = {}
        headers['Connection'] = 'Keep-Alive'
        req_header = 'Authorization'
        resp_header = 'www-authenticate'

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

        # Send negotiation message
        headers[req_header] = (
            'NTLM %s' % ntlm.create_NTLM_NEGOTIATE_MESSAGE(self.rawuser))
        log.debug('Request headers: %s', headers)
        conn.request('GET', self.authurl, None, headers)
        res = conn.getresponse()
        reshdr = dict(res.getheaders())
        log.debug('Response status: %s %s', res.status, res.reason)
        log.debug('Response headers: %s', reshdr)
        log.debug('Response data: %s [...]', res.read(100))

        # Remove the reference to the socket, so that it can not be closed by
        # the response object (we want to keep the socket open)
        res.fp = None

        # Server should respond with a challenge message
        auth_header_values = reshdr[resp_header].split(', ')
        auth_header_value = None
        for s in auth_header_values:
            if s[:5] == 'NTLM ':
                auth_header_value = s[5:]
        if auth_header_value is None:
            raise Exception('Unexpected %s response header: %s' %
                            (resp_header, reshdr[resp_header]))

        # Send authentication message
        ServerChallenge, NegotiateFlags = \
            ntlm.parse_NTLM_CHALLENGE_MESSAGE(auth_header_value)
        auth_msg = ntlm.create_NTLM_AUTHENTICATE_MESSAGE(ServerChallenge,
                                                         self.user,
                                                         self.domain,
                                                         self.pw,
                                                         NegotiateFlags)
        headers[req_header] = 'NTLM %s' % auth_msg
        log.debug('Request headers: %s', headers)
        conn.request('GET', self.authurl, None, headers)
        res = conn.getresponse()
        log.debug('Response status: %s %s', res.status, res.reason)
        log.debug('Response headers: %s', dict(res.getheaders()))
        log.debug('Response data: %s [...]', res.read()[:100])
        if res.status != 200:
            if res.status == 401:
                raise Exception('Server rejected request: wrong '
                                'username or password')
            raise Exception('Wrong server response: %s %s' %
                            (res.status, res.reason))

        res.fp = None
        log.debug('Connection established')
        return conn
Beispiel #48
0
class Client:
    """ Responsable for requests to Blitz RESTful API """
    
    def __init__(self, user, api_key, host=None, port=None, connect=True):
        self.username = user
        self.api_key = api_key
        self.host = 'www.blitz.io' if host is None else host
        self.port = 443 if port is None else port
        self.private_key = None
        if connect:
            self.connect()
    
    def connect(self):
        """ Connects the client. """
        self.connection = HTTPSConnection(self.host, self.port)

    def get_headers(self):
        """ Returns the headers need for a auccessful request to blitz.io. """
        private = self.private_key
        headers = {
            "Content-type": "application/json",
            'X-API-User': self.username, 
            'X-API-Key': self.api_key if private is None else private,
            'X-API-Client' : 'python'
        }
        return headers
    
    def set_private_key(self, key):
        """ Sets the user private key to be used in the request header.  """
        self.private_key = key
    
    def execute(self, post_data):
        """ Sends a queue request to blitz.io RESTful API. """
        path = "/api/1/curl/execute"
        data = json.dumps(post_data)
        self.connection.request("POST", path, data, self.get_headers())
        response = self.connection.getresponse()
        response_string = response.read().decode('UTF-8')
        return json.loads(response_string)
    
    def login(self):
        """ Login to blitz.io RESTful API. """
        path = "/login/api"
        self.connection.request("GET", path, None, self.get_headers())
        response = self.connection.getresponse()
        response_string = response.read().decode('UTF-8')
        return json.loads(response_string)
    
    def job_status(self, job_id):
        """ Sends a job status request to blitz.io  RESTful API. """
        path = "/api/1/jobs/{}/status".format(job_id)
        self.connection.request("GET", path, None, self.get_headers())
        response = self.connection.getresponse()
        response_string = response.read().decode('UTF-8')
        return json.loads(response_string)
    
    def abort_job(self, job_id):
        """ Send a abort request to blitz.io RESTful API. """
        path = "/api/1/jobs/{}/abort".format(job_id)
        self.connection.request("PUT", path, '', self.get_headers())
        response = self.connection.getresponse()
        response_string = response.read().decode('UTF-8')
        return json.loads(response_string)
    
    def close(self):
        """ Closes the connection. """
        self.connection.close()
        
    def parse(self, post_data):
        """ Sends a parse request to blitz.io RESTful API. """
        path = "/api/1/parse"
        data = json.dumps(post_data)
        self.connection.request("POST", path, data, self.get_headers())
        response = self.connection.getresponse()
        response_string = response.read().decode('UTF-8')
        return json.loads(response_string)
Beispiel #49
0
 def to_voice(self,outputFile,gender=None,backend=zAI_BACKEND):
     '''
     Perform Text-to-speech to synthesize audio.
     While we don't have a proper zAudio class, result is saved to disk as audio file.
     
     Parameters:
     -----------
     outputFile: path to save output audio file
     
     gender: desired gender of the voice used to generate audio. When set to None, any avalaible gender is used.
             If a gender is specified, an error will be thrown if no voice with thta gender is available for the corresponding backend and locale combination.
     
     backend: desired backend (Google | Microsot | local)
     
     Returns:
     --------
     saves a 16kHz 16bit mono PCM wav file to desired location
     
     '''
     
     if self.lang == None:
         raise Exception("Your zText object has no 'lang' defined. Please, set the 'lang' property or perform automatic language detection using 'detect_language()'")
     
     if gender is not None:
         gender = gender.lower()
         if  gender != 'female' and gender != 'male':
             raise Exception("Gender must be either 'female' or 'male', to force a gender, or None to use any available voice gender")
 
     # Check that the keys for the selected backend are available
     if backend == 'Microsoft':
         if MICROSOFT_AZURE_BING_VOICE_API_KEY == '':
             print('MICROSOFT_AZURE_BING_VOICE_API_KEY is empty. Using local backend.')
             backend = 'local'
     
     
     if backend=='Google':
         
         raise NotImplementedError("tanslate method is currently not available with Google backend")
     
     elif backend == 'Microsoft':
         
         if self.lang not in AZURE_TTS_DATA:
             raise ValueError('Conversion to speech is not supported for this backend and language: %s' % ISO639_1_CODES[self.lang])
             
         inputData = AZURE_TTS_DATA[self.lang]
         
         if gender == None:
             if 'females' not in inputData:
                 inputData = inputData['female']
             else:
                 inputData = inputData['male']
         else:
             if gender not in inputData:
                 raise ValueError('This combination of backend and language does not support a %s voice' % gender)
             
             inputData = inputData[gender]
             
         
         params = ""
         headers = {"Ocp-Apim-Subscription-Key": MICROSOFT_AZURE_BING_VOICE_API_KEY}
     
         AccessTokenHost = "api.cognitive.microsoft.com"
         path = "/sts/v1.0/issueToken"
     
         conn = HTTPSConnection(AccessTokenHost)
         conn.request("POST", path, params, headers)
         response = conn.getresponse()
         data = response.read()
         conn.close()
         accesstoken = data.decode("UTF-8")
     
         body = ElementTree.Element('speak', version='1.0')
         body.set('{http://www.w3.org/XML/1998/namespace}lang', inputData['locale'].lower())
         voice = ElementTree.SubElement(body, 'voice')
         voice.set('{http://www.w3.org/XML/1998/namespace}lang', inputData['locale'])
         voice.set('{http://www.w3.org/XML/1998/namespace}gender', inputData['gender'])
         voice.set('name', inputData['name'])
         voice.text = self.text
         
         headers = {"Content-type": "application/ssml+xml", 
         			"X-Microsoft-OutputFormat": "riff-16khz-16bit-mono-pcm", 
         			"Authorization": "Bearer " + accesstoken, 
         			"User-Agent": "zAI"}
         
         conn = HTTPSConnection("speech.platform.bing.com")
         conn.request("POST", "/synthesize", ElementTree.tostring(body), headers)
         response = conn.getresponse()
     
         data = response.read()
         conn.close()
         if response.status == 200:
             with open(outputFile, "wb") as wavfile:
                 wavfile.write(bytes(data))
         else:
             raise Exception('Request failed: %s' % response.reason)
                            
         
     elif backend == 'local':
         raise NotImplementedError("tanslate method is currently not available with local backend")
     else:
         raise ValueError('invalid backend selection. Valid values are currently "Google", "Microsoft" or "local".')
username = os.environ["HAW_USER"]
password = os.environ["HAW_PASSWORD"]

userpass_string = username + ":" + password
userpass_string = userpass_string.encode()

# Crawl the Container Harbor to get the current containers.
# Only works, when there is just one set of PNS containers.
c = HTTPSConnection("pnskss.informatik.haw-hamburg.de",
                    context=ssl._create_unverified_context())
userAndPass = b64encode(userpass_string).decode("ascii")
headers = {
    'Authorization': 'Basic %s' % userAndPass,
    'Accept': 'application/json'
}
c.request('GET', '/containers', headers=headers)
res = c.getresponse()
data = res.read()

json_data = json.loads(data)

template_file = open('inventory.template', 'r')
src = Template(template_file.read())

# Save the information in a dict to generate the inventory template for ansible
result_dict = {}

for container in json_data['containers']:
    service_networks = container['details']['NetworkSettings']['Networks']
    service = container['details']['Config']['Labels'][
        'com.docker.compose.service']
Beispiel #51
0
def checkEWS(host, port, mode, domain, user, data):

    ews_url = "/EWS/Exchange.asmx"

    if port == 443:
        try:
            uv_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            session = HTTPSConnection(host, port, context=uv_context)
        except AttributeError:
            session = HTTPSConnection(host, port)
    else:
        session = HTTPConnection(host, port)

    # Use impacket for NTLM
    ntlm_nego = ntlm.getNTLMSSPType1(host, domain)

    #Negotiate auth
    negotiate = base64.b64encode(ntlm_nego.getData())
    # Headers
    headers = {
        "Authorization":
        'NTLM %s' % negotiate.decode('utf-8'),
        "Content-type":
        "text/xml; charset=utf-8",
        "Accept":
        "text/xml",
        "User-Agent":
        "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
    }

    session.request("POST", ews_url, POST_BODY, headers)

    res = session.getresponse()
    res.read()

    if res.status != 401:
        print(
            'Status code returned: %d. Authentication does not seem required for URL'
            % (res.status))
        return False
    try:
        if 'NTLM' not in res.getheader('WWW-Authenticate'):
            print('NTLM Auth not offered by URL, offered protocols: %s' %
                  (res.getheader('WWW-Authenticate')))
            return False
    except (KeyError, TypeError):
        print('No authentication requested by the server for url %s' %
              (ews_url))
        return False

    print('[*] Got 401, performing NTLM authentication')
    # Get negotiate data
    try:
        ntlm_challenge_b64 = re.search(
            'NTLM ([a-zA-Z0-9+/]+={0,2})',
            res.getheader('WWW-Authenticate')).group(1)
        ntlm_challenge = base64.b64decode(ntlm_challenge_b64)
    except (IndexError, KeyError, AttributeError):
        print('No NTLM challenge returned from server')
        return False

    if mode == 'plaintext':
        password1 = data
        nt_hash = ''

    elif mode == 'ntlmhash':
        password1 = ''
        nt_hash = binascii.unhexlify(data)

    else:
        print('[!]Wrong parameter')
        return False

    lm_hash = ''
    ntlm_auth, _ = ntlm.getNTLMSSPType3(ntlm_nego, ntlm_challenge, user,
                                        password1, domain, lm_hash, nt_hash)
    auth = base64.b64encode(ntlm_auth.getData())

    headers = {
        "Authorization":
        'NTLM %s' % auth.decode('utf-8'),
        "Content-type":
        "text/xml; charset=utf-8",
        "Accept":
        "text/xml",
        "User-Agent":
        "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
    }

    session.request("POST", ews_url, POST_BODY, headers)
    res = session.getresponse()
    body = res.read()
    if res.status == 401:
        print('[!] Server returned HTTP status 401 - authentication failed')
        return False

    else:
        print('[+] Valid:%s %s' % (user, data))
        #print(body)
        return True
Beispiel #52
0
 def request(self, method, url, body=None, headers={}):
     self.request_length = 0
     HTTPSConnection.request(self, method, url, body, headers)
Beispiel #53
0
from base64 import b64encode
from http.client import HTTPSConnection

#This sets up the https connection
c = HTTPSConnection("jira.aspiraconnect.com")
#we need to base 64 encode it
#and then decode it to acsii as python 3 stores it as a byte string
userAndPass = b64encode(b"gzhang:Pinwen@18").decode("ascii")
headers = {
    'Authorization': 'Basic %s' % userAndPass,
    'Content-Type': 'application/json'
}
#then connect

c.request('GET',
          '/rest/api/2/user/assignable/search?project=AWO&maxResults=1000',
          headers=headers)
#get the response back

res = c.getresponse().read()
# at this point you could check the status etc
# this gets the page text

data = json.loads(res)

# for issue_raw in data:
#     print(issue_raw["key"],issue_raw["emailAddress"],issue_raw["displayName"],issue_raw["active"])


def user_dict():
    users = {}
Beispiel #54
0
         }
     }
 }
 headers = {
     'Accept': 'application/json',
     'Connection': 'Keep-Alive',
     'Authorization': 'none',
     'Content-Type': 'application/json',
     'User-Agent': _USER_AGENT,
     'Host': user_server,
     'Accept-Encoding': 'gzip'
 }
 logging.debug('POST /users/sign_in.json, body=%r, headers=%r' %
               (json.dumps(query), headers))
 conn.request('POST',
              '/users/sign_in.json',
              body=json.dumps(query),
              headers=headers)
 resp = conn.getresponse()
 if resp.status != 200:
     logging.error('Failed to login to Hisense server:\nStatus %d: %r',
                   resp.status, resp.reason)
     sys.exit(1)
 resp_data = resp.read()
 try:
     resp_data = gzip.decompress(resp_data)
 except OSError:
     pass  # Not gzipped.
 try:
     tokens = json.loads(resp_data)
 except UnicodeDecodeError:
     logging.exception(
Beispiel #55
0
class urlopen(object):
    """HTTP(S) urlopen that handles compressed connections

    Errors that respond with bencoded data can be read

    Raises IOError on other errors
    """
    conntypes = {'http': HTTPConnection, 'https': HTTPSConnection}

    def __init__(self, url):
        self.tries = 0
        self.error_return = None
        self.connection = None
        self.url = None
        self._open(url.strip())

    def _setconn(self, url):
        scheme, host, path, params, query, _ = urllib.parse.urlparse(url)
        if scheme not in self.conntypes:
            raise IOError(('url error', 'unknown url type', scheme, url))

        if self.connection is not None and not (
                isinstance(self.connection, self.conntypes[scheme])
                and host == self.connection.host):
            try:
                self.connection.close()
            except socket.error:
                pass
            self.connection = None

        if self.connection is None:
            if scheme == 'http':
                self.connection = HTTPConnection(host, timeout=30)
            else:
                self.connection = HTTPSConnection(host,
                                                  timeout=30,
                                                  context=SSLCONTEXT)

        # a[:len(b)] == (a if b else '')
        self.url = path + ';'[:len(params)] + params + '?'[:len(query)] + query

    def _open(self, url):
        try:
            self._setconn(url)
        except HTTPException as e:
            raise IOError(('http error', str(e)))

        for _ in range(MAX_REDIRECTS):
            try:
                self.connection.request('GET', self.url, None, {
                    'User-Agent': VERSION,
                    'Accept-Encoding': 'gzip'
                })
                self.response = self.connection.getresponse()
                if self.response.status == 200:  # Success
                    return
                if self.response.status in (301, 302):  # Redirect
                    self._setconn(self.response.getheader('Location'))
                    continue
            except HTTPException as e:
                raise IOError(('http error', str(e)))

            # Handle bencoded errors
            try:
                data = self._read()
                d = bdecode(data)
                if 'failure reason' in d:
                    self.error_return = data
                    return
            except (IOError, ValueError):
                pass

            # General HTTP error
            raise IOError(
                ('http error', self.response.status, self.response.reason))
        else:
            raise IOError(('http error', 500,
                           "Internal Server Error: Redirect Recursion"))

    def read(self):
        """Read response"""
        if self.error_return:
            return self.error_return
        return self._read()

    def _read(self):
        data = self.response.read()
        if self.response.getheader('Content-Encoding', '').find('gzip') >= 0:
            try:
                data = gzip.GzipFile(fileobj=io.BytesIO(data)).read()
            except IOError:
                raise IOError(('http error', 'got corrupt response'))
        return data

    def close(self):
        """Close connection

        Always succeeds"""
        if self.connection is not None:
            try:
                self.connection.close()
            except socket.error:
                pass

    def __enter__(self):
        return self

    def __exit__(self, _type, _value, _traceback):
        self.close()
    def _test_connection_to_URL(self, url_str):
        """
        Internal function that tests the connection to a URL via a simple
        HTTP/1.1 HEAD request.  If a test connection cannot be established,
        raises an exception with a helpful diagnostic message (e.g. the
        HTTP Response status code & reason).

        :param url_str: full string for the URL to test a connection to
        :type url_str: str

        :return: the value of the HTTP Response 'Content-Type' header,
                 or None if no successful test connection was made
        """
        content_type_header = None
        try:
            url = urlparse(url_str)
            test_conn = None
            if url.scheme == 'https':
                test_conn = HTTPSConnection(url.netloc)
            elif url.scheme == 'http':
                test_conn = HTTPConnection(url.netloc)
            else:
                raise RuntimeError("  Error:  Cannot connect to URL '{}'\n" \
                                   "  Unsupported network protocol scheme " \
                                   "'{}' specified\n" \
                                   "  Supported schemes: http | https".format(
                                    url_str, url.scheme))
            test_conn.request('HEAD', url.path)
            response = test_conn.getresponse()
            content_type_header = response.getheader('Content-Type')
            if response.status != OK:    # i.e. "HTTP/1.1 200 OK"
                raise RuntimeError("  Error: Cannot connect to URL '{}'\n" \
                                   "  HTTP Response status code & reason: " \
                                   "{} {}".format(url_str, response.status,
                                                           response.reason))
        except ValueError as ve:    # Raised by urlparse(...)
            raise RuntimeError("  Error: Cannot connect to URL '{}'\n" \
                               "  URL string may be invalid.".format(url_str))
        except HTTPException as he: # Raised by HTTPSConnection(...)
                                    # or HTTPConnection(...)
            raise RuntimeError("  Error: Cannot connect to URL '{}'\n" \
                               "  HTTPException: {}\n" \
                               "  url.scheme : '{}'\n" \
                               "  url.netloc : '{}'\n" \
                               "  url.path   : '{}'\n".format(
                                url_str, str(he),
                                url.scheme, url.netloc, url.path))
        except OSError as oe:   # Catches any low-level socket errors
                                # raised by test_conn.request(...)
                                # -> _socket (socketmodule.c)
            raise RuntimeError("  Error: Cannot connect to URL '{}'\n" \
                               "  Socket Error: {}: {}\n" \
                               "  url.scheme : '{}'\n" \
                               "  url.netloc : '{}'\n" \
                               "  url.path   : '{}'\n".format(
                                url_str, type(oe).__name__, str(oe),
                                url.scheme, url.netloc, url.path))
        finally:
            if isinstance(test_conn, HTTPConnection):
                test_conn.close()

        return content_type_header
Beispiel #57
0
class Robot(_Robot):
    def __init__(self,
                 fci_ip,
                 dynamic_rel=1.0,
                 user=None,
                 password=None,
                 repeat_on_error=True,
                 stop_at_python_signal=True):
        super().__init__(fci_ip,
                         dynamic_rel=dynamic_rel,
                         repeat_on_error=repeat_on_error,
                         stop_at_python_signal=stop_at_python_signal)
        self.hostname = fci_ip
        self.user = user
        self.password = password

        self.client = None
        self.token = None

    @staticmethod
    def _encode_password(user, password):
        bs = ','.join([
            str(b)
            for b in hashlib.sha256((password + '#' + user +
                                     '@franka').encode('utf-8')).digest()
        ])
        return base64.encodebytes(bs.encode('utf-8')).decode('utf-8')

    def __enter__(self):
        self.client = HTTPSConnection(
            self.hostname,
            timeout=12,
            context=ssl._create_unverified_context())  # [s]
        self.client.connect()
        self.client.request('POST',
                            '/admin/api/login',
                            body=json.dumps({
                                'login':
                                self.user,
                                'password':
                                self._encode_password(self.user, self.password)
                            }),
                            headers={'content-type': 'application/json'})
        self.token = self.client.getresponse().read().decode('utf8')
        return self

    def __exit__(self, type, value, traceback):
        self.client.close()

    def start_task(self, task):
        self.client.request('POST',
                            '/desk/api/execution',
                            body=f'id={task}',
                            headers={
                                'content-type':
                                'application/x-www-form-urlencoded',
                                'Cookie': f'authorization={self.token}'
                            })
        return self.client.getresponse().read()

    def unlock_brakes(self):
        self.client.request('POST',
                            '/desk/api/robot/open-brakes',
                            headers={
                                'content-type':
                                'application/x-www-form-urlencoded',
                                'Cookie': f'authorization={self.token}'
                            })
        return self.client.getresponse().read()

    def lock_brakes(self):
        self.client.request('POST',
                            '/desk/api/robot/close-brakes',
                            headers={
                                'content-type':
                                'application/x-www-form-urlencoded',
                                'Cookie': f'authorization={self.token}'
                            })
        return self.client.getresponse().read()

    def move_async(self, *args) -> Thread:
        p = Thread(target=self.move, args=tuple(args), daemon=True)
        p.start()
        sleep(0.001)  # Sleep one control cycle
        return p

    def get_gripper(self):
        return _Gripper(self.fci_ip)
Beispiel #58
0
class XMLManager(object):
    def __init__(self, cls, db_name, db_user, db_passwd, db_host, db_port,
                 db_table, ddl_dir, enable_ssl):
        self.cls = cls
        if not db_name:
            db_name = cls.__name__.lower()
        self.db_name = db_name
        self.db_user = db_user
        self.db_passwd = db_passwd
        self.db_host = db_host
        self.db_port = db_port
        self.db_table = db_table
        self.ddl_dir = ddl_dir
        self.s3 = None
        self.converter = XMLConverter(self)
        self.impl = getDOMImplementation()
        self.doc = self.impl.createDocument(None, 'objects', None)

        self.connection = None
        self.enable_ssl = enable_ssl
        self.auth_header = None
        if self.db_user:
            import base64
            base64string = base64.encodestring(
                '%s:%s' % (self.db_user, self.db_passwd))[:-1]
            authheader = "Basic %s" % base64string
            self.auth_header = authheader

    def _connect(self):
        if self.db_host:
            if self.enable_ssl:
                from http.client import HTTPSConnection as Connection
            else:
                from http.client import HTTPConnection as Connection

            self.connection = Connection(self.db_host, self.db_port)

    def _make_request(self, method, url, post_data=None, body=None):
        """
        Make a request on this connection
        """
        if not self.connection:
            self._connect()
        try:
            self.connection.close()
        except:
            pass
        self.connection.connect()
        headers = {}
        if self.auth_header:
            headers["Authorization"] = self.auth_header
        self.connection.request(method, url, body, headers)
        resp = self.connection.getresponse()
        return resp

    def new_doc(self):
        return self.impl.createDocument(None, 'objects', None)

    def _object_lister(self, cls, doc):
        for obj_node in doc.getElementsByTagName('object'):
            if not cls:
                class_name = obj_node.getAttribute('class')
                cls = find_class(class_name)
            id = obj_node.getAttribute('id')
            obj = cls(id)
            for prop_node in obj_node.getElementsByTagName('property'):
                prop_name = prop_node.getAttribute('name')
                prop = obj.find_property(prop_name)
                if prop:
                    if hasattr(prop, 'item_type'):
                        value = self.get_list(prop_node, prop.item_type)
                    else:
                        value = self.decode_value(prop, prop_node)
                        value = prop.make_value_from_datastore(value)
                    setattr(obj, prop.name, value)
            yield obj

    def reset(self):
        self._connect()

    def get_doc(self):
        return self.doc

    def encode_value(self, prop, value):
        return self.converter.encode_prop(prop, value)

    def decode_value(self, prop, value):
        return self.converter.decode_prop(prop, value)

    def get_s3_connection(self):
        if not self.s3:
            self.s3 = boto.connect_s3(self.aws_access_key_id,
                                      self.aws_secret_access_key)
        return self.s3

    def get_list(self, prop_node, item_type):
        values = []
        try:
            items_node = prop_node.getElementsByTagName('items')[0]
        except:
            return []
        for item_node in items_node.getElementsByTagName('item'):
            value = self.converter.decode(item_type, item_node)
            values.append(value)
        return values

    def get_object_from_doc(self, cls, id, doc):
        obj_node = doc.getElementsByTagName('object')[0]
        if not cls:
            class_name = obj_node.getAttribute('class')
            cls = find_class(class_name)
        if not id:
            id = obj_node.getAttribute('id')
        obj = cls(id)
        for prop_node in obj_node.getElementsByTagName('property'):
            prop_name = prop_node.getAttribute('name')
            prop = obj.find_property(prop_name)
            value = self.decode_value(prop, prop_node)
            value = prop.make_value_from_datastore(value)
            if value != None:
                try:
                    setattr(obj, prop.name, value)
                except:
                    pass
        return obj

    def get_props_from_doc(self, cls, id, doc):
        """
        Pull out the properties from this document
        Returns the class, the properties in a hash, and the id if provided as a tuple
        :return: (cls, props, id)
        """
        obj_node = doc.getElementsByTagName('object')[0]
        if not cls:
            class_name = obj_node.getAttribute('class')
            cls = find_class(class_name)
        if not id:
            id = obj_node.getAttribute('id')
        props = {}
        for prop_node in obj_node.getElementsByTagName('property'):
            prop_name = prop_node.getAttribute('name')
            prop = cls.find_property(prop_name)
            value = self.decode_value(prop, prop_node)
            value = prop.make_value_from_datastore(value)
            if value != None:
                props[prop.name] = value
        return (cls, props, id)

    def get_object(self, cls, id):
        if not self.connection:
            self._connect()

        if not self.connection:
            raise NotImplementedError(
                "Can't query without a database connection")
        url = "/%s/%s" % (self.db_name, id)
        resp = self._make_request('GET', url)
        if resp.status == 200:
            doc = parse(resp)
        else:
            raise Exception("Error: %s" % resp.status)
        return self.get_object_from_doc(cls, id, doc)

    def query(self, cls, filters, limit=None, order_by=None):
        if not self.connection:
            self._connect()

        if not self.connection:
            raise NotImplementedError(
                "Can't query without a database connection")

        from urllib.parse import urlencode

        query = str(self._build_query(cls, filters, limit, order_by))
        if query:
            url = "/%s?%s" % (self.db_name, urlencode({"query": query}))
        else:
            url = "/%s" % self.db_name
        resp = self._make_request('GET', url)
        if resp.status == 200:
            doc = parse(resp)
        else:
            raise Exception("Error: %s" % resp.status)
        return self._object_lister(cls, doc)

    def _build_query(self, cls, filters, limit, order_by):
        import types
        if len(filters) > 4:
            raise Exception('Too many filters, max is 4')
        parts = []
        properties = cls.properties(hidden=False)
        for filter, value in filters:
            name, op = filter.strip().split()
            found = False
            for property in properties:
                if property.name == name:
                    found = True
                    if types.TypeType(value) == list:
                        filter_parts = []
                        for val in value:
                            val = self.encode_value(property, val)
                            filter_parts.append("'%s' %s '%s'" %
                                                (name, op, val))
                        parts.append("[%s]" % " OR ".join(filter_parts))
                    else:
                        value = self.encode_value(property, value)
                        parts.append("['%s' %s '%s']" % (name, op, value))
            if not found:
                raise Exception('%s is not a valid field' % name)
        if order_by:
            if order_by.startswith("-"):
                key = order_by[1:]
                type = "desc"
            else:
                key = order_by
                type = "asc"
            parts.append("['%s' starts-with ''] sort '%s' %s" %
                         (key, key, type))
        return ' intersection '.join(parts)

    def query_gql(self, query_string, *args, **kwds):
        raise NotImplementedError("GQL queries not supported in XML")

    def save_list(self, doc, items, prop_node):
        items_node = doc.createElement('items')
        prop_node.appendChild(items_node)
        for item in items:
            item_node = doc.createElement('item')
            items_node.appendChild(item_node)
            if isinstance(item, Node):
                item_node.appendChild(item)
            else:
                text_node = doc.createTextNode(item)
                item_node.appendChild(text_node)

    def save_object(self, obj, expected_value=None):
        """
        Marshal the object and do a PUT
        """
        doc = self.marshal_object(obj)
        if obj.id:
            url = "/%s/%s" % (self.db_name, obj.id)
        else:
            url = "/%s" % (self.db_name)
        resp = self._make_request("PUT", url, body=doc.toxml())
        new_obj = self.get_object_from_doc(obj.__class__, None, parse(resp))
        obj.id = new_obj.id
        for prop in obj.properties():
            try:
                propname = prop.name
            except AttributeError:
                propname = None
            if propname:
                value = getattr(new_obj, prop.name)
                if value:
                    setattr(obj, prop.name, value)
        return obj

    def marshal_object(self, obj, doc=None):
        if not doc:
            doc = self.new_doc()
        if not doc:
            doc = self.doc
        obj_node = doc.createElement('object')

        if obj.id:
            obj_node.setAttribute('id', obj.id)

        obj_node.setAttribute(
            'class',
            '%s.%s' % (obj.__class__.__module__, obj.__class__.__name__))
        root = doc.documentElement
        root.appendChild(obj_node)
        for property in obj.properties(hidden=False):
            prop_node = doc.createElement('property')
            prop_node.setAttribute('name', property.name)
            prop_node.setAttribute('type', property.type_name)
            value = property.get_value_for_datastore(obj)
            if value is not None:
                value = self.encode_value(property, value)
                if isinstance(value, list):
                    self.save_list(doc, value, prop_node)
                elif isinstance(value, Node):
                    prop_node.appendChild(value)
                else:
                    text_node = doc.createTextNode(
                        str(value).encode("ascii", "ignore"))
                    prop_node.appendChild(text_node)
            obj_node.appendChild(prop_node)

        return doc

    def unmarshal_object(self, fp, cls=None, id=None):
        if isinstance(fp, str) or isinstance(fp, str):
            doc = parseString(fp)
        else:
            doc = parse(fp)
        return self.get_object_from_doc(cls, id, doc)

    def unmarshal_props(self, fp, cls=None, id=None):
        """
        Same as unmarshalling an object, except it returns
        from "get_props_from_doc"
        """
        if isinstance(fp, str) or isinstance(fp, str):
            doc = parseString(fp)
        else:
            doc = parse(fp)
        return self.get_props_from_doc(cls, id, doc)

    def delete_object(self, obj):
        url = "/%s/%s" % (self.db_name, obj.id)
        return self._make_request("DELETE", url)

    def set_key_value(self, obj, name, value):
        self.domain.put_attributes(obj.id, {name: value}, replace=True)

    def delete_key_value(self, obj, name):
        self.domain.delete_attributes(obj.id, name)

    def get_key_value(self, obj, name):
        a = self.domain.get_attributes(obj.id, name)
        if name in a:
            return a[name]
        else:
            return None

    def get_raw_item(self, obj):
        return self.domain.get_item(obj.id)

    def set_property(self, prop, obj, name, value):
        pass

    def get_property(self, prop, obj, name):
        pass

    def load_object(self, obj):
        if not obj._loaded:
            obj = obj.get_by_id(obj.id)
            obj._loaded = True
        return obj
Beispiel #59
0
def payment_status(request):
    # Read notification parameters
    pos_id = request.POST["pos_id"]
    session_id = request.POST["session_id"]
    ts = request.POST["ts"]
    sig = request.POST["sig"]
    logger.info(
        "Payment status - pos_id: %s, session_id: %s, ts: %s, sig: %s" %
        (pos_id, session_id, ts, sig))
    check_sig(sig, (pos_id, session_id, ts))
    # Determine the status of transaction based on the notification
    c = HTTPSConnection("secure.payu.com")
    timestamp = str(int(time.time()))
    c.request(
        "POST",
        "/paygw/UTF/Payment/get/txt/",
        urlencode((
            ("pos_id", pos_id),
            ("session_id", session_id),
            ("ts", timestamp),
            ("sig", make_sig((pos_id, session_id, timestamp))),
        )),
        {
            "Content-type": "application/x-www-form-urlencoded",
            "Accept": "text/plain",
        },
    )
    raw_response = codecs.decode(c.getresponse().read(), "utf-8")
    r = {}
    for i in [
            i.split(":", 1) for i in raw_response.split("\n")
            if i.strip() != ""
    ]:
        r[i[0]] = i[1].strip()
    check_sig(
        r["trans_sig"],
        (
            r["trans_pos_id"],
            r["trans_session_id"],
            r["trans_order_id"],
            r["trans_status"],
            r["trans_amount"],
            r["trans_desc"],
            r["trans_ts"],
        ),
    )
    amount = math.floor(int(r["trans_amount"]) / 100)
    # Update the corresponding payment
    # TODO: use update_or_create in Django 1.7
    p, created = Payment.objects.select_for_update().get_or_create(
        session_id=r["trans_session_id"],
        defaults={
            "order_id": r["trans_order_id"],
            "amount": amount,
            "description": r["trans_desc"],
        },
    )

    if p.amount != amount:
        logger.error(
            "Payment amount doesn't match",
            extra={
                "pay_type": p.pay_type,
                "status": p.status,
                "payment_response": r,
                "expected_amount": p.amount,
                "request": request,
            },
        )
        return HttpResponse("Bad amount", status=400)
    p.pay_type = r["trans_pay_type"]
    p.status = r["trans_status"]
    if r["trans_recv"] != "":
        p.realized = r["trans_recv"]
    p.save()

    logger.info(
        "Payment status: pay_type: %s, status: %s, payment response: %s" %
        (p.pay_type, p.status, r))

    # Return positive error code as per PayU protocol
    return HttpResponse("OK")
Beispiel #60
0
class GraphAPI(object):
    """A client for the Facebook Graph API.

    See http://developers.facebook.com/docs/api for complete
    documentation for the API.

    The Graph API is made up of the objects in Facebook (e.g., people,
    pages, events, photos) and the connections between them (e.g.,
    friends, photo tags, and event RSVPs). This client provides access
    to those primitive types in a generic way. For example, given an
    OAuth access token, this will fetch the profile of the active user
    and the list of the user's friends:

       graph = facebook.GraphAPI(access_token)
       user = graph.get_object("me")
       friends = graph.get_connections(user["id"], "friends")

    You can see a list of all of the objects and connections supported
    by the API at http://developers.facebook.com/docs/reference/api/.

    You can obtain an access token via OAuth or by using the Facebook
    JavaScript SDK. See
    http://developers.facebook.com/docs/authentication/ for details.

    If you are using the JavaScript SDK, you can use the
    get_user_from_cookie() method below to get the OAuth access token
    for the active user from the cookie saved by the SDK.

    """
    def __init__(self, access_token=None, timeout=None):
        self.access_token = access_token
        self.timeout = timeout
        try:
            self.conn = HTTPSConnection('graph.facebook.com',
                                        timeout=self.timeout)
        except TypeError:
            # Timeout support for Python <2.6
            if self.timeout:
                socket.setdefaulttimeout(self.timeout)
            self.conn = HTTPSConnection('graph.facebook.com')

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

    def get_object(self, id, **args):
        """Fetchs the given object from the graph."""
        return self.request(id, args)

    def get_objects(self, ids, **args):
        """Fetchs all of the given object from the graph.

        We return a map from ID to object. If any of the IDs are
        invalid, we raise an exception.
        """
        args["ids"] = ",".join(ids)
        return self.request("", args)

    def get_connections(self, id, connection_name, **args):
        """Fetchs the connections for given object."""
        return self.request(id + "/" + connection_name, args)

    def put_object(self, parent_object, connection_name, **data):
        """Writes the given object to the graph, connected to the given parent.

        For example,

            graph.put_object("me", "feed", message="Hello, world")

        writes "Hello, world" to the active user's wall. Likewise, this
        will comment on a the first post of the active user's feed:

            feed = graph.get_connections("me", "feed")
            post = feed["data"][0]
            graph.put_object(post["id"], "comments", message="First!")

        See http://developers.facebook.com/docs/api#publishing for all
        of the supported writeable objects.

        Certain write operations require extended permissions. For
        example, publishing to a user's feed requires the
        "publish_actions" permission. See
        http://developers.facebook.com/docs/publishing/ for details
        about publishing permissions.

        """
        assert self.access_token, "Write operations require an access token"
        return self.request(parent_object + "/" + connection_name,
                            post_args=data)

    def put_wall_post(self, message, attachment={}, profile_id="me"):
        """Writes a wall post to the given profile's wall.

        We default to writing to the authenticated user's wall if no
        profile_id is specified.

        attachment adds a structured attachment to the status message
        being posted to the Wall. It should be a dictionary of the form:

            {"name": "Link name"
             "link": "http://www.example.com/",
             "caption": "{*actor*} posted a new review",
             "description": "This is a longer description of the attachment",
             "picture": "http://www.example.com/thumbnail.jpg"}

        """
        return self.put_object(profile_id,
                               "feed",
                               message=message,
                               **attachment)

    def put_comment(self, object_id, message):
        """Writes the given comment on the given post."""
        return self.put_object(object_id, "comments", message=message)

    def put_like(self, object_id):
        """Likes the given post."""
        return self.put_object(object_id, "likes")

    def delete_object(self, id):
        """Deletes the object with the given ID from the graph."""
        self.request(id, post_args={"method": "delete"})

    def delete_request(self, user_id, request_id):
        """Deletes the Request with the given ID for the given user."""
        conn = HTTPSConnection('graph.facebook.com')

        url = '/%s_%s?%s' % (
            request_id,
            user_id,
            urlencode({'access_token': self.access_token}),
        )
        conn.request('DELETE', url)
        response = conn.getresponse()
        data = response.read()

        response = _parse_json(data)
        # Raise an error if we got one, but don't not if Facebook just
        # gave us a Bool value
        if (response and isinstance(response, dict) and response.get("error")):
            raise GraphAPIError(response)

        conn.close()

    def put_photo(self, image, message=None, album_id=None, **kwargs):
        """Uploads an image using multipart/form-data.

        image=File like object for the image
        message=Caption for your image
        album_id=None posts to /me/photos which uses or creates and uses
        an album for your application.

        """
        object_id = album_id or "me"
        #it would have been nice to reuse self.request;
        #but multipart is messy in urllib
        post_args = {
            'access_token': self.access_token,
            'source': image,
            'message': message,
        }
        post_args.update(kwargs)
        content_type, body = self._encode_multipart_form(post_args)
        req = Request(("https://graph.facebook.com/%s/photos" % object_id),
                      data=body)
        req.add_header('Content-Type', content_type)
        try:
            data = urlopen(req).read()
        #For Python 3 use this:
        #except HTTPError as e:
        except HTTPError as e:
            data = e.read()  # Facebook sends OAuth errors as 400, and urllib2
            # throws an exception, we want a GraphAPIError
        try:
            response = _parse_json(data)
            # Raise an error if we got one, but don't not if Facebook just
            # gave us a Bool value
            if (response and isinstance(response, dict)
                    and response.get("error")):
                raise GraphAPIError(response)
        except ValueError:
            response = data

        return response

    # based on: http://code.activestate.com/recipes/146306/
    def _encode_multipart_form(self, fields):
        """Encode files as 'multipart/form-data'.

        Fields are a dict of form name-> value. For files, value should
        be a file object. Other file-like objects might work and a fake
        name will be chosen.

        Returns (content_type, body) ready for httplib.HTTP instance.

        """
        BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
        CRLF = '\r\n'
        L = []
        for (key, value) in fields.items():
            logging.debug("Encoding %s, (%s)%s" % (key, type(value), value))
            if not value:
                continue
            L.append('--' + BOUNDARY)
            if hasattr(value, 'read') and callable(value.read):
                filename = getattr(value, 'name', '%s.jpg' % key)
                L.append(('Content-Disposition: form-data;'
                          'name="%s";'
                          'filename="%s"') % (key, filename))
                L.append('Content-Type: image/jpeg')
                value = value.read()
                logging.debug(type(value))
            else:
                L.append('Content-Disposition: form-data; name="%s"' % key)
            L.append('')
            if isinstance(value, unicode):
                logging.debug("Convert to ascii")
                value = value.encode('ascii')
            L.append(value)
        L.append('--' + BOUNDARY + '--')
        L.append('')
        body = CRLF.join(L)
        content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
        return content_type, body

    def request(self, path, args=None, post_args=None):
        """Fetches the given path in the Graph API.

        We translate args to a valid query string. If post_args is
        given, we send a POST request to the given path with the given
        arguments.

        """
        args = args or {}

        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        post_data = None if post_args is None else urlencode(post_args)
        try:
            self.conn.request('GET', "/" + path + "?" + urlencode(args),
                              post_data)
            file = self.conn.getresponse()
        except HTTPError as e:
            response = _parse_json(e.read())
            raise GraphAPIError(response)

        charsetStr = "charset="
        mimetype = file.getheader('content-type')
        if "text" in mimetype:
            encoding = mimetype[mimetype.find(charsetStr) + len(charsetStr):]
            response = _parse_json(file.read().decode(encoding))
        elif "image" in mimetype:
            response = {
                "data": file.read(),
                "mime-type": mimetype,
                "url": file.getheader('location'),
            }
        else:
            raise GraphAPIError('Maintype was not text or image')

        if response and isinstance(response, dict) and response.get("error"):
            raise GraphAPIError(response["error"]["type"],
                                response["error"]["message"])
        return response

    def fql(self, query, args=None, post_args=None):
        """FQL query.

        Example query: "SELECT affiliations FROM user WHERE uid = me()"

        """
        args = args or {}
        if self.access_token:
            if post_args is not None:
                post_args["access_token"] = self.access_token
            else:
                args["access_token"] = self.access_token
        post_data = None if post_args is None else urlencode(post_args)
        """Check if query is a dict and
           use the multiquery method
           else use single query
        """
        if not isinstance(query, basestring):
            args["queries"] = query
            fql_method = 'fql.multiquery'
        else:
            args["query"] = query
            fql_method = 'fql.query'

        args["format"] = "json"

        try:
            file = urlopen("https://api.facebook.com/method/" + fql_method +
                           "?" + urlencode(args),
                           post_data,
                           timeout=self.timeout)
        except TypeError:
            # Timeout support for Python <2.6
            if self.timeout:
                socket.setdefaulttimeout(self.timeout)
            file = urlopen(
                "https://api.facebook.com/method/" + fql_method + "?" +
                urlencode(args), post_data)

        try:
            content = file.read()
            response = _parse_json(content)
            #Return a list if success, return a dictionary if failed
            if type(response) is dict and "error_code" in response:
                raise GraphAPIError(response)
        except Exception as e:
            raise e
        finally:
            file.close()

        return response

    def extend_access_token(self, app_id, app_secret):
        """
        Extends the expiration time of a valid OAuth access token. See
        <https://developers.facebook.com/roadmap/offline-access-removal/
        #extend_token>

        """
        args = {
            "client_id": app_id,
            "client_secret": app_secret,
            "grant_type": "fb_exchange_token",
            "fb_exchange_token": self.access_token,
        }
        response = urllib.urlopen("https://graph.facebook.com/oauth/"
                                  "access_token?" + urlencode(args)).read()
        query_str = parse_qs(response)
        if "access_token" in query_str:
            result = {"access_token": query_str["access_token"][0]}
            if "expires" in query_str:
                result["expires"] = query_str["expires"][0]
            return result
        else:
            response = json.loads(response)
            raise GraphAPIError(response)