import requests
# disable warnings caused by using requests with verify=False argument
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
# helper function to check request response; may raise Exception
def _check_response(res, error_message):
if res.status_code != 200:
message = "EI Server replied with: {0} ({1}).".format(res.status_code, res.reason)
if error_message:
message = "{0}. {1}".format(error_message, message)
raise Exception(message)
def get_token(user, password, server_address, server_port):
server = "https://{0}:{1}/".format(server_address, server_port)
response = requests.put(server + "FRONTEND/LOGIN", verify=False,
json={"username": user, "password": password})
_check_response(response, "Login failed")
return {"server": server, "token": response.headers.get("X-Security-Token")}
def ban_hash(token, sha1, should_clean=True, comment=""):
headers = {"Authorization":"Bearer {0}".format(token["token"])}
response = requests.put(token["server"] + "FRONTEND/HASHES/BLOCK", headers=headers, verify=False,
json={"sha1": [sha1], "shouldClean": should_clean, "comment": comment})
_check_response(response, "Ban hash failed")
token = get_token("More", "supersecretpassword", "localhost", 8889)
ban_hash(token, "1234567890abcdef1234567890abcdef12345678")
|