欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python SOAP API, HTTP POST and HTTP GET

程序员文章站 2023-08-26 10:06:36
request module: https://docs.python-requests.org/zh_CN/latest/user/quickstart.html Test AP...

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

Test API:https://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?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: "https://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 *

# https://ws.cdyne.com/emailverify/Emailvernotestemail.asmx?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': ''"https://ws.cdyne.com/VerifyEmail"''}

response = requests.post('https://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:https://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



  
    
      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 *

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



  
    
      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 *

# https://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('https://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



  Mail Server will accept email
  3
  gmail-smtp-in.l.google.com
  true

----------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



  string
  int
  string
  boolean


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

payload = {'email': 'mutantninja@gmail.com',
           'LicenseKey': '123'}
response = requests.get("https://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



  Mail Server will accept email
  3
  gmail-smtp-in.l.google.com
  true

----------end of Response----------