What are the differences between the urllib, urllib2, urllib3 and requests module?
I know it's been said already, but I'd highly recommend the requests
Python package.
If you've used languages other than python, you're probably thinking urllib
and urllib2
are easy to use, not much code, and highly capable, that's how I used to think. But the requests
package is so unbelievably useful and short that everyone should be using it.
First, it supports a fully restful API, and is as easy as:
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')
Regardless of whether GET / POST, you never have to encode parameters again, it simply takes a dictionary as an argument and is good to go:
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://www.mywebsite.com/user', data=userdata)
Plus it even has a built in JSON decoder (again, I know json.loads()
isn't a lot more to write, but this sure is convenient):
resp.json()
Or if your response data is just text, use:
resp.text
This is just the tip of the iceberg. This is the list of features from the requests site:
- International Domains and URLs
- Keep-Alive & Connection Pooling
- Sessions with Cookie Persistence
- Browser-style SSL Verification
- Basic/Digest Authentication
- Elegant Key/Value Cookies
- Automatic Decompression
- Unicode Response Bodies
- Multipart File Uploads
- Connection Timeouts
- .netrc support
- List item
- Python 2.7, 3.6—3.9
- Thread-safe.
Difference between requests.get() and urrlib.request.urlopen() python
Use the former one:
I will add the source of why it's better.
Anyways you need to set verify
as False
to prevent requests from verifying SSL certificates for HTTPS requests:
import requests
r = requests.get("https://example.com", verify=False)
Edit:
Difference between requests.get() and urllib.request.urlopen() python
What are the differences between the urllib, urllib2, and requests module?
Which urllib I should choose?
As Alexander says in the comments, use requests
. That's all you need.
Python 3 urllib Vs requests performance
First of all, to reproduce the problem, I had to add the following line to your onStringSend
function:
request.get_data()
Otherwise, I was getting “connection reset by peer” errors because the server’s receive buffer kept filling up.
Now, the immediate reason for this problem is that Response.content
(which is called implicitly when stream=False
) iterates over the response data in chunks of 10240 bytes:
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
Therefore, the easiest way to solve the problem is to use stream=True
, thus telling Requests that you will be reading the data at your own pace:
response_data = s.post(url=url, data=data, stream=True, verify=False).raw.read()
With this change, the performance of the Requests version becomes more or less the same as that of the urllib version.
Please also see the “Raw Response Content” section in the Requests docs for useful advice.
Now, the interesting question remains: why is Response.content
iterating in such small chunks? After talking to Cory Benfield, a core developer of Requests, it looks like there may be no particular reason. I filed issue #3186 in Requests to look further into this.
What python module replaces urllib2 for use with python 3 and flask?
You can use requests
library that has popularly cleaner interface to do http operations.
URL for the library.
http://www.python-requests.org/en/master/
You can do
pip install requests
on your shell/cmd to install.
In code,
import requests
response = requests.get(WEATHER_URL.format(query))
weather = response.json() # or something as easy as this.
What are the differences between the urllib, urllib2, urllib3 and requests module?
I know it's been said already, but I'd highly recommend the requests
Python package.
If you've used languages other than python, you're probably thinking urllib
and urllib2
are easy to use, not much code, and highly capable, that's how I used to think. But the requests
package is so unbelievably useful and short that everyone should be using it.
First, it supports a fully restful API, and is as easy as:
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')
Regardless of whether GET / POST, you never have to encode parameters again, it simply takes a dictionary as an argument and is good to go:
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://www.mywebsite.com/user', data=userdata)
Plus it even has a built in JSON decoder (again, I know json.loads()
isn't a lot more to write, but this sure is convenient):
resp.json()
Or if your response data is just text, use:
resp.text
This is just the tip of the iceberg. This is the list of features from the requests site:
- International Domains and URLs
- Keep-Alive & Connection Pooling
- Sessions with Cookie Persistence
- Browser-style SSL Verification
- Basic/Digest Authentication
- Elegant Key/Value Cookies
- Automatic Decompression
- Unicode Response Bodies
- Multipart File Uploads
- Connection Timeouts
- .netrc support
- List item
- Python 2.7, 3.6—3.9
- Thread-safe.
Related Topics
How to Ignore the First Line of Data When Processing CSV Data
How to Pass Another Entire Column as Argument to Pandas Fillna()
Ubuntu 'Failed to Import the Site Module' Error Message
Change Parent Shell's Environment from a Subprocess
Get Rows Based on Distinct Values from One Column
Why Is the Id of a Python Class Not Unique When Called Quickly
"Command Not Found" Using Line in Argument to Os.System Using Python
How to Install Writable Shared and User Specific Data Files with Setuptools
Using Python Subprocess.Call() to Launch an Ncurses Process
Staleelementexception When Iterating with Python
How to Rotate an Image Around an Off Center Pivot in Pygame
What Is the Best Project Structure for a Python Application