bjoern@0: #!/usr/bin/env python bjoern@0: # -*- coding: utf-8 -*- bjoern@0: bjoern@0: import optparse bjoern@0: import sys bjoern@0: bjoern@0: import requests bjoern@0: bjoern@0: bjoern@0: def main(): bjoern@0: usage = "usage: %prog [options] infile outfile" bjoern@0: parser = optparse.OptionParser(usage=usage) bjoern@0: parser.add_option("-f", "--format", default="pdf") bjoern@0: parser.add_option("-s", "--host", default="localhost") bjoern@0: parser.add_option("-p", "--port", default="5000") bjoern@7: parser.add_option("--json", dest="json", metavar="FILE") bjoern@0: (options, args) = parser.parse_args() bjoern@0: bjoern@0: if len(args) < 2: bjoern@0: parser.print_usage() bjoern@0: sys.exit(1) bjoern@0: bjoern@35: url = "http://%s:%s/convert/" % (options.host, options.port) bjoern@0: infilename = args[0] bjoern@0: outfilename = args[1] bjoern@0: format = options.format bjoern@0: bjoern@0: files = {'file': open(infilename, 'rb')} bjoern@0: data = {'format': format} bjoern@0: bjoern@7: if options.json: bjoern@7: with open(options.json, "r") as f: bjoern@7: data["datadict"] = f.read() bjoern@7: bjoern@0: r = requests.post(url, data=data, files=files) bjoern@0: bjoern@0: if r.status_code == 200: bjoern@0: with open(outfilename, "wb") as f: bjoern@0: f.write(r.content) bjoern@0: print "OK" bjoern@0: else: bjoern@0: print "An error has occured" bjoern@0: print r.status_code, r.headers bjoern@7: print r.text bjoern@0: sys.exit(2) bjoern@0: bjoern@0: bjoern@0: if __name__ == "__main__": bjoern@0: main()