-
Notifications
You must be signed in to change notification settings - Fork 0
Python Requests basics to intermediate
Copy code
import requests response = requests.get('https://jsonplaceholder.typicode.com/posts/1') print(response.status_code) print(response.json())
Explanation:
-
requests.get(url)sends a GET request to the specified URL. -
response.status_codeprints the HTTP status code (e.g., 200 for success). -
response.json()parses the response to a JSON object.
Copy code
import requests params = {'userId': 1} response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params) print(response.url) print(response.json())
Explanation:
-
paramsis a dictionary of query parameters to append to the URL. -
response.urlshows the full URL after parameters are added.
Copy code
import requests response = requests.get('https://jsonplaceholder.typicode.com/posts/1') print(response.headers)
Explanation:
-
response.headersreturns the headers of the response.
Copy code
import requests data = { 'title': 'foo', 'body': 'bar', 'userId': 1 } response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data) print(response.status_code) print(response.json())
Explanation:
-
json=datasends JSON data in the POST request. -
requests.post(url, json=data)sends a POST request with the specified JSON data.
Copy code
import requests data = { 'username': 'user', 'password': 'pass' } response = requests.post('https://httpbin.org/post', data=data) print(response.json())
Explanation:
-
datasends form-encoded data in the POST request.
Copy code
import requests data = { 'id': 1, 'title': 'updated title', 'body': 'updated body', 'userId': 1 } response = requests.put('https://jsonplaceholder.typicode.com/posts/1', json=data) print(response.json())
Explanation:
-
requests.put(url, json=data)sends a PUT request with the specified JSON data.
Copy code
import requests response = requests.delete('https://jsonplaceholder.typicode.com/posts/1') print(response.status_code)
Explanation:
-
requests.delete(url)sends a DELETE request to the specified URL.
Copy code
import requests try: response = requests.get('https://httpbin.org/delay/5', timeout=2) except requests.Timeout: print('The request timed out')
Explanation:
-
timeoutspecifies the maximum number of seconds to wait for a response. -
requests.Timeoutis raised if the request exceeds the timeout.
Copy code
import requests headers = { 'User-Agent': 'my-app/0.0.1' } response = requests.get('https://httpbin.org/headers', headers=headers) print(response.json())
Explanation:
-
headersis a dictionary of HTTP headers to send with the request.
Copy code
import requests response = requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass')) print(response.status_code)
Explanation:
-
authprovides basic authentication credentials.
Copy code
import requests response = requests.get('http://github.com') print(response.url) print(response.history)
Explanation:
-
response.urlshows the final URL after redirection. -
response.historyshows a list of response objects that were created to complete the request.
python
Copy code
import requests response = requests.get('https://jsonplaceholder.typicode.com/photos', stream=True) for chunk in response.iter_content(chunk_size=1024): print(chunk)
Explanation:
-
stream=Trueallows handling the content in chunks. -
response.iter_content(chunk_size)iterates over the response data in chunks.
python
Copy code
import requests url = 'https://via.placeholder.com/150' response = requests.get(url) with open('image.png', 'wb') as file: file.write(response.content)
Explanation:
-
response.contentreturns the raw bytes of the response content. -
with openopens a file in write-binary mode to save the content.
python
Copy code
import requests files = {'file': open('image.png', 'rb')} response = requests.post('https://httpbin.org/post', files=files) print(response.json())
Explanation:
-
filesis a dictionary of files to send in the POST request. -
open('filename', 'rb')opens a file in read-binary mode.
python
Copy code
import requests session = requests.Session() session.get('https://httpbin.org/cookies/set/sessioncookie/123456789') response = session.get('https://httpbin.org/cookies') print(response.json())
Explanation:
-
requests.Session()creates a session object to persist certain parameters across requests. -
session.get(url)sends a GET request within the session.
python
Copy code
import requests response = requests.get('https://httpbin.org/delay/2', timeout=(3, 5)) print(response.status_code)
Explanation:
-
timeout=(connect, read)sets the timeout for connect and read operations.
python
Copy code
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080' } response = requests.get('https://httpbin.org/ip', proxies=proxies) print(response.json())
Explanation:
-
proxiesis a dictionary of proxy URLs for HTTP and HTTPS.
python
Copy code
import requests response = requests.get('https://example.com', verify='/path/to/certfile') print(response.status_code)
Explanation:
-
verifyspecifies the path to a custom SSL certificate file.
python
Copy code
import requests url = 'https://httpbin.org/cookies/set/sessioncookie/123456789' response = requests.get(url) print(response.cookies['sessioncookie'])
Explanation:
-
response.cookiesis a dictionary-like object containing cookies sent by the server.
python
Copy code
import requests from bs4 import BeautifulSoup response = requests.get('https://example.com') soup = BeautifulSoup(response.text, 'html.parser') print(soup.title.string)
Explanation:
-
response.textreturns the response content as a string. -
BeautifulSoupparses the HTML content and provides methods to navigate and search the parse tree.
python
Copy code
import requests response = requests.get('https://jsonplaceholder.typicode.com/users/1') data = response.json() print(data['address']['geo'])
Explanation:
- This example fetches user data and extracts the nested
geoinformation within theaddressfield.
python
Copy code
import requests try: response = requests.get('https://jsonplaceholder.typicode.com/invalid-url') response.raise_for_status() except requests.exceptions.HTTPError as err: print(f"HTTP error occurred: {err}") except Exception as err: print(f"Other error occurred: {err}")
Explanation:
- This code handles HTTP errors using
raise_for_status()and catches other exceptions usingtry/except.
python
Copy code
import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504]) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) response = session.get('https://httpbin.org/status/500') print(response.status_code)
Explanation:
- This code sets up automatic retries for failed requests using
RetryandHTTPAdapter.
python
Copy code
import requests session = requests.Session() session.get('https://httpbin.org/cookies/set/sessioncookie/123456789') response = session.get('https://httpbin.org/cookies') print(response.json())
Explanation:
- Sessions keep cookies between requests, useful for maintaining login sessions.
python
Copy code
import requests url = 'https://jsonplaceholder.typicode.com/posts' data = { 'title': 'foo', 'body': 'bar', 'userId': 1 } response = requests.post(url, json=data) print(response.json())
Explanation:
- This example sends a POST request with JSON data and prints the response.
python
Copy code
import requests url = 'https://httpbin.org/post' files = {'file': open('report.xls', 'rb')} response = requests.post(url, files=files) print(response.json())
Explanation:
- This code uploads a file using multipart form data in a POST request.
python
Copy code
import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry = Retry(total=3, backoff_factor=0.3) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) try: response = session.get('https://httpbin.org/delay/5', timeout=2) except requests.exceptions.Timeout: print("The request timed out")
Explanation:
- This code handles request timeouts and retries the request up to 3 times with exponential backoff.
python
Copy code
import requests response = requests.get('https://expired.badssl.com/', verify=False) print(response.status_code)
Explanation:
-
verify=Falsedisables SSL verification, useful for testing but not recommended for production.
python
Copy code
import requests from requests.auth import HTTPBasicAuth response = requests.get('https://httpbin.org/basic-auth/user/pass', auth=HTTPBasicAuth('user', 'pass')) print(response.status_code)
Explanation:
- This code demonstrates how to use HTTP basic authentication with the
HTTPBasicAuthclass.
python
Copy code
import requests url = 'https://httpbin.org/headers' headers = {'User-Agent': 'my-app/0.0.1'} response = requests.get(url, headers=headers) print(response.json())
Explanation:
- Custom headers can be added to the request by passing a dictionary to the
headersparameter.
python
Copy code
import requests import xml.etree.ElementTree as ET response = requests.get('https://www.w3schools.com/xml/note.xml') root = ET.fromstring(response.content) for child in root: print(child.tag, child.text)
Explanation:
- This example parses an XML response using
ElementTreeand prints each element's tag and text.
python
Copy code
import requests response = requests.get('http://github.com') print(response.url) print(response.history)
Explanation:
-
response.historyprovides a list of redirect responses that were followed to reach the final URL.
python
Copy code
import requests params = {'page': 2, 'count': 10} response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params) print(response.url)
Explanation:
- Query parameters can be added to the URL by passing a dictionary to the
paramsparameter.
python
Copy code
import requests url = 'https://speed.hetzner.de/100MB.bin' response = requests.get(url, stream=True) with open('large_file.bin', 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk)
Explanation:
-
stream=Trueenables streaming, anditer_contentallows downloading large files in chunks.
python
Copy code
import requests url = 'https://httpbin.org/post' data = { 'user': { 'id': 1, 'name': 'John Doe', 'contacts': [ {'type': 'email', 'value': 'john.doe@example.com'}, {'type': 'phone', 'value': '123-456-7890'} ] } } response = requests.post(url, json=data) print(response.json())
Explanation:
- This example sends complex nested JSON data in a POST request.
python
Copy code
import requests try: response = requests.get('https://httpbin.org/status/404') response.raise_for_status() except requests.exceptions.HTTPError as http_err: print(f'HTTP error occurred: {http_err}') except Exception as err: print(f'Other error occurred: {err}') else: print('Success!')
Explanation:
- This code demonstrates advanced error handling with
raise_for_status()and multiple exception types.
python
Copy code
import requests def response_hook(response, *args, **kwargs): print(response.status_code) response = requests.get('https://httpbin.org/get', hooks={'response': response_hook})
Explanation:
- Hooks allow attaching custom callback functions to request events.
python
Copy code
import requests session = requests.Session() session.get('https://httpbin.org/cookies/set/sessioncookie/123456789') response = session.get('https://httpbin.org/cookies') print(response.json())
Explanation:
- This example demonstrates how session cookies are preserved across requests.
python
Copy code
import requests response = requests.get('https://httpbin.org/delay/1', timeout=(5, 10)) print(response.status_code)
Explanation:
- Separate timeouts can be set for the connect and read operations by passing a tuple to the
timeoutparameter.
python
Copy code
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080' } response = requests.get('https://httpbin.org/ip', proxies=proxies) print(response.json())
Explanation:
- Proxies can be configured for both HTTP and HTTPS requests by passing a dictionary to the
proxiesparameter.
python
Copy code
import requests response = requests.get('https://jsonplaceholder.typicode.com/users/1') data = response.json() print(data['address']['geo'])
Explanation:
-
requests.get()sends a GET request to the specified URL. -
.json()method converts the response to a Python dictionary. -
data['address']['geo']accesses the nestedgeofield inside theaddressdictionary.
python
Copy code
import requests try: response = requests.get('https://jsonplaceholder.typicode.com/invalid-url') response.raise_for_status() except requests.exceptions.HTTPError as err: print(f"HTTP error occurred: {err}") except Exception as err: print(f"Other error occurred: {err}")
Explanation:
- The
tryblock attempts to send a GET request. -
raise_for_status()will raise an HTTPError if the HTTP request returned an unsuccessful status code. - The
exceptblocks catch and print specific errors.
python
Copy code
import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504]) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) response = session.get('https://httpbin.org/status/500') print(response.status_code)
Explanation:
-
Retrysets up retry logic with a backoff factor. -
HTTPAdaptermounts the retry logic to the session. -
session.get()uses the retry logic for the request.
python
Copy code
import requests session = requests.Session() session.get('https://httpbin.org/cookies/set/sessioncookie/123456789') response = session.get('https://httpbin.org/cookies') print(response.json())
Explanation:
-
requests.Session()creates a session object. - Cookies set in one request are persisted and used in subsequent requests.
python
Copy code
import requests url = 'https://jsonplaceholder.typicode.com/posts' data = { 'title': 'foo', 'body': 'bar', 'userId': 1 } response = requests.post(url, json=data) print(response.json())
Explanation:
-
requests.post()sends a POST request with JSON data. - The
jsonparameter is used to send JSON data in the body of the request.
python
Copy code
import requests url = 'https://httpbin.org/post' files = {'file': open('report.xls', 'rb')} response = requests.post(url, files=files) print(response.json())
Explanation:
-
filesparameter allows file uploads. -
'rb'mode opens the file in binary read mode for uploading.
python
Copy code
import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry = Retry(total=3, backoff_factor=0.3) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) try: response = session.get('https://httpbin.org/delay/5', timeout=2) except requests.exceptions.Timeout: print("The request timed out")
Explanation:
-
timeout=2sets a timeout for the request. -
Retrylogic retries the request if it fails.
python
Copy code
import requests response = requests.get('https://expired.badssl.com/', verify=False) print(response.status_code)
Explanation:
-
verify=Falsedisables SSL verification. - Useful for testing, but not recommended for production due to security risks.
python
Copy code
import requests from requests.auth import HTTPBasicAuth response = requests.get('https://httpbin.org/basic-auth/user/pass', auth=HTTPBasicAuth('user', 'pass')) print(response.status_code)
Explanation:
-
HTTPBasicAuthis used to provide basic authentication credentials.
python
Copy code
import requests url = 'https://httpbin.org/headers' headers = {'User-Agent': 'my-app/0.0.1'} response = requests.get(url, headers=headers) print(response.json())
Explanation:
- Custom headers can be sent with a request using the
headersparameter.
python
Copy code
import requests import xml.etree.ElementTree as ET response = requests.get('https://www.w3schools.com/xml/note.xml') root = ET.fromstring(response.content) for child in root: print(child.tag, child.text)
Explanation:
-
ET.fromstring()parses XML content. - Iterating through the XML tree to print tags and text.
python
Copy code
import requests response = requests.get('http://github.com') print(response.url) print(response.history)
Explanation:
-
response.historycontains the list of redirect responses. -
response.urlis the final URL after all redirects.
python
Copy code
import requests params = {'page': 2, 'count': 10} response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params) print(response.url)
Explanation:
- Query parameters can be passed using the
paramsparameter. -
paramsis a dictionary of query parameters.
python
Copy code
import requests url = 'https://speed.hetzner.de/100MB.bin' response = requests.get(url, stream=True) with open('large_file.bin', 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk)
Explanation:
-
stream=Trueenables streaming mode. -
iter_content()allows downloading in chunks to handle large files.
python
Copy code
import requests url = 'https://httpbin.org/post' data = { 'user': { 'id': 1, 'name': 'John Doe', 'contacts': [ {'type': 'email', 'value': 'john.doe@example.com'}, {'type': 'phone', 'value': '123-456-7890'} ] } } response = requests.post(url, json=data) print(response.json())
Explanation:
- Complex nested JSON data can be sent using the
jsonparameter.
python
Copy code
import requests try: response = requests.get('https://httpbin.org/status/404') response.raise_for_status() except requests.exceptions.HTTPError as http_err: print(f'HTTP error occurred: {http_err}') except Exception as err: print(f'Other error occurred: {err}') else: print('Success!')
Explanation:
-
raise_for_status()raises an HTTPError for bad responses. -
elseblock runs if no exception is raised.
python
Copy code
import requests def response_hook(response, *args, **kwargs): print(response.status_code) response = requests.get('https://httpbin.org/get', hooks={'response': response_hook})
Explanation:
- Hooks allow attaching custom callback functions to request events.
python
Copy code
import requests session = requests.Session() session.get('https://httpbin.org/cookies/set/sessioncookie/123456789') response = session.get('https://httpbin.org/cookies') print(response.json())
Explanation:
- Session cookies are persisted across requests within the session.
python
Copy code
import requests response = requests.get('https://httpbin.org/delay/1', timeout=(5, 10)) print(response.status_code)
Explanation:
-
timeoutcan be a tuple(connect timeout, read timeout).
python
Copy code
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080' } response = requests.get('https://httpbin.org/ip', proxies=proxies) print(response.json())
Explanation:
- Proxies can be configured for both HTTP and HTTPS requests using the
proxiesparameter.
These examples cover a range of intermediate-level topics and should give you a solid un
derstanding of how to use the requests library in Python for more complex tasks.
Code Example:
python
Copy code
import requests response = requests.get('https://jsonplaceholder.typicode.com/users/1') data = response.json() print(data['address']['geo'])
Explanation:
-
requests.get('URL')sends a GET request to the specified URL. - The
.json()method converts the response to a Python dictionary. -
data['address']['geo']accesses the nestedgeofield inside theaddressdictionary.
Key Points:
- JSON responses are easily handled by converting them to dictionaries.
- Accessing nested structures requires understanding the hierarchy.
Practical Tip: Use JSON data to parse and manipulate responses from web APIs, making it easy to work with structured data.
Code Example:
python
Copy code
import requests try: response = requests.get('https://jsonplaceholder.typicode.com/invalid-url') response.raise_for_status() except requests.exceptions.HTTPError as err: print(f"HTTP error occurred: {err}") except Exception as err: print(f"Other error occurred: {err}")
Explanation:
- The
tryblock attempts to send a GET request. -
raise_for_status()will raise anHTTPErrorif the HTTP request returned an unsuccessful status code. - The
exceptblocks catch and print specific errors.
Key Points:
- Error handling is crucial for robust programs.
- Use
try/exceptto manage exceptions and provide meaningful messages.
Practical Tip: Always handle potential errors in network requests to avoid crashes and ensure a smooth user experience.
Code Example:
python
Copy code
import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504]) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) response = session.get('https://httpbin.org/status/500') print(response.status_code)
Explanation:
-
Retrysets up retry logic with a backoff factor. -
HTTPAdaptermounts the retry logic to the session. -
session.get()uses the retry logic for the request.
Key Points:
- Retries help handle transient errors gracefully.
- The
backoff_factorcontrols the delay between retries.
Practical Tip: Implement retry logic to make your application more resilient to temporary network issues.
Code Example:
python
Copy code
import requests session = requests.Session() session.get('https://httpbin.org/cookies/set/sessioncookie/123456789') response = session.get('https://httpbin.org/cookies') print(response.json())
Explanation:
-
requests.Session()creates a session object. - Cookies set in one request are persisted and used in subsequent requests.
Key Points:
- Sessions allow cookies to persist across multiple requests.
- Useful for maintaining stateful interactions with a web service.
Practical Tip: Use sessions to handle login cookies and maintain state across multiple requests without needing to manually manage cookies.
Code Example:
python
Copy code
import requests url = 'https://jsonplaceholder.typicode.com/posts' data = { 'title': 'foo', 'body': 'bar', 'userId': 1 } response = requests.post(url, json=data) print(response.json())
Explanation:
-
requests.post()sends a POST request with JSON data. - The
jsonparameter is used to send JSON data in the body of the request.
Key Points:
- Posting JSON data is straightforward with the
jsonparameter. - This is commonly used for creating or updating resources in web APIs.
Practical Tip: Use JSON data to communicate structured information with your web service endpoints efficiently.
Code Example:
python
Copy code
import requests url = 'https://httpbin.org/post' files = {'file': open('report.xls', 'rb')} response = requests.post(url, files=files) print(response.json())
Explanation:
- The
filesparameter allows file uploads. -
'rb'mode opens the file in binary read mode for uploading.
Key Points:
- Multipart form data is used for uploading files.
- Ensure files are opened in binary mode for compatibility.
Practical Tip: Use multipart form data to upload files to a server, such as images, documents, or any binary data.
Code Example:
python
Copy code
import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry = Retry(total=3, backoff_factor=0.3) adapter = HTTPAdapter(max_retries=retry) session.mount('http://', adapter) session.mount('https://', adapter) try: response = session.get('https://httpbin.org/delay/5', timeout=2) except requests.exceptions.Timeout: print("The request timed out")
Explanation:
-
timeout=2sets a timeout for the request. - Retry logic retries the request if it fails.
Key Points:
- Timeouts prevent your application from hanging indefinitely.
- Retries with backoff ensure resilience against temporary issues.
Practical Tip: Configure timeouts and retries to improve the reliability and performance of your application.
Code Example:
python
Copy code
import requests response = requests.get('https://expired.badssl.com/', verify=False) print(response.status_code)
Explanation:
-
verify=Falsedisables SSL verification. - Useful for testing, but not recommended for production due to security risks.
Key Points:
- Disabling SSL verification can expose your application to security risks.
- Only disable SSL verification in a controlled testing environment.
Practical Tip: Always enable SSL verification in production to ensure secure communication.
Code Example:
python
Copy code
import requests from requests.auth import HTTPBasicAuth response = requests.get('https://httpbin.org/basic-auth/user/pass', auth=HTTPBasicAuth('user', 'pass')) print(response.status_code)
Explanation:
-
HTTPBasicAuthis used to provide basic authentication credentials.
Key Points:
- Basic authentication sends user credentials with each request.
- Ensure sensitive information is transmitted securely.
Practical Tip: Use custom authentication to access protected resources, ensuring credentials are handled securely.
Code Example:
python
Copy code
import requests url = 'https://httpbin.org/headers' headers = {'User-Agent': 'my-app/0.0.1'} response = requests.get(url, headers=headers) print(response.json())
Explanation:
- Custom headers can be sent with a request using the
headersparameter.
Key Points:
- Headers can provide additional information to the server.
- Common headers include
User-Agent,Authorization, andContent-Type.
Practical Tip: Use custom headers to tailor your requests and provide necessary context or authentication tokens.
Code Example:
python
Copy code
import requests import xml.etree.ElementTree as ET response = requests.get('https://www.w3schools.com/xml/note.xml') root = ET.fromstring(response.content) for child in root: print(child.tag, child.text)
Explanation:
-
ET.fromstring()parses XML content. - Iterating through the XML tree to print tags and text.
Key Points:
- XML responses can be parsed using the
xml.etree.ElementTreemodule. - Access elements and attributes using standard XML parsing techniques.
Practical Tip: Use XML parsing to handle responses from APIs that return XML data, such as some legacy systems or specific web services.
Code Example:
python
Copy code
import requests response = requests.get('http://github.com') print(response.url) print(response.history)
Explanation:
-
response.historycontains the list of redirect responses. -
response.urlis the final URL after all redirects.
Key Points:
- Requests automatically follow redirects by default.
-
response.historyhelps trace the sequence of redirects.
Practical Tip: Monitor redirects to understand how requests are being routed and ensure you're reaching the intended endpoint.
Code Example:
python
Copy code
import requests params = {'page': 2, 'count': 10} response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params) print(response.url)
Explanation:
- Query parameters can be passed using the
paramsparameter. -
paramsis a dictionary of query parameters.
Key Points:
- Query parameters are appended to the URL.
- Useful for filtering and paginating results.
Practical Tip: Use query parameters to refine your API requests and retrieve specific data subsets.
Code Example:
python
Copy code
import requests url = 'https://speed.hetzner.de/100MB.bin' response = requests.get(url, stream=True) with open('large_file.bin', 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk)
Explanation:
-
stream=Trueenables streaming mode. -
iter_content()iterates over the response in chunks.
Key Points:
- Streaming is efficient for downloading large files.
- Chunked writing prevents high memory usage.
Practical Tip: Stream and save large files in manageable chunks to avoid memory overload.
Code Example:
python
Copy code
import requests url = 'https://httpbin.org/post' files = {'file': ('large_file.bin', open('large_file.bin', 'rb'), 'application/octet-stream')} response = requests.post(url, files=files) print(response.json())
Explanation:
- The
filesparameter handles file uploads. - Specify the file name, file object, and MIME type.
Key Points:
- Uploading large files requires specifying appropriate MIME types.
- Ensure files are opened in binary mode for compatibility.
Practical Tip: Upload large files using appropriate MIME types to ensure the server handles them correctly.
Code Example:
python
Copy code
import requests proxies = { 'http': 'http://10.10.1.10:3128', 'https': 'http://10.10.1.10:1080', } response = requests.get('https://httpbin.org/ip', proxies=proxies) print(response.json())
Explanation:
- The
proxiesparameter allows requests to go through specified proxy servers.
Key Points:
- Proxies can be used for security, caching, or bypassing restrictions.
- Ensure proxy settings are correctly configured.
Practical Tip: Use proxies to enhance security, manage traffic, or access restricted content.
Code Example:
python
Copy code
import requests response = requests.get('https://httpbin.org/delay/2', timeout=1) print(response.status_code)
Explanation:
- The
timeoutparameter specifies the maximum time to wait for a response.
Key Points:
- Timeouts prevent indefinite waits.
- Can be set for both connection and read operations.
Practical Tip: Set appropriate timeouts to ensure your application remains responsive.
Code Example:
python
Copy code
import requests session = requests.Session() session.headers.update({'User-Agent': 'my-app/0.0.1'}) response = session.get('https://httpbin.org/headers') print(response.json())
Explanation:
-
requests.Session()creates a session object. - Session headers are persisted across requests.
Key Points:
- Reusing session objects can improve performance.
- Useful for maintaining state and settings across multiple requests.
Practical Tip: Reuse session objects to optimize performance and manage stateful interactions efficiently.
Code Example:
python
Copy code
import requests import logging logging.basicConfig(level=logging.DEBUG) response = requests.get('https://httpbin.org/get') print(response.text)
Explanation:
-
logging.basicConfig(level=logging.DEBUG)enables debug logging. - Detailed request and response information is logged.
Key Points:
- Debugging provides insights into request/response cycles.
- Useful for troubleshooting and optimizing requests.
Practical Tip: Enable debug logging to diagnose issues and ensure your requests are functioning as expected.
Code Example:
python
Copy code
import requests from requests.auth import HTTPDigestAuth url = 'https://httpbin.org/digest-auth/auth/user/pass' response = requests.get(url, auth=HTTPDigestAuth('user', 'pass')) print(response.status_code)
Explanation:
-
HTTPDigestAuthprovides digest authentication. - More secure than basic authentication.
Key Points:
- Digest authentication adds security by hashing credentials.
- Use when interacting with APIs that require enhanced security.
Practical Tip: Opt for advanced authentication methods when dealing with sensitive or secure API endpoints.
These notes provide a comprehensive guide to using the requests module in Python, covering a range of scenarios and best practices. Each example is designed to be practical and engaging, with detailed explanations to help you understand the concepts thoroughly.