Mercurial > odfcast
annotate mergeclient.py @ 21:a8c628466a9d
Add an example client for MergeView based on requests
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 29 Sep 2014 16:00:19 +0200 |
parents | |
children | 3918c3c69485 |
rev | line source |
---|---|
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 import optparse |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 import sys |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 import requests |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 def main(): |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 usage = "usage: %prog [options] file1 file2 [ file3, ...]" |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 parser = optparse.OptionParser(usage=usage) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 parser.add_option("-s", "--host", default="localhost") |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 parser.add_option("-p", "--port", default="5000") |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 parser.add_option("-o", "--out", default="merged.pdf", dest="out") |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
16 (options, args) = parser.parse_args() |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
17 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 if len(args) < 2: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
19 parser.print_usage() |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 sys.exit(1) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 service = "merge" |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 url = "http://%s:%s/%s/" % (options.host, options.port, service) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 files = [] |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 for filename in args: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 files.append(('files', open(filename, 'rb'))) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 r = requests.post(url, files=files) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 if r.status_code == 200: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 with open(options.out, "wb") as f: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 f.write(r.content) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 print "OK" |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 else: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 print "An error has occured" |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 print r.status_code, r.headers |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 print r.text |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 sys.exit(2) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 if __name__ == "__main__": |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 main() |