annotate checkclient.py @ 74:22ce0dc2e71c

Add client to test demonstrate the check feature
author Frank Koormann <frank.koormann@intevation.de>
date Fri, 06 Mar 2015 15:27:12 +0100
parents
children 3928af61b4ce
rev   line source
74
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
1 #!/usr/bin/env python
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
3
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
4 import optparse
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
5 import sys
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
6
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
7 import requests
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
8
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
9
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
10 def main():
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
11 usage = "usage: %prog [options] infile"
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
12 parser = optparse.OptionParser(usage=usage)
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
13 parser.add_option("-s", "--host", default="localhost")
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
14 parser.add_option("-p", "--port", default="5000")
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
15 (options, args) = parser.parse_args()
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
16
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
17 if len(args) < 2:
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
18 parser.print_usage()
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
19 sys.exit(1)
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
20
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
21 url = "http://%s:%s/check/" % (options.host, options.port)
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
22 infilename = args[0]
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
23 format = options.format
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
24
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
25 files = {'file': open(infilename, 'rb')}
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
26
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
27 r = requests.post(url, data=data, files=files)
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
28
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
29 if r.status_code == 200:
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
30 with open(outfilename, "wb") as f:
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
31 f.write(r.content)
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
32 print "OK"
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
33 else:
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
34 print "An error has occured"
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
35 print r.status_code, r.headers
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
36 print r.text
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
37 sys.exit(2)
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
38
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
39
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
40 if __name__ == "__main__":
22ce0dc2e71c Add client to test demonstrate the check feature
Frank Koormann <frank.koormann@intevation.de>
parents:
diff changeset
41 main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)