示例#1
0
def request_post(url,payload=None,timeout=16):
    """For a given url and payload, makes a post request and returns the response.

    :param url: The url to send a post request to.
    :type url: str
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :param timeout: The time for the post to wait for a response. Should be slightly greater than multiples of 3.
    :type timeout: Optional[int]
    :returns: Returns the data from the post request.

    """
    try:
        res = Session.post(url, data=payload, timeout=timeout)
        data = res.json()
        if 'challenge' in data:
            return data
        res.raise_for_status()
        if 'mfa_required' in data:
            mfa_token = input("Please Type In The MFA Code: ")
            payload['mfa_code'] = mfa_token
            res = Session.post(url, data=payload, timeout=timeout)
            while (res.status_code != 200):
                mfa_token = input("That MFA code was not correct. Please Type In Another MFA Code: ")
                payload['mfa_code'] = mfa_token
                res = Session.post(url, data=payload, timeout=timeout)
            data = res.json()
    except (requests.exceptions.HTTPError,AttributeError) as message:
        data = None
        print(message)

    return data
示例#2
0
def request_post(url,payload=None,timeout=16):
    """For a given url and payload, makes a post request and returns the response.

    :param url: The url to send a post request to.
    :type url: str
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :param timeout: The time for the post to wait for a response. Should be slightly greater than multiples of 3.
    :type timeout: Optional[int]
    :returns: Returns the data from the post request.

    """
    try:
        res = Session.post(url, data=payload, timeout=timeout)
        res.raise_for_status()
        data = res.json()
    except (requests.exceptions.HTTPError,AttributeError) as message:
        try:
            print("HTTPError: {}".format(data))
        except:
            print("HTTPError: [fail to print resp.json()]")
        data = None
        print(message)

    return data
示例#3
0
def request_post(url, payload=None, timeout=16, json=False):
    """For a given url and payload, makes a post request and returns the response.

    :param url: The url to send a post request to.
    :type url: str
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :param timeout: The time for the post to wait for a response. Should be slightly greater than multiples of 3.
    :type timeout: Optional[int]
    :param json: This is used for buying and selling options.
    :type json: bool
    :returns: Returns the data from the post request.

    """
    try:
        if json:
            update_session('Content-Type', 'application/json')
            res = Session.post(url, json=payload, timeout=timeout)
            update_session('Content-Type',
                           'application/x-www-form-urlencoded; charset=utf-8')
        else:
            res = Session.post(url, data=payload, timeout=timeout)
        data = res.json()
        if 'challenge' in data:
            return data
        res.raise_for_status()
        if 'mfa_required' in data:
            mfa_token = input("Please Type In The MFA Code: ")
            payload['mfa_code'] = mfa_token
            res = Session.post(url, data=payload, timeout=timeout)
            while (res.status_code != 200):
                mfa_token = input(
                    "That MFA code was not correct. Please Type In Another MFA Code: "
                )
                payload['mfa_code'] = mfa_token
                res = Session.post(url, data=payload, timeout=timeout)
            data = res.json()
    except (requests.exceptions.HTTPError, AttributeError) as message:
        data = None
        print(message)

    return data