Project

General

Profile

« Previous | Next » 

Revision 1678

digir_client: Set header sendTime, source dynamically. In debug mode, print the request XML.

View differences:

digir_client
1 1
#!/usr/bin/env python
2 2
# A DiGIR client
3 3

  
4
import os
4 5
import os.path
5 6
import sys
6 7
import urllib
......
8 9

  
9 10
sys.path.append(os.path.dirname(__file__)+"/../lib")
10 11

  
12
import dates
11 13
import opts
12 14
import streams
13 15
import util
......
30 32
      http://digir.sourceforge.net/schema/conceptual/darwin/2003/1.0/darwin2.xsd">
31 33
    <header>
32 34
        <version>1.0</version>
33
        <sendTime>2012-03-29 01:01:33.00Z</sendTime>
34
        <source>128.111.220.180</source>
35
        <sendTime>[time]</sendTime>
36
        <source>[source]</source>
35 37
        <destination resource="[resource]">[url]</destination>
36 38
        <type>search</type>
37 39
    </header>
38 40
    <search>
39 41
        <filter>
40 42
            <equals>
41
                <darwin:Kingdom>PLANTAE</darwin:Kingdom>
43
                <darwin:Kingdom>plantae</darwin:Kingdom>
42 44
            </equals>
43 45
        </filter>
44 46
        <records limit="[count]" start="[start]">
......
60 62
    resource = opts.get_env_var('resource', None, env_names)
61 63
    start = util.cast(int, opts.get_env_var('start', 0, env_names))
62 64
    count = util.cast(int, opts.get_env_var('n', 1, env_names))
65
    debug = opts.env_flag('debug', False, env_names)
63 66
    if url == None or resource == None: usage_err()
64 67
    
65
    this_request_xml_template = (request_xml_template.replace('[url]', url)
66
        .replace('[resource]', resource).replace('[count]', str(count)))
68
    def clear_line(): sys.stderr.write('\n')
69
    log_indent = 0
70
    def log(msg, line_ending='\n'):
71
        sys.stderr.write(('    '*log_indent)+msg+line_ending)
67 72
    
68
    request_xml = this_request_xml_template.replace('[start]', str(start))
73
    self_dir = os.path.dirname(__file__)
74
    source = os.popen(self_dir+"/local_ip").read().strip()
75
    this_request_xml_template = (request_xml_template
76
        .replace('[source]', source)
77
        .replace('[url]', url)
78
        .replace('[resource]', resource)
79
        .replace('[count]', str(count))
80
        )
81
    
82
    time = dates.strftime('%Y-%m-%d %H:%M:%S %Z', dates.now())
83
    request_xml = (this_request_xml_template
84
        .replace('[start]', str(start))
85
        .replace('[time]', time)
86
        )
87
    if debug: sys.stderr.write(request_xml)
69 88
    this_url = url+'?'+urllib.urlencode({'request': request_xml})
70 89
    stream = streams.StreamIter(streams.TimeoutInputStream(
71 90
        urllib2.urlopen(this_url), timeout))

Also available in: Unified diff