Mercurial > odfcast
annotate 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 |
rev | line source |
---|---|
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 import optparse |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 import sys |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 import requests |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 url = "http://127.0.0.1:5000/convert/" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 def main(): |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 usage = "usage: %prog [options] infile outfile" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 parser = optparse.OptionParser(usage=usage) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 parser.add_option("-f", "--format", default="pdf") |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
16 parser.add_option("-s", "--host", default="localhost") |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
17 parser.add_option("-p", "--port", default="5000") |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 (options, args) = parser.parse_args() |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
19 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 if len(args) < 2: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 parser.print_usage() |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 sys.exit(1) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 url = "http://%s:%s/convert/" % (options.host, options.port) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 infilename = args[0] |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 outfilename = args[1] |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 format = options.format |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 files = {'file': open(infilename, 'rb')} |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 data = {'format': format} |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 r = requests.post(url, data=data, files=files) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 if r.status_code == 200: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 with open(outfilename, "wb") as f: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 f.write(r.content) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 print "OK" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 else: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 print "An error has occured" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 print r.status_code, r.headers |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 sys.exit(2) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 if __name__ == "__main__": |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 main() |