Example #1
0
    def getCityRealTimeInfo(self):
        # self.createCityRealTimeTable()
        action = 'GetCityRealTimeAQIModelByCitycode'
        for key in self.cityDict.keys():
            request = requests.get(
                    url= self.url + action,
                    params={'cityCode': key},
                    headers=self.headers)
            records = Record.parse(BytesIO(request.content))
            res = StringIO()
            print_records(records, fp=res)
            res.seek(0)

            temp = res.readlines()
            str = ''
            for row in temp:
                    row = row.replace('[\'', '')
                    row = row.replace('\']', '')
                    row = row.replace('\\n\'', '')
                    row = row.replace('a:', '')
                    row = row.replace('b:', '')
                    row = row.replace('—', '—')
                    str = str+row

            dict = xmltodict.parse(str)
            json_d = json.loads(json.dumps(dict))

            result = json_d['GetCityRealTimeAQIModelByCitycodeResponse']['GetCityRealTimeAQIModelByCitycodeResult']['RootResults']['CityAQIPublishLive']

            # print(result)
            self.dealWithCityRealTimeData(result)
Example #2
0
 def received(self, context):
     body = context.reply
     with io.BytesIO(body) as f, io.StringIO() as s:
         records = Record.parse(f)
         print_records(records, fp=s)
         context.reply = str(s.getvalue())
         s.close()
Example #3
0
    def craw(self, action, data):
        res = StringIO()
        res.write('<'+action+' xmlns="http://tempuri.org/">'+data+'</'+action+'>')
        res.seek(0)

        # usage from wcfbin-python
        res_r = XMLParser.parse(res)
        req = dump_records(res_r)

        # request
        request = requests.post(url=self.url+action,
                                data=req,
                                headers = self.headers)

        records = Record.parse(BytesIO(request.content))

        print_records(records, fp=res)
        res.seek(0)

        temp = res.readlines()

        pat = re.compile('<[^>]+>')
        enc = pat.sub('', temp[1][1:])[:-1]
        # print(type(enc))

        enc = base64.b64decode(enc)
        enc = zlib.decompress(enc)

        dict = xmltodict.parse(enc)
        print(json.dumps(dict))
        return dict
Example #4
0
def encode_decode(headers, data):

    if not data:
        return headers, data


    if 'X-WCF-Encode' in headers:
        data = dump_records(XMLParser.parse(data))
        del headers['X-WCF-Encode']
        headers['Content-Type'] = 'application/soap+msbin1'
        headers['Content-Length'] = str(len(data))
    else:
        #print headers['Content-type']
        if 'Content-Type' not in headers or headers['Content-Type'] != 'application/soap+msbin1':
            return headers, data
        #print headers
        fp = StringIO(data)
        data = Record.parse(fp)
        fp.close()
        fp = StringIO()
        print_records(data, fp=fp)
        data = fp.getvalue()
        fp.close()
        headers['X-WCF-Encode'] = '1'
        headers['Content-Type'] = 'text/soap+xml'
        headers['Content-Length'] = str(len(data))
    return headers, data
Example #5
0
def encode_decode(headers, data):

    if not data:
        return headers, data


    if 'X-WCF-Encode' in headers:
        data = dump_records(XMLParser.parse(data))
        del headers['X-WCF-Encode']
        headers['Content-Type'] = 'application/soap+msbin1'
        headers['Content-Length'] = str(len(data))
    else:
        #print headers['Content-type']
        if 'Content-Type' not in headers or headers['Content-Type'] != 'application/soap+msbin1':
            return headers, data
        #print headers
        fp = BytesIO(data)
        data = Record.parse(fp)
        fp.close()
        fp = StringIO()
        print_records(data, fp=fp)
        data = fp.getvalue()
        fp.close()
        headers['X-WCF-Encode'] = '1'
        headers['Content-Type'] = 'text/soap+xml'
        headers['Content-Length'] = str(len(data))
    return headers, data
Example #6
0
 def decodeWCF(self, binaryString):
     try:
         fp = StringIO(binaryString)
         data = Record.parse(fp)
         fp.close()
         fp = StringIO()
         print_records(data, fp=fp)
         data = fp.getvalue()
         fp.close()
         return data
     except ValueError:
         tb = traceback.format_exc()
         return tb
def parse(data, key):
    fp = BytesIO(data)
    build_dictionary(fp, key)
    records = Record.parse(fp)
    out = StringIO()
    print_records(records, fp=out)
    out.seek(0)

    if pygments is not None:
        print(pygments.highlight(out.read(),
                                 pygments.lexers.get_lexer_by_name('XML'),
                                 pygments.formatters.get_formatter_by_name('terminal')))
    else:
        print(out.read())
Example #8
0
def parse(data, key):
    fp = BytesIO(data)
    build_dictionary(fp, key)
    records = Record.parse(fp)
    out = StringIO()
    print_records(records, fp=out)
    out.seek(0)

    if pygments is not None:
        print(
            pygments.highlight(
                out.read(), pygments.lexers.get_lexer_by_name('XML'),
                pygments.formatters.get_formatter_by_name('terminal')))
    else:
        print(out.read())
Example #9
0
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if __name__ == '__main__':
    import sys
    from wcf.records import Record, print_records

    # Trying to nicely handle the case where sys.stdin isn't available
    if sys.version_info >= (
            3,
            0,
    ):
        try:
            fp = sys.stdin.buffer
        except:
            pass
    else:
        try:
            fp = sys.stdin
        except:
            pass
    if len(sys.argv) > 1:
        filename = sys.argv[1]
        fp = open(filename, 'rb')

    with fp:
        records = Record.parse(fp)
        print_records(records)
Example #10
0
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of the ERMW GmbH nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if __name__ == '__main__':
    import sys
    from wcf.records import Record,print_records
    fp = sys.stdin
    if len(sys.argv) > 1:
        filename = sys.argv[1]
        fp = open(filename, 'rb')
    
    with fp:
        records = Record.parse(fp)
        print_records(records)