All Apps and Add-ons

Need help with python script

KulvinderSingh
Path Finder

trying to get a script working 

import urllib.request, json
try:
with urllib.request.urlopen("XXXXXXXXX.json") as url:
data = json.loads(url.read().decode())
print("data received")
for item in data:
print(item)
except urllib.error.URLError as e:
print(f"Request failed with error: (e)")

this works fine and fetches the data but  I need this to pass through proxy server when I try that it does not work.. any help is apprecaited.

 

Labels (1)
0 Karma

splunkernator
Path Finder

I normally use requests when handling http or rest calls with python, as I find it really useful

Requests has methods for deserialising json as well and options to configure proxies as well

you might find this helpful?

https://stackoverflow.com/questions/8287628/proxies-with-python-requests-module

you can then handle your own exceptions based on the http response - the useless facts api is handy for testing if you want to avoid prod

if you need to handle authentication on the proxy that may be more complex

e.g.

import requests


def get_content(base, route, proxies):
    url = f'{base}/{route}'
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.get(url, headers=headers, proxies=proxies)
    return response.json()


def main():
    base = "https://uselessfacts.jsph.pl"
    route = "api/v2/facts/random"
    http_proxy = "http://10.10.1.10:3128"
    https_proxy = "https://10.10.1.11:1080"
    ftp_proxy = "ftp://10.10.1.10:3128"
    
    proxies = {
        "http": http_proxy,
        "https": https_proxy,
        "ftp": ftp_proxy
    }
    response = get_content(base, route, proxies)
    print(response)


if __name__ == "__main__":
    main()

  

KulvinderSingh
Path Finder

Will give it try and let you know how it goes.

0 Karma
Get Updates on the Splunk Community!

Enterprise Security Content Update (ESCU) | New Releases

In December, the Splunk Threat Research Team had 1 release of new security content via the Enterprise Security ...

Why am I not seeing the finding in Splunk Enterprise Security Analyst Queue?

(This is the first of a series of 2 blogs). Splunk Enterprise Security is a fantastic tool that offers robust ...

Index This | What are the 12 Days of Splunk-mas?

December 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...