Python SOAP API, HTTP POST and HTTP GET

request module: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

Test API: http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?op=VerifyEmail

#0 print_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"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <VerifyEmail xmlns="http://ws.cdyne.com/">
      <email>string</email>
      <LicenseKey>string</LicenseKey>
    </VerifyEmail>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <VerifyEmailResponse xmlns="http://ws.cdyne.com/">
      <VerifyEmailResult>
        <ResponseText>string</ResponseText>
        <ResponseCode>int</ResponseCode>
        <LastMailServer>string</LastMailServer>
        <GoodEmail>boolean</GoodEmail>
      </VerifyEmailResult>
    </VerifyEmailResponse>
  </soap:Body>
</soap:Envelope>

#!/usr/bin/env python
import requests

from print_utils import *

# http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?op=VerifyEmail
# copy from SOAP 1.1 body
request_body = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <VerifyEmail xmlns="http://ws.cdyne.com/">
      <email>{email}</email>
      <LicenseKey>{key}</LicenseKey>
    </VerifyEmail>
  </soap:Body>
</soap:Envelope>'''.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('http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx',
                         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:http://ws.cdyne.com/VerifyEmail
Connection:keep-alive
Accept:*/*
User-Agent:python-requests/2.9.1
Host:ws.cdyne.com
Content-Type:text/xml; charset=utf-8

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <VerifyEmail xmlns="http://ws.cdyne.com/">
      <email>mutantninja@gmail.com</email>
      <LicenseKey>123</LicenseKey>
    </VerifyEmail>
  </soap:Body>
</soap:Envelope>
----------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

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><VerifyEmailResponse xmlns="http://ws.cdyne.com/"><VerifyEmailResult><ResponseText>Mail Server will accept email</ResponseText><ResponseCode>3</ResponseCode><LastMailServer>gmail-smtp-in.l.google.com</LastMailServer><GoodEmail>true</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope>
----------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

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <VerifyEmail xmlns="http://ws.cdyne.com/">
      <email>string</email>
      <LicenseKey>string</LicenseKey>
    </VerifyEmail>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <VerifyEmailResponse xmlns="http://ws.cdyne.com/">
      <VerifyEmailResult>
        <ResponseText>string</ResponseText>
        <ResponseCode>int</ResponseCode>
        <LastMailServer>string</LastMailServer>
        <GoodEmail>boolean</GoodEmail>
      </VerifyEmailResult>
    </VerifyEmailResponse>
  </soap12:Body>
</soap12:Envelope>

#!/usr/bin/env python
import requests

from print_utils import *

# http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?op=VerifyEmail
# copy from SOAP 1.2 body
request_body = '''<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <VerifyEmail xmlns="http://ws.cdyne.com/">
      <email>{email}</email>
      <LicenseKey>{key}</LicenseKey>
    </VerifyEmail>
  </soap12:Body>
</soap12:Envelope>'''.format(email='mutantninja@gmail.com', key='123')

request_headers = {'Host': 'ws.cdyne.com',
                   'Content-Type': 'application/soap+xml; charset=utf-8'}

response = requests.post('http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx',
                         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

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <VerifyEmail xmlns="http://ws.cdyne.com/">
      <email>mutantninja@gmail.com</email>
      <LicenseKey>123</LicenseKey>
    </VerifyEmail>
  </soap12:Body>
</soap12:Envelope>
----------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

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><VerifyEmailResponse xmlns="http://ws.cdyne.com/"><VerifyEmailResult><ResponseText>Mail Server will accept email</ResponseText><ResponseCode>3</ResponseCode><LastMailServer>gmail-smtp-in.l.google.com</LastMailServer><GoodEmail>true</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope>
----------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

<?xml version="1.0" encoding="utf-8"?>
<ReturnIndicator xmlns="http://ws.cdyne.com/">
  <ResponseText>string</ResponseText>
  <ResponseCode>int</ResponseCode>
  <LastMailServer>string</LastMailServer>
  <GoodEmail>boolean</GoodEmail>
</ReturnIndicator>

#!/usr/bin/env python
import requests

from print_utils import *

# http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?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('http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx/VerifyEmail',
                         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

<?xml version="1.0" encoding="utf-8"?>
<ReturnIndicator xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ws.cdyne.com/">
  <ResponseText>Mail Server will accept email</ResponseText>
  <ResponseCode>3</ResponseCode>
  <LastMailServer>gmail-smtp-in.l.google.com</LastMailServer>
  <GoodEmail>true</GoodEmail>
</ReturnIndicator>
----------end of Response----------

#4 HTTP GET

GET /emailverify/Emailvernotestemail.asmx/VerifyEmail?email=string&LicenseKey=string HTTP/1.1
Host: ws.cdyne.com
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<ReturnIndicator xmlns="http://ws.cdyne.com/">
  <ResponseText>string</ResponseText>
  <ResponseCode>int</ResponseCode>
  <LastMailServer>string</LastMailServer>
  <GoodEmail>boolean</GoodEmail>
</ReturnIndicator>

#!/usr/bin/env python
import requests
from print_utils import *

payload = {'email': 'mutantninja@gmail.com',
           'LicenseKey': '123'}
response = requests.get("http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx/VerifyEmail", params=payload)

request = response.request

print_request(request)

print_response(response)

----------start of Request----------
GET /emailverify/Emailvernotestemail.asmx/VerifyEmail?LicenseKey=123&email=mutantninja%40gmail.com
Connection:keep-alive
Accept-Encoding:gzip, deflate
Accept:*/*
User-Agent:python-requests/2.9.1

----------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:19:28 GMT
X-Powered-By:ASP.NET
Content-Type:text/xml; charset=utf-8
None

<?xml version="1.0" encoding="utf-8"?>
<ReturnIndicator xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ws.cdyne.com/">
  <ResponseText>Mail Server will accept email</ResponseText>
  <ResponseCode>3</ResponseCode>
  <LastMailServer>gmail-smtp-in.l.google.com</LastMailServer>
  <GoodEmail>true</GoodEmail>
</ReturnIndicator>
----------end of Response----------





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值