Mercurial > odfcast
diff castclient.py @ 0:4a34f72f036b
Add initial conversion service
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 26 Sep 2014 14:21:21 +0200 |
parents | |
children | c04edbd52967 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/castclient.py Fri Sep 26 14:21:21 2014 +0200 @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import optparse +import sys + +import requests + +url = "http://127.0.0.1:5000/convert/" + + +def main(): + usage = "usage: %prog [options] infile outfile" + parser = optparse.OptionParser(usage=usage) + parser.add_option("-f", "--format", default="pdf") + parser.add_option("-s", "--host", default="localhost") + parser.add_option("-p", "--port", default="5000") + (options, args) = parser.parse_args() + + if len(args) < 2: + parser.print_usage() + sys.exit(1) + + url = "http://%s:%s/convert/" % (options.host, options.port) + infilename = args[0] + outfilename = args[1] + format = options.format + + files = {'file': open(infilename, 'rb')} + data = {'format': format} + + r = requests.post(url, data=data, files=files) + + if r.status_code == 200: + with open(outfilename, "wb") as f: + f.write(r.content) + print "OK" + else: + print "An error has occured" + print r.status_code, r.headers + sys.exit(2) + + +if __name__ == "__main__": + main()