Skip to content

Instantly share code, notes, and snippets.

@toniher
Created March 7, 2019 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toniher/ab980f42298b27e15fd0bb29c16e02a8 to your computer and use it in GitHub Desktop.
Save toniher/ab980f42298b27e15fd0bb29c16e02a8 to your computer and use it in GitHub Desktop.
Webservice script for using pergola through the Web
# -*- coding: utf-8 -*-
"""
Pergola webservice client
~~~~~~~~~~~~~~~~~~~~~~~~~~~
More details at: https://pergola.crg.eu
Usage: python pergola-webservice.py myconfig.json myoutput.zip
"""
import requests
import json
import sys
import os
# For debugging
import pprint
pp = pprint.PrettyPrinter(depth=6)
def read_config( configfile ):
import json
config = None
with open(configfile) as data_file:
config = json.load(data_file)
return config
# Read Example JSON file
config = None
# Default output file
outputfile = "output.zip"
if len( sys.argv ) > 1 :
configfile = sys.argv[1]
config = read_config( configfile )
if len( sys.argv ) > 2 :
outputfile = sys.argv[2]
if config :
if 'url' in config :
url = config['url']
else :
url = "https://pergola.crg.eu"
files = []
# http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
if 'files' in config:
for key in config['files'] :
if type(config['files'][key]) is list :
for f in config['files'][key] :
files.append( ( key, ( f['filename'], open( f['content-file']), f['content-type'] ) ) )
else :
files.append( ( key, ( config['files'][key]['filename'], open( config['files'][key]['content-file']), config['files'][key]['content-type'] ) ) )
# pp.pprint( files )
response = requests.post( url+"/submit", data=config['params'], files=files, verify=False )
if response and response.content and response.status_code == 200 :
content = json.loads( response.content )
if content["status"] == "OK" :
if content["session"] :
download_url = url+"/download?session="+content["session"]+"&out=output.zip&format=zipdir"
response_down = requests.get( download_url, allow_redirects=True, verify=False )
open( outputfile, 'wb' ).write( response_down.content )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment