Program Tip

urllib, urllib2, urllib3 및 요청 모듈의 차이점은 무엇입니까?

programtip 2020. 9. 30. 11:23
반응형

urllib, urllib2, urllib3 및 요청 모듈의 차이점은 무엇입니까?


파이썬에서의 차이 무엇인가 urllib, urllib2, urllib3requests모듈은? 왜 세 개입니까? 그들은 같은 일을하는 것 같습니다 ...


이미 말했음을 알고 있지만 requestsPython 패키지를 적극 권장합니다 .

파이썬 이외의 언어를 사용했다면 아마도 생각 urllib하고 urllib2사용하기 쉽고 코드가 많지 않고 능력이 뛰어납니다. 그게 제가 생각했던 방식입니다. 그러나 requests패키지는 믿을 수 없을 정도로 유용하고 짧기 때문에 모든 사람이 사용해야합니다.

첫째, 완전히 편안한 API를 지원하며 다음과 같이 쉽습니다.

import requests

resp = requests.get('http://www.mywebsite.com/user')
resp = requests.post('http://www.mywebsite.com/user')
resp = requests.put('http://www.mywebsite.com/user/put')
resp = requests.delete('http://www.mywebsite.com/user/delete')

GET / POST 여부에 관계없이 매개 변수를 다시 인코딩 할 필요가 없습니다. 단순히 사전을 인수로 사용하므로 이동하는 것이 좋습니다.

userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://www.mywebsite.com/user', data=userdata)

또한 JSON 디코더가 내장되어 있습니다 (다시 json.loads()작성해야 할 내용이 많지는 않지만 확실히 편리합니다).

resp.json()

또는 응답 데이터가 텍스트 인 경우 다음을 사용하십시오.

resp.text

이것은 빙산의 일각에 불과합니다. 다음은 요청 사이트의 기능 목록입니다.

  • 국제 도메인 및 URL
  • Keep-Alive 및 연결 풀링
  • 쿠키 지속성이있는 세션
  • 브라우저 스타일 SSL 확인
  • 기본 / 다이제스트 인증
  • 우아한 키 / 값 쿠키
  • 자동 감압
  • 유니 코드 응답 기관
  • 멀티 파트 파일 업로드
  • 연결 시간 초과
  • .netrc 지원
  • 목록 항목
  • Python 2.6 ~ 3.4
  • 스레드로부터 안전합니다.

urllib2는 몇 가지 추가 기능을 제공합니다. 즉,이 urlopen()함수를 사용하면 헤더를 지정할 수 있습니다 (일반적으로 훨씬 더 장황한 과거에 httplib를 사용해야했습니다). 더 중요한 것은 urllib2 Request가 더 많은 것을 허용 하는 클래스를 제공 한다는 것입니다. 요청 수행에 대한 선언적 접근 방식 :

r = Request(url='http://www.mysite.com')
r.add_header('User-Agent', 'awesome fetcher')
r.add_data(urllib.urlencode({'foo': 'bar'})
response = urlopen(r)

참고 urlencode()만 URLLIB에 urllib2가 없습니다.

urllib2에서 고급 URL 지원을 구현하기위한 핸들러도 있습니다. 짧은 대답은 레거시 코드로 작업하지 않는 한 urllib2의 URL 오프너를 사용하고 싶겠지 만 일부 유틸리티 기능을 위해 urllib로 가져와야한다는 것입니다.

Bonus answer With Google App Engine, you can use any of httplib, urllib or urllib2, but all of them are just wrappers for Google's URL Fetch API. That is, you are still subject to the same limitations such as ports, protocols, and the length of the response allowed. You can use the core of the libraries as you would expect for retrieving HTTP URLs, though.


urllib and urllib2 are both Python modules that do URL request related stuff but offer different functionalities.

1) urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL.

2) 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.

Requests - Requests’ is a simple, easy-to-use HTTP library written in Python.

1) Python Requests encodes the parameters automatically so you just pass them as simple arguments, unlike in the case of urllib, where you need to use the method urllib.encode() to encode the parameters before passing them.

2) It automatically decoded the response into Unicode.

3) Requests also has far more convenient error handling.If your authentication failed, urllib2 would raise a urllib2.URLError, while Requests would return a normal response object, as expected. All you have to see if the request was successful by boolean response.ok

For example reference - https://dancallahan.info/journal/python-requests/


urllib2.urlopen accepts an instance of the Request class or a url, whereas urllib.urlopen only accepts a url.

A similar discussion took place here: http://www.velocityreviews.com/forums/t326690-urllib-urllib2-what-is-the-difference.html


I like the urllib.urlencode function, and it doesn't appear to exist in urllib2.

>>> urllib.urlencode({'abc':'d f', 'def': '-!2'})
'abc=d+f&def=-%212'

One considerable difference is about porting Python2 to Python3. urllib2 does not exist for python3 and its methods ported to urllib. So you are using that heavily and want to migrate to Python3 in future, consider using urllib. However 2to3 tool will automatically do most of the work for you.


Just to add to the existing answers, I don't see anyone mentioning that python requests is not a native library. If you are ok with adding dependencies, then requests is fine. However, if you are trying to avoid adding dependencies, urllib is a native python library that is already available to you.


You should generally use urllib2, since this makes things a bit easier at times by accepting Request objects and will also raise a URLException on protocol errors. With Google App Engine though, you can't use either. You have to use the URL Fetch API that Google provides in its sandboxed Python environment.


To get the content of a url:

try: # Try importing requests first.
    import requests
except ImportError: 
    try: # Try importing Python3 urllib
        import urllib.request
    except AttributeError: # Now importing Python2 urllib
        import urllib


def get_content(url):
    try:  # Using requests.
        return requests.get(url).content # Returns requests.models.Response.
    except NameError:  
        try: # Using Python3 urllib.
            with urllib.request.urlopen(index_url) as response:
                return response.read() # Returns http.client.HTTPResponse.
        except AttributeError: # Using Python3 urllib.
            return urllib.urlopen(url).read() # Returns an instance.

It's hard to write Python2 and Python3 and request dependencies code for the responses because they urlopen() functions and requests.get() function return different types:

  • Python2 urllib.request.urlopen() returns a http.client.HTTPResponse
  • Python3 urllib.urlopen(url) returns an instance
  • Request request.get(url) returns a requests.models.Response

A key point that I find missing in the above answers is that urllib returns an object of type <class http.client.HTTPResponse> whereas requests returns <class 'requests.models.Response'>.

Due to this, read() method can be used with urllib but not with requests.

P.S. : requests is already rich with so many methods that it hardly needs one more as read() ;>

참고URL : https://stackoverflow.com/questions/2018026/what-are-the-differences-between-the-urllib-urllib2-urllib3-and-requests-modul

반응형