最近学习python 又在做接口 于是找了N多资料 终于实现了 下面是代码
importcookielib
importurllib2
importurllib
#用chilkat拆分pfx得到key,cert的pem格式key_file='chilkat_pkey.pem'
cert_file='chilkat_cert.pem'classHTTPSClientAuthConnection(httplib.HTTPSConnection):
def__init__(self,host,timeout=None):
httplib.HTTPSConnection.__init__(self,host,key_file=key_file,cert_file=cert_file)
self.timeout=timeout#OnlyvalidinPython2.6classHTTPSClientAuthHandler(urllib2.HTTPSHandler):
defhttps_open(self,req):
returnself.do_open(HTTPSClientAuthConnection,req)
classICBCAPI(object):
def__init__(self):
self._cookiejar=cookielib.CookieJar()
defget(self,url,**data):
parameters=urllib.urlencode(data)
opener=urllib2.build_opener(urllib2.HTTPHandler(),HTTPSClientAuthHandler(),urllib2.HTTPCookieProcessor(self._cookiejar))
req=urllib2.Request(url,parameters)
server_response=opener.open(req).read()
returnurllib2.unquote(server_response)
deftest():
api=ICBCAPI()
apiUrl="https://corporbank3.dccnet.com.cn/servlet/ICBCINBSEBusinessServlet"merReqData='''
<?xmlversion="1.0"encoding="GBK\"standalone="no"?>
<ICBCAPI>
<in>
<orderNum>填你们自己的</orderNum>
<tranDate>填你们自己的</tranDate>
<ShopCode>填你们自己的</ShopCode>
<ShopAccount>填你们自己的</ShopAccount>
</in>
</ICBCAPI>'''printapi.get(apiUrl,APIName='EAPI',APIVersion='001.001.002.001',MerReqData=merReqData)
if__name__=='__main__':
test()