python-aiohttpHow to make request with basic HTTP auth using Python Aiohttp?
Using Aiohttp, you can make a request with basic HTTP auth using the aiohttp.BasicAuth
class. The following example code will make a request to a URL with basic auth credentials:
import aiohttp
async with aiohttp.ClientSession() as session:
auth = aiohttp.BasicAuth(login='username', password='password')
async with session.get('http://example.com', auth=auth) as resp:
print(resp.status)
The output of the above code will be 200
, indicating that the request was successful.
Code explanation
import aiohttp
: imports the Aiohttp library.aiohttp.BasicAuth(login='username', password='password')
: creates an instance of theaiohttp.BasicAuth
class with the username and password credentials.session.get('http://example.com', auth=auth)
: makes a request to the URL with the basic auth credentials.print(resp.status)
: prints the response status code.
Helpful links
Related
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to disable SSL verification in Python Aiohttp?
- How to create a connection pool with Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to close a Python Aiohttp session?
- How to rate limit with Python Aiohttp?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- Bearer token request example with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to get response text with Python Aiohttp?
- How to rate limit with Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to retry a request with Python Aiohttp?
See more codes...