目录
实际项目需要调用已经存在的webservice接口,插入邮件信息通过后台接口发送邮件。
WebService概念:WebService遵循SOAP协议通过XML封装数据,然后由Http协议来传输数据。在常规的http接口中python直接用Requests库就能解决,但是webservice没办法用Requests来请求,我们需要通过第三方库来简单实现调用。
系统环境
操作系统: Windows10 Pro
Python版本:python-3.7.9 64bit
引用库:suds-1.1.2
这里我们使用suds包来调用webservice,需要先通过命令 pip install suds 安装库
具体使用方法,我在代码里面有非常清楚的注释,最终代码展示:
- # -*- coding:UTF-8 -*-
- # Author by : Andy zhang
- # updated at : 2022/8/23
- # Version : 1.0.0
- # Install cmd:pip install suds
- #
-
- from suds.client import Client
-
- # 调用webservice接口发送邮件
- def sendemail(mfrom, mto, mcc, mbcc, subject, content, attach):
- try:
- # 要访问的Webservice地址
- prod_url = "http://192.168.31.200/axis2/services/Gms?wsdl"
-
- # 创建Webservice Client对象
- client = Client(prod_url)
-
- # print(client.option) #通过这个可以查看可以设置哪些options
-
- client.set_options(timeout=100) #设置请求超时时间
-
- # print(client) # 此方法可以打印出webservice提供哪些类型及方法
-
- # 调用对应方法 # insertMailboxII 是你的方法,(mfrom, mto, mcc, mbcc, subject, "PyRPA", content, attach) 为参数
- result = client.service.insertMailboxII(mfrom, mto, mcc, mbcc, subject, "PyRPA", content, attach)
-
- print(result)
- return True
- except Exception as e:
- print("failed to send email:" + e)
- return False
-
-
- if __name__ == '__main__':
- mfrom = "sender@163.com"
- mto = "a@163.com;b@163.com"
- mcc = ""
- mbcc = ""
- subject = "test"
- application = "PyRPA"
- content = "This test email was sent by RPA mail modules "
- attach = ""
- result = sendemail(mfrom, mto, mcc, mbcc, subject, content, attach)
- print(result)
Python 3.7.9
Windows64 位安装包下载地址:
https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe
Mac 电脑安装包下载地址:
https://www.python.org/ftp/python/3.7.9/python-3.7.9-macosx10.9.pkg