maxcutler / python-wordpress-xmlrpc

Python library for WordPress XML-RPC integration

Home Page:http://python-wordpress-xmlrpc.rtfd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error control of Client function in python-wordpress-xmlrpc

huangwb8 opened this issue · comments

Here is a common code for request xmlrpc of WordPress:

 client = Client(domain + '/xmlrpc.php', username, password)

Sometimes I would meet "TimeOutError". I want to do something when this error appears.

For example:

def get_page(url):
    try:
        response = requests.get(url, timeout=1)
        if response.status_code == 200:
            return response.text
        else:
            print('Get Page Failed', response.status_code)
            return None
    except (ConnectionError, ReadTimeout):
        print('Crawling Failed', url)
        return None
def main():
    url = 'https://www.baidu.com'
    print(get_page(url))
if __name__ == '__main__':
    main()

Where the output of requests.get has an attribute called status_code.

Is it possible to manage errors in this way for Client function in python-wordpress-xmlrpc?

Successful codes is as follows:

def wp_xmlrpc(domain,username, password):
    """
    Error control
    """
    try:
        client = Client(domain + '/xmlrpc.php', username, password) 
        print('SUCCESS to connect to your WordPress website: ' + domain)
        return client
    except Exception as e:
        print('FAILED to connect to your WordPress website: ' + str(e))
        sys.exit(0)