python 扭曲的 http 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2147148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:53:47  来源:igfitidea点击:

twisted http client

pythonhttpclienttwisted

提问by jldupont

I am after an example describing the usage of Twisted's HTTP Client.

我在一个描述 Twisted 的 HTTP 客户端用法的示例之后。

After reading the excellent blog post on the internals of Twisted, I understand how the "Factory" and "Protocol" components play their role but I am unclear on how to introduce "Request" in the overall Client flow.

在阅读了关于 Twisted 内部的优秀博客文章后,我了解了“工厂”和“协议”组件如何发挥作用,但我不清楚如何在整个客户端流程中引入“请求”。

More specifically, I need to be able to perform HTTP GET and POST requests to a remote server using Twisted.

更具体地说,我需要能够使用 Twisted 对远程服务器执行 HTTP GET 和 POST 请求。

Updated: after a discussion on irc #twisted / #python, it seems that twisted.web2is fading away in favor of beefing up functionality on twisted.webe.g. Agent.

更新:在对 irc #twisted / #python 进行讨论后,似乎twisted.web2正在逐渐消失,有利于增强twisted.web例如 Agent 的功能。

回答by Jean-Paul Calderone

As of Twisted 9.0, there are actually two HTTP clients available. The older one has quite a few features, such as automatically following redirects, interpreting cookie headers, etc. You can find an example of its usage here:

从 Twisted 9.0 开始,实际上有两个 HTTP 客户端可用。较旧的有很多功能,例如自动跟随重定向、解释 cookie 标头等。您可以在此处找到其用法示例:

http://twistedmatrix.com/documents/current/web/examples/

http://twistedmatrix.com/documents/current/web/examples/

(getpage.pyand dlpage.py)

(getpage.pydlpage.py)

Unfortunately, the interface presented by the older client makes a number of common tasks difficult. For example, using getPage, you cannot examine arbitrary response headers.

不幸的是,旧客户端提供的界面使许多常见任务变得困难。例如,使用getPage,您不能检查任意响应标头。

The newer HTTP client isn't yet as featureful as the old one, but it presents an interface intended to eliminate the limitations of getPage. It is also intended to be more easily extended and customized. You can find a document describing its usage here:

较新的 HTTP 客户端的功能还不如旧的,但它提供了一个旨在消除getPage. 它还旨在更容易扩展和定制。您可以在此处找到描述其用法的文档:

http://twistedmatrix.com/documents/current/web/howto/client.html

http://twistedmatrix.com/documents/current/web/howto/client.html

回答by Reshad user2701173

I started using treq with twisted. treq has an API which is very similar to Requests. https://pypi.python.org/pypi/treq/0.2.0

我开始将 treq 与扭曲一起使用。treq 有一个与请求非常相似的 API。 https://pypi.python.org/pypi/treq/0.2.0

回答by Sylvain

As of Twisted 10, you may want to use the Agentclass.

从 Twisted 10 开始,您可能想要使用Agent该类。

Please follow this link: http://twistedmatrix.com/documents/10.2.0/web/howto/client.html

请点击此链接:http: //twistedmatrix.com/documents/10.2.0/web/howto/client.html