############################################################################################# # 2022-09-20 # Python example script using pycurl with EarthScope Command Line Interface (CLI) to # authenticate for data access at data-idm.unavco.org (soon to be needed for data.unavco.org) # + Calls the CLI in a subprocess to get an access token (CLI refreshes tokens automatically, as needed) # + See instructions for registering for data access and setting up and using the CLI at: # https://www.unavco.org/data/gps-gnss/file-server/command-line-interface.html # + Takes command line argument for URL # e.g., https://data-idm.unavco.org/archive/gnss/rinex/obs/2022/060/p1230600.22d.Z # Usage: python3 python-pycurl-cli-example.py https://data-idm.unavco.org/archive/gnss/rinex/obs/2022/060/p1230600.22d.Z # # Please take this basic script and use/extend it for your needs. ############################################################################################ import subprocess import sys from pycurl import Curl # Reads the URL from the command line argument. url = sys.argv[1] # Assigns the local downloaded file's name from the last part of the URL. filename = url.split('/')[-1] # Run the CLI to get token. es_return = subprocess.run(['es', 'sso', 'access', '--token'], capture_output=True) es_stdout = es_return.stdout.decode().rstrip() # Read subprocess standard output, convert binary to string and remove trailing white space ############################################ ## Make cURL request of URL, and write file. ############################################ curl = Curl() # Set protocol to HTTP1.1 vs default HTTP2. For installs of libcurl/pycurl built against older versions of openssl that don't support the ALPN used for HTTP2. # Note: HTTP2 is a more performant protocal. To use it comment out the following line. curl.setopt(curl.HTTP_VERSION, curl.CURL_HTTP_VERSION_1_1) # Turn on verbose output for debugging purposes. curl.setopt(curl.VERBOSE, 1) curl.setopt(curl.URL, url) curl.setopt(curl.FOLLOWLOCATION, True) # Pass in the token as a http header. curl.setopt(curl.HTTPHEADER, [f'Authorization: Bearer {es_stdout}']) # Save file with original filename from url. with open(filename, 'wb') as bf: curl.setopt(curl.WRITEDATA, bf) curl.perform() curl.close()