python ./和../_适用于Python的HTTP / 2.0

python ./和../

The HTTPbis have spoken, HTTP/2.0 is happening. Major websites are beginning to adopt it (hello there Twitter!), and the spec is beginning to get nailed down. If you’re unfamiliar with HTTP/2.0 and all the fun things it brings you, Ilya Grigorik has been doing an excellent job evangelising for it. Take a look at this talk if you want a deep dive into HTTP/2.0. If you want a shorter discussion, this article is also a great reference.

HTTPbis说过,HTTP / 2.0正在发生。 主要网站都开始采用它(您好,Twitter!),并且该规范也开始得到确定。 如果您不熟悉HTTP / 2.0及其带来的所有有趣的功能,那么Ilya Grigorik一直在为它进行出色的宣传。 如果您想深入了解HTTP / 2.0,请看一下这篇演讲 。 如果您希望进行简短的讨论,那么本文也是不错的参考。

HTTP/2.0 brings a number of advantages: it saves time setting up and tearing down TCP connections, it compresses HTTP headers, it provides flow control and connection multiplexing. For certain applications in certain use-cases, these can represent a huge advantage over HTTP/1.1.

HTTP / 2.0具有许多优点:它节省了建立和断开TCP连接的时间,压缩了HTTP标头,提供了流控制和连接多路复用。 对于某些用例中的某些应用程序,它们可以代表优于HTTP / 1.1的巨大优势。

It’s time for Python to get support for it. With that in mind, I’ve spent the last few weeks working on my brand new project. I give you: hyper.

是时候让Python获得支持了。 考虑到这一点,我在过去的几周里一直致力于我的全新项目。 我给你: hyper

(Hyper)

Hyper is an attempt to build a pure-Python HTTP/2.0 stack that perfectly matches the http.client API from Python’s standard library. This will allow hyper to be a drop-in replacement for http.client, providing all the HTTP/2.0 goodness to all Python programs without the need for potentially painful rewrites.

Hyper是一种尝试构建与Python标准库中的http.client API完全匹配的纯Python HTTP / 2.0堆栈的尝试。 这将使hyper可以替代http.client ,从而为所有Python程序提供所有HTTP / 2.0优点,而无需进行可能麻烦的重写。

How does it work? Like this:

它是如何工作的? 像这样:

from from hyper hyper import import HTTP20Connection

HTTP20Connection

conn conn = = HTTP20ConnectionHTTP20Connection (( 'twitter.com:443''twitter.com:443' )
)
connconn .. requestrequest (( 'GET''GET' , , '/''/' )
)
resp resp = = connconn .. getresponsegetresponse ()

()

printprint (( respresp .. readread ())())

That’s all it takes. The same API as http.client, but with all the fun of HTTP/2.0. For more examples, see the documentation. If you’re really excited, just go right ahead and download it using pip. Alternatively, take a look at the code on GitHub: it’s available under the MIT license, so everyone should be able to grab it and play with it.

这就是全部。 与http.client相同的API,但具有HTTP / 2.0的所有乐趣。 有关更多示例,请参见文档 。 如果您真的很兴奋,请继续并使用pip下载它。 另外,请查看GitHub上的代码:它在MIT许可下可用,因此每个人都应该可以抓住它并进行操作。

特征 (Features)

At the moment, hyper is in a very early alpha (version 0.0.1). I’ve proved that the mainline use-case works fine, but I’m sure there are plenty of bugs lying around, and there are several features I’d like to implement that I haven’t.

目前, hyper处于非常早期的alpha(0.0.1版)中。 我已经证明主线用例可以正常工作,但是我确定周围有很多错误,并且我想实现一些我还没有实现的功能。

Nevertheless, the core feature set is already in place:

尽管如此,核心功能集已经到位:

  • Can make HTTP/2.0 requests.
  • Obeys the HTTP/2.0 flow-control mechanisms.
  • Verifies TLS certificates.
  • Streaming uploads.
  • Streaming downloads.
  • 可以发出HTTP / 2.0请求。
  • 遵守HTTP / 2.0流控制机制。
  • 验证TLS证书。
  • 流式上传。
  • 流下载。

There are plenty of features planned, with the headline one being transparent HTTP/2.0 and HTTP/1.1 interworking. Currently, hyper will only work against servers that fully support HTTP/2.0. I’m planning to implement an abstraction layer that should prevent the user from needing to know whether the server they’re contacting supports HTTP/2.0: hyper will transparently fall back to HTTP/1.1.

计划中有很多功能,标题之一是透明的HTTP / 2.0和HTTP / 1.1互通。 目前, hyper仅适用于完全支持HTTP / 2.0的服务器。 我打算实现一个抽象层,该层应防止用户需要知道他们正在联系的服务器是否支持HTTP / 2.0: hyper将透明地回退到HTTP / 1.1。

The only feature I have no intention of implementing support for is one of HTTP/2.0’s headline features: Server Push. I’ll discuss this omission later.

我无意实现对此功能的支持的唯一功能是HTTP / 2.0的主要功能之一:服务器推送。 我将在稍后讨论此遗漏。

警告 (Warning)

Please note that hyper is in a very early alpha. I do not recommend you use it in production: things will go wrong. However, I’d like as many of you as possible to take it out for a spin and report any bugs you find on the GitHub page. The more bugs you find the better hyper will get.

请注意, hyper处于非常早期的Alpha状态。 我不建议您在生产中使用它:事情会出错。 但是,我希望尽可能多的人试用一下并报告在GitHub页面上发现的所有错误。 您发现的错误越多, hyper就会越好。

限制条件 (Restrictions)

Currently, hyper requires Python 3.3 or higher. This unfortunate restriction is caused by the fact that earlier versions of the standard library’s ssl module are totally braindead and do not support the TLS Next-Protocol-Negotiation extension, which HTTP/2.0 requires. Until I can find a good way around this limitation, hyper will remain limited to at least Python 3.3. If you don’t like this (I certainly don’t), I encourage you to take your complaint up with the Python core development team.

目前, hyper需要Python 3.3或更高版本。 这种不幸的限制是由于以下事实造成的:标准库的ssl模块的早期版本完全死了,并且不支持HTTP / 2.0要求的TLS下一协议协商扩展。 在我找到解决此限制的好方法之前, hyper将至少限于Python 3.3。 如果您不喜欢(我当然不喜欢),我鼓励您向Python核心开发团队投诉。

Additionally, you currently need to find out whether your target server supports HTTP/2.0 or not before you point hyper at it, or it’ll raise an AssertionError. This is a necessary guard in this early release, but this will be improved in future releases.

另外,您当前需要在指向hyper服务器之前确定目标服务器是否支持HTTP / 2.0,否则它将引发AssertionError 。 在此早期版本中,这是必要的防护措施,但在将来的版本中将对此进行改进。

Sadly, hyper is also currently inherently single-threaded. This means it has some unusual behaviours, but also that you need to implement your own threading mechanism on top of it if you want to use it in multi-threaded code. A suggested implementation is sketched out in the documentation, and I hope to provide example code in the near future. More long-term, hyper will become thread-safe and you’ll be able to simply have global connections.

可悲的是, hyper目前还固有地是单线程的。 这意味着它具有一些异常的行为,但是,如果要在多线程代码中使用它,则还需要在它之上实现自己的线程机制。 建议的实现已在文档中列出,我希望在不久的将来提供示例代码。 长期而言, hyper将变为线程安全的,您将可以简单地进行全局连接。

服务器推送的遗漏 (The Omission of Server Push)

I should briefly touch on the omission of Server Push from hyper. I’ve been on record in the past as saying that Server Push is fundamentally incompatible with the way most Python HTTP code is written. All the big Python HTTP client libraries are built primarily to be used from synchronous code: they expect to send a request and then block until the response comes back. They absolutely do not expect to receive multiple responses to the same request, and provide no API for receiving these responses.

我应该简要地hyper的服务器推送遗漏。 过去我一直在记录,说Server Push与大多数Python HTTP代码的编写方式根本不兼容。 所有大型Python HTTP客户端库都是主要为同步代码使用而构建的:它们希望发送请求,然后阻塞直到响应返回。 他们绝对不希望收到对同一请求的多个响应,并且不提供用于接收这些响应的API。

I don’t believe it’s possible to write a pleasant API that correctly handles Server Push without using either an event loop or having hyper spawn its own background threads. Until asyncio becomes a standard, I don’t think hyper should force you to do the first, and it’s obnoxious for a library to spawn its own threads. This means that, for the moment, hyper does not support Server Push. I do not believe this to be a big loss for 90% of the potential users of hyper, so I judge it to be an acceptable limitation. If you strongly disagree, please get in touch so we can talk about why you see this issue differently to me.

我不认为有可能编写一个令人愉快的API来正确处理Server Push,而无需使用事件循环或使用hyper派生自己的后台线程。 在asyncio成为标准之前,我不认为hyper应该强迫您执行第一个操作,并且库产生自己的线程是令人讨厌的。 这意味着,目前, hyper不支持服务器推送。 对于hyper 90%的潜在用户,我认为这不会造成太大的损失,因此我认为这是可以接受的限制。 如果您强烈不同意,请与我们联系,以便我们与您讨论为何您对此问题有所不同。

发展历程 (Development)

hyper is in early alpha and under active development on GitHub. I’d love it if people took the time to download and try out hyper. I’m particularly interested to see what kinds of bugs people hit: you’ll definitely hit them, and a detailed bug report would be really phenomenal.

hyper处于早期测试阶段,并且正在GitHub上进行积极开发。 如果人们花时间下载并试用hyper我将非常喜欢。 我特别想知道人们遇到了哪种错误:您一定会遇到这些错误,并且详细的错误报告确实非常出色。

翻译自: https://www.pybloggers.com/2014/02/http2-0-for-python/

python ./和../

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值