#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import urllib
import uuid
import pyamf
import json
import copy
from urllib import request
from pyamf import remoting
from pyamf.flex import messaging
class BodyMiddle(object):
def __init__(self, uno=None, hno=None, paramStr=None):
self.UNO = uno
self.HNO = hno
self.paramStr = paramStr
class BodyObject(object):
def __init__(self):
self.UNO = None
self.HNO = None
self.paramStr = None
# 注册自定义的 Body 参数类型,这样数据类型 flex.messaging.messages.RemotingMessage 就会在后面被一并发给服务端(否则服务端就可能返回参数不是预期的异常 Client.Message.Deserialize.InvalidType)
pyamf.register_class(BodyObject, alias='flex.messaging.messages.RemotingMessage')
# 构造 flex.messaging.messages.RemotingMessage 消息
msg = messaging.RemotingMessage(
operation='ExecRPC',
# source=None,
timeToLive=0,
messageId=str(uuid.uuid1()).upper(),
destination='SMService',
# body=[],
clientId=None,
timestamp=0,
)
# 构造请求数据
def getRequestData():
uno = '其他'
hno = '地下室'
paramStr = 'VrMDnR1zrkarltpNyFEfEIaWSLH9AW5wPT0gztBK7tbw7HwPIkNIddRVK31aoKsC+ayP7kdXzSUEHFoW2x3o58UJjEXj5tzUXIEhTTtLW5NTL3Qtn0yXE/1exxu1A0z95pszZgd+wCTreQF+XEbIcyzwIdxHFEH1WG2vNEjDEsU='
msg.body = ['HouseTableServiceImpl', 'getHouses', BodyMiddle(uno, hno, paramStr)]
msg.headers['DSEndpoint'] = 'my-amf'
msg.headers['DSId'] = str(uuid.uuid1()).upper()
# 按 AMF 协议编码数据
req = remoting.Request('null', body=(msg,))
env = remoting.Envelope(amfVersion=pyamf.AMF3)
env.bodies = [('/2', req)]
data = bytes(remoting.encode(env).read())
return data
# 返回一个请求的数据格式
def getResponse(data):
url = 'https://zw.cdzj.chengdu.gov.cn/DE-SMServerFx/messagebroker/amf'
# req = Request(url, data, headers={'Content-Type': 'application/x-amf'})
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'Content-Type': 'application/x-amf',
'Accept': '*/*',
'Origin': 'https://zw.cdzj.chengdu.gov.cn',
'X-Requested-With': 'ShockwaveFlash/34.0.0.184',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'no-cors',
'Sec-Fetch-Dest': 'embed',
'Referer': 'https://zw.cdzj.chengdu.gov.cn/DE-SMServerFx/FundateClient.swf?t=1¶m=RrJw0Hr%2BoUejulOypoolBnDEFQ47eLMP1vIWbT2WbEiz9Z2daWruY1VF0UlympS4snJ3Fz5ICu6DOvoYJH1sNDeVe757q6jWvUpHMIbzBoH4PaZV7L9w9i4hg9LCcK6iAho6aqUkR4O%2Footqr14jLcdhP1X7CHbsUAZT0cpjr%2FY%3D',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
# req = urllib.request.Request(url, data, headers={'Content-Type': 'application/x-amf'})
req = urllib.request.Request(url, data, headers=headers)
# 解析返回数据
# opener = urllib2.build_opener()
opener = urllib.request.build_opener()
return opener.open(req).read()
def getContent(response):
amf_parse_info = remoting.decode(response)
print("amf 响应数据:", amf_parse_info)
curr_data = amf_parse_info.bodies[0][1].body.body['HOUSEITEMLIST'][0]
print('响应中的第一条数据', curr_data)
# print('ddddd', '\n'.join(['%s:%s' % item for item in curr_data.__dict__.items()]))
# # 数据总条数
# total_num = amf_parse_info.bodies[0][1].body.body[3]
# info = amf_parse_info.bodies[0][1].body.body[0]
# return total_num, info
def store2json(info):
res = []
for record in info:
record['reportDate'] = record['reportDate'].strftime('%Y-%m-%d %H:%M:%S')
record['auditDate'] = record['auditDate'].strftime('%Y-%m-%d %H:%M:%S')
res.append(record)
fp = open('info.json', 'w')
json.dump(res, fp, indent=4)
fp.close()
# 获取数据量
reqData = getRequestData()
rep = getResponse(reqData)
getContent(rep)