python中的异步请求库_如何开始使用Python中的请求库

python中的异步请求库 介绍 (Introduction)In many web apps, it’s normal to connect to various third-party services by using APIs. When you use these APIs you can get access to data like weather information, spo...
摘要由CSDN通过智能技术生成

python中的异步请求库

介绍 (Introduction)

In many web apps, it’s normal to connect to various third-party services by using APIs. When you use these APIs you can get access to data like weather information, sports scores, movie listings, tweets, search engine results, and pictures. You can also use APIs to add functionality to your app. Examples of these are payments, scheduling, emails, translations, maps, and file transfers. If you were to create any of those on your own it would take a ton of time, but with APIs, it can take only minutes to connect to one and access its features and data.

在许多Web应用程序中,通常使用API​​连接到各种第三方服务。 使用这些API时,您可以访问天气信息,体育比分,电影列表,推文,搜索引擎结果和图片等数据。 您还可以使用API​​向您的应用添加功能。 这些示例包括付款,计划,电子邮件,翻译,地图和文件传输。 如果您要自己创建任何一个,那将花费大量时间,但是使用API​​,只需几分钟即可连接到一个数据库并访问其功能和数据。

In this article, we’ll learn about the Python Requests library, which allows you to send HTTP requests in Python.

在本文中,我们将学习Python请求库,该库允许您使用Python发送HTTP请求。

And since using an API is sending HTTP requests and receiving responses, Requests allows you to use APIs in Python. We’ll demonstrate the use of a language translation API here so you can see an example of how it works.

并且由于使用API​​发送HTTP请求并接收响应,因此Requests允许您在Py​​thon中使用API​​。 我们将在此处演示语言翻译API的用法,以便您查看其工作方式示例。

HTTP请求快速概述 (Quick Overview of HTTP Requests)

HTTP requests are how the web works. Every time you navigate to a web page, your browser makes multiple requests to the web page’s server. The server then responds with all the data necessary to render the page, and your browser then actually renders the page so you can see it.

HTTP请求是网络的工作方式。 每次导航到网页时,浏览器都会向该网页的服务器发出多个请求。 然后,服务器将提供呈现页面所需的所有数据进行响应,然后您的浏览器将实际呈现该页面,以便您可以看到它。

The generic process is this: a client (like a browser or Python script using Requests) will send some data to a URL, and then the server located at the URL will read the data, decide what to do with it, and return a response to the client. Finally, the client can decide what to do with the data in the response.

通用过程是这样的:客户端(例如使用Requests的浏览器或Python脚本)将向URL发送一些数据,然后位于URL的服务器将读取数据,决定如何处理并返回响应给客户。 最后,客户可以决定如何处理响应中的数据。

Part of the data the client sends in a request is the request method. Some common request methods are GET, POST, and PUT. GET requests are normally for reading data only without making a change to something, while POST and PUT requests generally are for modifying data on the server. So for example, the Stripe API allows you to use POST requests to create a new charge so a user can purchase something from your app.

客户端在请求中发送的部分数据是请求方法。 一些常见的请求方法是GET,POST和PUT。 GET请求通常仅用于读取数据而不进行任何更改,而POST和PUT请求通常用于修改服务器上的数据。 因此,例如,Stripe API允许您使用POST请求创建新的费用,以便用户可以从您的应用程序中购买商品。

Note: This article will cover GET requests, because we won’t be modifying any data on a server.

注意:本文将介绍GET请求,因为我们不会在服务器上修改任何数据。

When sending a request from a Python script or inside a web app, you, the developer, gets to decide what gets sent in each request and what to do with the response. So let’s explore that by first sending a request to Scotch.io and then by using a language translation API.

从Python脚本或Web应用程序发送请求时,开发人员可以决定在每个请求中发送了什么以及如何处理响应。 因此,让我们通过首先向Scotch.io发送请求,然后使用语言翻译API来进行探索。

安装Python请求 (Install Python Requests)

Before we can do anything, we need to install the library. So let’s go ahead and install requests using pip. It’s a good idea to create a virtual environment first if you don’t already have one.

在做任何事情之前,我们需要安装该库。 因此,让我们继续使用pip安装请求。 如果您还没有虚拟环境,那么最好先创建一个虚拟环境。

  • pip install requests

    点安装请求

我们的第一个要求 (Our First Request)

To start, let’s use Requests for requesting the Scotch.io site. Create a file called script.py and add the following code to it. In this article, we won’t have much code to work with, so when something changes you can just update the existing code instead of adding new lines.

首先,让我们使用请求来请求Scotch.io网站。 创建一个名为script.py的文件,并向其中添加以下代码。 在本文中,我们没有太多的代码可以使用,因此,当发生某些更改时,您只需更新现有代码即可,而无需添加新行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值