Skip to content

Latest commit

 

History

History
193 lines (126 loc) · 9.86 KB

File metadata and controls

193 lines (126 loc) · 9.86 KB

十五、SOAP 和 RESTAPI 通信

在本章中,我们将介绍 SOAP 和 RESTAPI 的基础知识。我们还将研究用于 SOAP 和 RESTAPI 的 Python 库。我们将学习有关 SOAP 的 Zeep 库和 RESTAPI 的请求。您将学习如何使用 JSON 数据。我们将看到处理 JSON 数据的简单示例,例如将 JSON 字符串转换为 Python 对象以及将 Python 对象转换为 JSON 字符串。

在本章中,您将学习以下内容:

  • 什么是肥皂?
  • 为 SOAP 使用库
  • 什么是 RESTful API?
  • 为 RESTful API 使用标准库
  • 使用 JSON 数据

什么是肥皂?

SOAP是简单对象访问协议。SOAP 是允许进程使用不同操作系统的标准通信协议系统。它们通过 HTTP 和 XML 进行通信。它是一种 web 服务技术。SOAPAPI 主要用于创建、更新、删除和恢复数据等任务。SOAPAPI 使用 Web 服务描述语言来描述 Web 服务提供的功能。SOAP 描述了所有函数和数据类型。它构建了一个基于 XML 的协议。

为 SOAP 使用库

在本节中,我们将学习用于 SOAP 的 Python 库。这里列出了用于 SOAP 的各种库:

  • SOAPpy
  • Zeep
  • Ladon
  • suds-jurko
  • pysimplesoap

这些是 Python 的 SOAP API 库。在本节中,我们将仅了解 Zeep 库。

要使用 Zeep 的功能,首先需要安装它。在终端中运行以下命令以安装 Zeep:

 $ pip3 install Zeep

Zeep模块用于 WSDL 文档。它为服务和文档生成代码,并向 SOAP 服务器提供编程接口。lxml库用于解析 XML 文档。

现在,我们来看一个例子。创建一个soap_example.py脚本,并在其中编写以下代码:

import zeep w = 'http://www.soapclient.com/xml/soapresponder.wsdl' c = zeep.Client(wsdl=w) print(c.service.Method1('Hello', 'World'))

运行脚本,您将获得以下输出:

student@ubuntu:~$ python3 soap_example.py Output : Your input parameters are Hello and World

在前面的示例中,我们首先导入了zeep模块。我们首先提到了网站名称。然后我们创建了zeep客户机对象。我们之前使用的 WSDL 定义了一个简单的Method1函数,该函数由zeep通过client.service.Method1提供。它接受两个参数并返回一个字符串。

什么是 RESTful API?

REST代表表征状态转移。RESTful API 有一种用于 web 服务开发的通信方法。它是一种 web 服务,作为 internet 上不同系统之间的通信渠道。它是一个应用接口,用于使用HTTP请求处理GET、PUT、POST和DELETE数据。

REST 的优点是它使用较少的带宽,适合 internet 使用。RESTAPI 使用统一接口。所有资源都由GET、POST、PUT和DELETE操作处理。RESTAPI 使用GET检索资源,使用PUT更新资源或更改资源状态,使用POST创建资源,使用DELETE删除资源。使用 RESTAPI 的系统提供了快速的性能和可靠性。

RESTAPI 独立地处理每个请求。从客户端到服务器的请求必须包含理解该请求所需的所有信息。

为 RESTful API 使用标准库

在本节中,我们将学习如何使用 RESTful API。为此,我们将使用 Python 的requests和 JSON 模块。现在我们来看一个例子。首先,我们将使用requests模块从 API 获取信息。我们将看到GET和POST请求。

首先,您必须按如下方式安装requests库:

 $ pip3 install requests

现在,我们来看一个例子。创建一个rest_get_example.py脚本,并在其中写入以下内容:

import requests req_obj = requests.get('https://www.imdb.com/news/top?ref_=nv_tp_nw') print(req_obj)

运行脚本,您将获得如下输出:

student@ubuntu:~/work$ python3 rest_get_example.py Output: <Response [200]>

在前面的示例中,我们导入了requests模块以获取请求。接下来,我们创建了一个请求对象req_obj,并指定了一个我们希望从中获取请求的链接。接下来,我们打印了它。我们得到的输出是状态码200,表示成功。

现在,我们将看到POST请求示例。POST请求用于向服务器发送数据。创建一个rest_post_example.py脚本,并在其中写入以下内容:

import requests import json url_name = 'http://httpbin.org/post' data = {"Name" : "John"} data_json = json.dumps(data) headers = {'Content-type': 'application/json'} response = requests.post(url_name, data=data_json, headers=headers) print(response)

运行脚本,您将获得以下输出:

student@ubuntu:~/work$ python3 rest_post_example.py Output: <Response [200]>

在前面的示例中,我们了解了POST请求。首先,我们导入了必要的模块请求和 JSON。接下来,我们提到了 URL。此外,我们还输入了要以字典格式发布的数据。接下来,我们提到了标题。然后我们发布了一个POST请求。我们得到的输出是状态码200,这是一个成功码。

使用 JSON 数据

在本节中,我们将学习 JSON 数据。JSON代表JavaScript 对象表示法。JSON 是一种数据交换格式。它将 Python 对象编码为 JSON 字符串,并将 JSON 字符串解码为 Python 对象。Python 有一个 JSON 模块,用于格式化 JSON 输出。它具有序列化和反序列化 JSON 的功能。

  • json.dump(obj, fileObj):此函数将对象序列化为 JSON 格式的流。
  • json.dumps(obj):此函数将对象序列化为 JSON 格式的字符串。
  • json.load(JSONfile):此函数将 JSON 文件反序列化为 Python 对象。
  • json.loads(JSONfile):此函数将字符串类型的 JSON 文件反序列化为 Python 对象。

这里还列出了两个用于编码和解码的类:

  • JSONEncoder:用于将 Python 对象转换为 JSON 格式。
  • JSONDecoder:用于将 JSON 格式的文件转换为 Python 对象。

现在,我们将看到一些使用 JSON 模块的示例。首先,我们将看到从 JSON 到 Python 的转换。为此,创建一个脚本json_to_python.py并在其中编写以下代码:

import json j_obj =  '{ "Name":"Harry", "Age":26, "Department":"HR"}' p_obj = json.loads(j_obj) print(p_obj["Name"]) print(p_obj["Department"])

运行脚本,您将获得如下输出:

student@ubuntu:~/work$ python3 json_to_python.py Output: Harry HR

在前面的示例中,我们编写了一段代码,将 JSON 字符串转换为 Python 对象。json.loads()函数用于将 JSON 字符串转换为 Python 对象。

现在,我们将看到如何将 Python 转换为 JSON。为此,创建一个python_to_json.py脚本并在其中编写以下代码:

import json emp_dict1 =  '{ "Name":"Harry", "Age":26, "Department":"HR"}' json_obj = json.dumps(emp_dict1) print(json_obj)

运行脚本,您将获得以下输出:

student@ubuntu:~/work$ python3 python_to_json.py Output: "{ \"Name\":\"Harry\", \"Age\":26, \"Department\":\"HR\"}"

在前面的示例中,我们将 Python 对象转换为 JSON 字符串。json.dumps()功能用于此转换。

现在,我们将看到如何将各种类型的 Python 对象转换为 JSON 字符串。为此,创建一个python_object_to_json.py脚本,并在其中写入以下内容:

import json python_dict =  {"Name": "Harry", "Age": 26} python_list =  ["Mumbai", "Pune"] python_tuple =  ("Basketball", "Cricket") python_str =  ("hello_world") python_int =  (150) python_float =  (59.66) python_T =  (True) python_F =  (False) python_N =  (None) json_obj = json.dumps(python_dict) json_arr1 = json.dumps(python_list) json_arr2 = json.dumps(python_tuple) json_str = json.dumps(python_str) json_num1 = json.dumps(python_int) json_num2 = json.dumps(python_float) json_t = json.dumps(python_T) json_f = json.dumps(python_F) json_n = json.dumps(python_N) print("json object : ", json_obj) print("json array1 : ", json_arr1) print("json array2 : ", json_arr2) print("json string : ", json_str) print("json number1 : ", json_num1) print("json number2 : ", json_num2) print("json true", json_t) print("json false", json_f) print("json null", json_n)

运行脚本,您将获得以下输出:

student@ubuntu:~/work$ python3 python_object_to_json.py Output: json object :  {"Name": "Harry", "Age": 26} json array1 :  ["Mumbai", "Pune"] json array2 :  ["Basketball", "Cricket"] json string :  "hello_world" json number1 :  150 json number2 :  59.66 json true true json false false json null null

在前面的示例中,我们使用json.dumps()函数将各种类型的 Python 对象转换为 JSON 字符串。转换后,Python 列表和元组被转换为数组。整数和浮点数在 JSON 中被视为数字。以下是从 Python 到 JSON 的转换图表:

| 蟒蛇 | JSON | | dict | 对象 | | list | 大堆 | | tuple | 大堆 | | str | 一串 | | int | 数字 | | float | 数字 | | True | 符合事实的 | | False | 错误的 | | None | 无效的 |

总结

在本章中,您学习了 SOAP API 和 RESTful API。您了解了 SOAPAPI 的zeepPython 库和 RESTAPI 的请求库。您还学习了如何处理 JSON 数据,例如,将 JSON 转换为 Python,反之亦然。

在下一章中,您将了解 web 报废和用于执行此任务的 Python 库。

问题

  1. SOAP 和 RESTAPI 之间的区别是什么?

  2. json.loads和json.load之间有什么区别?

  3. JSON 支持所有平台吗?

  4. 以下代码段的输出是什么?

boolean_value = False
print(json.dumps(boolean_value))
  1. 以下代码段的输出是什么?
>> weird_json = '{"x": 1, "x": 2, "x": 3}'
>>> json.loads(weird_json)

进一步阅读