DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
importostar
importostar

Posted on

What is the fastest way to HTTP GET in Python?

What is the fastest way to do a HTTP GET in a Python program?

To start the HTTP GET method in python, any of the following modules can be used:

You can install all of the above module using pip package manager and the easiest module is the request module.

The example in this blog post shows how the requests module can be used.

Install requests

You can use pip to install the module

pip install requests

IF you use PyCharm, you can install it in project settings.

In the console it outputs something like this:

    Collecting requests
      Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 58 kB 2.4 MB/s 
    Collecting certifi>=2017.4.17
      Downloading certifi-2019.11.28-py2.py3-none-any.whl (156 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 156 kB 9.3 MB/s 
    Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
      Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 125 kB 10.6 MB/s 
    Collecting chardet<4,>=3.0.2
      Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
    Collecting idna<3,>=2.5
      Downloading idna-2.9-py2.py3-none-any.whl (58 kB)
         |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 58 kB 4.1 MB/s 
    Installing collected packages: certifi, urllib3, chardet, idna, requests
    Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.8

Requests HTTP GET method example

Import the requests module, set the url and do a requests.get() call.

This returns a response object, response.text contains the data.

>>> import requests
>>> url = "https://dev.to"
>>> response = requests.get(url=url)
>>> print(response)
<Response [200]>
>>> print(response.text)

This outputs:

<!DOCTYPE html>
<html lang="en">
  <head>
   etc

If your app uses a GUI module like PyQt, then you'll want to do HTTP requests in another thread (not on the GUI thread).

Top comments (0)

🌚 Life is too short to browse without dark mode