Python SOAP API, HTTP POST and HTTP GET

发布时间:2017-09-11 13:08:12
Python SOAP API, HTTP POST and HTTP GET

Test API:?op=VerifyEmail

#0print_utils.py

def print_decorator(argument): def real_decorator(function): def wrapper(*args, **kwargs): print '-' * 10 + 'start of ' + argument + '-' * 10 function(*args, **kwargs) print '-' * 10 + 'end of ' + argument + '-' * 10 return wrapper return real_decorator @print_decorator('Request') def print_request(request): print ' '.join((request.method, request.path_url)) print_headers(request.headers) print if request.body: print_xml(request.body) def print_headers(headers): for k, v in headers.items(): print ':'.join((k, v)) @print_decorator('Response') def print_response(response): print response.status_code print print_headers(response.headers) print print_xml(response.text) def print_xml(xml_str): print xml_str

#1 Python SOAP API 1.1


POST /emailverify/Emailvernotestemail.asmx HTTP/1.1 Host: ws.cdyne.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://ws.cdyne.com/VerifyEmail" string string HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length string int string boolean
#!/usr/bin/env python import requests from print_utils import * # ?op=VerifyEmail # copy from SOAP 1.1 body request_body = ''' {email} {key} '''.format(email='mutantninja@gmail.com', key='123') request_headers = {'Host': 'ws.cdyne.com', 'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': ''"http://ws.cdyne.com/VerifyEmail"''} response = requests.post('', data=request_body, headers=request_headers) request = response.request print_request(request) print_response(response)
Output ----------start of Request---------- POST /emailverify/Emailvernotestemail.asmx Content-Length:398 Accept-Encoding:gzip, deflate SOAPAction: Connection:keep-alive Accept:*/* User-Agent:python-requests/2.9.1 Host:ws.cdyne.com Content-Type:text/xml; charset=utf-8 mutantninja@gmail.com 123 ----------end of Request---------- ----------start of Response---------- 200 Content-Length:531 X-AspNet-Version:4.0.30319 Expires:-1 Server:Microsoft-IIS/8.5 Pragma:no-cache Cache-Control:no-cache Date:Fri, 28 Oct 2016 01:08:21 GMT X-Powered-By:ASP.NET Content-Type:text/xml; charset=utf-8 None Mail Server will accept email3gmail-smtp-in.l.google.comtrue ----------end of Response----------

#2 Python SOAP API 1.2

POST /emailverify/Emailvernotestemail.asmx HTTP/1.1 Host: ws.cdyne.com Content-Type: application/soap+xml; charset=utf-8 Content-Length: length string string HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length string int string boolean
#!/usr/bin/env python import requests from print_utils import * # ?op=VerifyEmail # copy from SOAP 1.2 body request_body = ''' {email} {key} '''.format(email='mutantninja@gmail.com', key='123') request_headers = {'Host': 'ws.cdyne.com', 'Content-Type': 'application/soap+xml; charset=utf-8'} response = requests.post('', data=request_body, headers=request_headers) request = response.request print_request(request) print_response(response)
----------start of Request---------- POST /emailverify/Emailvernotestemail.asmx Content-Length:406 Accept-Encoding:gzip, deflate Connection:keep-alive Accept:*/* User-Agent:python-requests/2.9.1 Host:ws.cdyne.com Content-Type:application/soap+xml; charset=utf-8 mutantninja@gmail.com 123 ----------end of Request---------- ----------start of Response---------- 200 Content-Length:529 X-AspNet-Version:4.0.30319 Expires:-1 Server:Microsoft-IIS/8.5 Pragma:no-cache Cache-Control:no-cache Date:Fri, 28 Oct 2016 01:11:25 GMT X-Powered-By:ASP.NET Content-Type:application/soap+xml; charset=utf-8 None Mail Server will accept email3gmail-smtp-in.l.google.comtrue ----------end of Response----------

#3 HTTP POST

POST /emailverify/Emailvernotestemail.asmx/VerifyEmail HTTP/1.1 Host: ws.cdyne.com Content-Type: application/x-www-form-urlencoded Content-Length: length email=string&LicenseKey=string HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length string int string boolean
#!/usr/bin/env python import requests from print_utils import * # ?op=VerifyEmail # copy from SOAP 1.1 body request_body = 'email={email}&LicenseKey={key}'.format(email='mutantninja@gmail.com', key='123') request_headers = {'Host': 'ws.cdyne.com', 'Content-Type': 'application/x-www-form-urlencoded'} response = requests.post('', data=request_body, headers=request_headers) request = response.request print_request(request) print_response(response)
----------start of Request---------- POST /emailverify/Emailvernotestemail.asmx/VerifyEmail Content-Length:42 Accept-Encoding:gzip, deflate Connection:keep-alive Accept:*/* User-Agent:python-requests/2.9.1 Host:ws.cdyne.com Content-Type:application/x-www-form-urlencoded email=mutantninja@gmail.com&LicenseKey=123 ----------end of Request---------- ----------start of Response---------- 200 Content-Length:395 X-AspNet-Version:4.0.30319 Expires:-1 Server:Microsoft-IIS/8.5 Pragma:no-cache Cache-Control:no-cache Date:Fri, 28 Oct 2016 01:17:03 GMT X-Powered-By:ASP.NET Content-Type:text/xml; charset=utf-8 None Mail Server will accept email 3 gmail-smtp-in.l.google.com true ----------end of Response----------

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉建网站公司 http://www.45qun.com