Python urllib 与 httplib?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3305250/
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-08-18 10:22:13  来源:igfitidea点击:

Python urllib vs httplib?

pythonhttpurllibhttplib

提问by jahmax

When would someone use httplib and when urllib?

有人会在什么时候使用 httplib,什么时候使用 urllib?

What are the differences?

有什么区别?

I think I ready urllib uses httplib, I am planning to make an app that will need to make http request and so far I only used httplib.HTTPConnection in python for requests, and reading about urllib I see I can use that for request too, so whats the benefit of one or the other?

我想我已经准备好 urllib 使用 httplib,我打算制作一个需要发出 http 请求的应用程序,到目前为止我只在 python 中使用了 httplib.HTTPConnection 来请求,阅读关于 urllib 我看到我也可以将它用于请求,那么一个或另一个的好处是什么?

采纳答案by Robus

urllib (particularly urllib2) handles many things by default or has appropriate libs to do so. For example, urllib2 will follow redirects automatically and you can use cookiejar to handle login scripts. These are all things you'd have to code yourself if you were using httplib.

urllib(特别是 urllib2)在默认情况下处理许多事情或有适当的库来这样做。例如,urllib2 将自动跟随重定向,您可以使用 cookiejar 来处理登录脚本。如果您使用 httplib,这些都是您必须自己编写的代码。

回答by Matt Joiner

If you're dealing solely with http/https and need access to HTTP specific stuff, use httplib.

如果您只处理 http/https 并且需要访问特定于 HTTP 的内容,请使用 httplib。

For all other cases, use urllib2.

对于所有其他情况,请使用 urllib2。

回答by Corey Goldberg

urllib/urllib2 is built on top of httplib. It offers more features than writing to httplib directly.

urllib/urllib2 建立在 httplib 之上。它提供了比直接写入 httplib 更多的功能。

however, httplib gives you finer control over the underlying connections.

但是,httplib 使您可以更好地控制底层连接。

回答by optixx

If you need high level stuff like Caching, Keep-Alive, Compression or Authentication, tryhttplib2

如果您需要缓存、Keep-Alive、压缩或身份验证等高级内容,请尝试httplib2

回答by Lihang Li

I would like to say something about urllib, urllib2, httpliband httplib2.

我想谈一下urlliburllib2httplibhttplib2

The main different between urllib*and httplib*is that:

urllib*和之间的主要区别在于httplib*

httplib and httplib2 handles HTTP/HTTPs request and response directly and give you more space to do your own job.

httplib 和 httplib2 直接处理 HTTP/HTTPs 请求和响应,为您提供更多空间来完成自己的工作。

urllib and urllib2 are build upon httplib, they are more abstract and powerful, but sometimes won't fulfill your specified need about some HTTP related operations.

urllib 和 urllib2 建立在 httplib 之上,它们更加抽象和强大,但有时无法满足您对某些 HTTP 相关操作的指定需求。

And for httpliband httplib2, I'd say that they are both HTTP client library. However httplib2is much more powerful and have much more features than httplib.

对于httplibhttplib2,我想说它们都是HTTP client library。然而,httplib2它比httplib.

As for urlliband urllib2, quote from this link:

至于urlliband urllib2,引自此链接

urllib and urllib2 are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:

  • urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL. That means, you cannot masquerade your User Agent string etc.
  • urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

urllib 和 urllib2 都是 Python 模块,它们执行 URL 请求相关的内容,但提供不同的功能。下面列出了它们的两个最显着差异:

  • urllib2 可以接受一个 Request 对象来设置 URL 请求的标头,urllib 只接受一个 URL。这意味着,您不能伪装您的用户代理字符串等。
  • urllib 提供了用于生成 GET 查询字符串的 urlencode 方法,urllib2 没有这样的功能。这是 urllib 经常与 urllib2 一起使用的原因之一。

I would recommend my personal blog Httplib Httplib2 Urllib Urllib2-what's the Difference.

我会推荐我的个人博客Httplib Httplib2 Urllib Urllib2-what's the Difference

Hope it helps:-)

希望能帮助到你:-)

回答by Cometsong

For those folks moving things up to Py3 (and for some reason cannot or have not refactored to use the awesome requestsmodule), this is a good transition between versions:

对于那些将事情升级到 Py3 的人(并且由于某种原因不能或没有重构以使用 awesome requests模块),这是一个很好的版本之间的转换:

try:
    import http.client as httplib
except ImportError:
    import httplib

Works in both Python version sets.

适用于两个 Python 版本集。