maxcutler / python-wordpress-xmlrpc

Python library for WordPress XML-RPC integration
http://python-wordpress-xmlrpc.rtfd.org
MIT License
383 stars 130 forks source link

Error control of Client function in python-wordpress-xmlrpc #151

Closed huangwb8 closed 2 years ago

huangwb8 commented 2 years ago

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?

huangwb8 commented 2 years ago

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)