Client of redundant servers
Client of redundant servers
Generic client of redundant servers. A simple framework to make requests of unreliable servers. Throws an exception if no servers are available, otherwise returns a result from the first server that doesn't fail. Supports round-robin, fixed, and random orders of servers.
The intention is that you can use this to glue together things that are otherwise slightly tedious, using your own client classes which inherit from ClientOfRedundantServers.
pip install client-of-redundant-servers
Say you have some web servers, all serving the same content, and you want to get a file from any one. You don't care which server responds, but you don't want to have to manually look for failures and try again.
client_of_redundant_servers lets you write things like this:
import requests import client_of_redundant_servers as cors from collections import OrderedDict class ClientOfRedundantWebServers(cors.ClientOfRedundantServers): def __init__(self, url_list: list): # Super().__init__ wants a dict, but this example is so simple that # a list can be used to create an OrderedDict of None. url_dict = OrderedDict((url, None) for url in url_list) super().__init__(url_dict) def _get_file_func(self, url): try: r = requests.get(url) # Check for errors that didn't raise a requests.exception if not r.ok: raise cors.CurrentServerFailed return r except requests.exceptions.RequestException: raise cors.CurrentServerFailed def get_file(self): return self.request(self._get_file_func) # Only picking on Ubuntu because it is widely mirrored. urls = ["http://badserver.example.com/badfile", "http://www.mirrorservice.org/sites/cdimage.ubuntu.com/cdimage/releases/16.04/release/SHA256SUMS", "http://nl.archive.ubuntu.com/ubuntu-cdimages/16.04/release/SHA256SUMS"] client = ClientOfRedundantWebServers(urls) try: r = client.get_file() print(r.text) print("*****") print("Retrieved from : " + r.url) except cors.AllAvailableServersFailed: print("Error! No servers were available to service the request.")
If you run that, you'll see that the client tries to retrieve the file from
which fails, so it continues to try the next available server.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size client_of_redundant_servers-0.6-py3-none-any.whl (9.4 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size client_of_redundant_servers-0.6.tar.gz (6.8 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for client_of_redundant_servers-0.6-py3-none-any.whl
Hashes for client_of_redundant_servers-0.6.tar.gz