annotate mergeclient.py @ 69:dcf870775c26

Add compability with flask 0.8 In flask version 0.10 (current stable version) the behaviour changed. With this version it is possible to specify the server and port via the config variable SERVER_NAME. To allow this behaviour also with flask 0.8 which is the version shipped with Debian Wheezy this commit extracts the host and port from the SERVER_NAME config variable.
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 05 Jan 2015 12:37:56 +0100
parents 4645e50539ff
children 349d49bb69f4
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")
63
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
16 parser.add_option("-i", "--ignore-file-errors", action="store_true",
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
17 default=False, dest="ignorefileerrors")
68
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
18 parser.add_option("-m", "--multi", action="store_true",
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
19 default=False, dest="multi")
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 (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
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 if len(args) < 2:
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 parser.print_usage()
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24 sys.exit(1)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26 service = "merge"
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27 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
28
63
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
29 if options.ignorefileerrors:
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
30 url += "?ignore_file_errors=1"
3918c3c69485 Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents: 21
diff changeset
31
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32 files = []
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
33
68
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
34 if options.multi:
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
35 for i, filename in enumerate(args):
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
36 files.append(("file%s" % i, open(filename, 'rb')))
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
37 else:
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
38 for filename in args:
4645e50539ff Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents: 63
diff changeset
39 files.append(('files', open(filename, 'rb')))
21
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41 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
42
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
43 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
44 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
45 f.write(r.content)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
46 print "OK"
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 else:
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 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
49 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
50 print r.text
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 sys.exit(2)
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 if __name__ == "__main__":
a8c628466a9d Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
54 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)