Python requests proxy error

Python Requests — 407 Proxy Authentication Required

A 407 Proxy Authentication Required response is often returned either when a request being made through a proxy requires authentication or if the credentials being passed are invalid or have been incorrectly escaped. The following are a number of different approaches on how you can fix this issue, as some proxies require different implementations.

Proxy Authentication using Requests’ Session Feature

Python’s requests library lets you easily configure the proxy on a per-request basis using a session object.

import requests from urllib3.util import parse_url, Url from urllib.parse import quote proxy_url_credentials = self.add_creds_to_proxy_url('http://someproxyurl.com:8080/', 'some_user', 'some_password') proxies = 'http': proxy_url_credentials, 'https': proxy_url_credentials> session = requests.session() session.proxies.update(proxies) response = session.get('https://google.com/', verify=False) print(response.text) def add_creds_to_proxy_url(self, url, username, password) url_dict = parse_url(url)._asdict() url_dict['auth'] = username + ':' + quote(password, '') return Url(**url_dict).url 

The above code will configure your proxy url so that the credentials are included and the password has been percent-encoded . This is important because passwords can often contain various symbols that are not permitted in an HTTP request. This often results in errors such as:

requests.exceptions.ProxyError Max retries exceeded with url: Failed to establish a new connection [Errno -5] No address associated with hostname [Errno -3] Temporary failure in name resolution 

Python Articles

See more of my articles on Python:

Источник

How to fix python requests with proxy results in sslerror wrong_version_number?

The Python Requests library is a powerful tool for making HTTP requests, but it can sometimes run into issues when working with a proxy. One common error that users may encounter is the «SSLError: WRONG_VERSION_NUMBER» error, which is raised when the client and server are using different SSL/TLS versions. This can occur for a variety of reasons, including mismatches in the versions supported by the client, the proxy, and the server, or issues with the SSL certificate being used. To resolve this error, several methods can be tried.

Method 1: Upgrade to a newer version of Requests

To fix the SSLError WRONG_VERSION_NUMBER issue with Python requests and proxy, the best solution is to upgrade to a newer version of Requests. Here are the steps to do it:

    Check the current version of Requests by running the following command in your terminal:

pip freeze | grep requests
pip install --upgrade requests
import requests proxies =  'http': 'http://user:pass@proxy:port', 'https': 'https://user:pass@proxy:port' > response = requests.get('https://www.example.com', proxies=proxies, verify='/path/to/certfile')
import requests proxies =  'http': 'http://user:pass@proxy:port', 'https': 'https://user:pass@proxy:port' > response = requests.get('https://www.example.com', proxies=proxies, verify=False)

That’s it! By upgrading to a newer version of Requests and specifying the SSL certificates to use, you can fix the SSLError WRONG_VERSION_NUMBER issue with Python requests and proxy.

Method 2: Force a specific SSL version with the requests.Session object

To fix the SSLError WRONG_VERSION_NUMBER when using Python requests with proxy, you can force a specific SSL version with the requests.Session object. Here are the steps:

import requests session = requests.Session()
session.mount('https://', requests.adapters.HTTPAdapter( max_retries=3, pool_connections=10, pool_maxsize=10, ssl_version=requests.utils.DEFAULT_SSL_CIPHER_LIST[-1]))
response = session.get('https://www.example.com')
print(response.status_code) print(response.content)

Here is the complete code:

import requests session = requests.Session() session.mount('https://', requests.adapters.HTTPAdapter( max_retries=3, pool_connections=10, pool_maxsize=10, ssl_version=requests.utils.DEFAULT_SSL_CIPHER_LIST[-1])) response = session.get('https://www.example.com') print(response.status_code) print(response.content)

This should fix the SSLError WRONG_VERSION_NUMBER when using Python requests with proxy.

Method 3: Disable SSL verification for the request

To fix the SSLError WRONG_VERSION_NUMBER error when using Python requests with a proxy, you can disable SSL verification for the request. Here are the steps to do this:

session.proxies =  "http": "http://your-proxy-url", "https": "http://your-proxy-url" >
response = session.get("https://your-url.com")

Here is the complete code:

import requests session = requests.Session() session.verify = False session.proxies =  "http": "http://your-proxy-url", "https": "http://your-proxy-url" > response = session.get("https://your-url.com")

This code will disable SSL verification for the request and use the specified proxy.

Method 4: Use a different proxy that supports the desired SSL version

To fix the SSLError WRONG_VERSION_NUMBER error with Python requests and a proxy, you can try using a different proxy that supports the desired SSL version. Here’s an example code snippet to achieve this:

import requests proxy_url = 'http://myproxy.com' proxy_port = '8080' ssl_version = 'TLSv1.2' url = 'https://www.example.com' proxies =  'http': f'proxy_url>:proxy_port>', 'https': f'proxy_url>:proxy_port>', 'ssl': f'ssl_version>' > response = requests.get(url, proxies=proxies) print(response.content)

In this code, we first define the proxy URL and port, as well as the desired SSL version. We then define the URL to make a request to, and create a proxy dictionary with the necessary information.

Finally, we make the request using the requests.get() method, passing in the URL and the proxy dictionary. The response content is then printed to the console.

Note that this is just one possible solution to the SSLError WRONG_VERSION_NUMBER error with Python requests and a proxy. There may be other methods or configurations that work better for your specific use case.

Источник

Python Requests — 407 Proxy Authentication Required

A 407 Proxy Authentication Required response is often returned either when a request being made through a proxy requires authentication or if the credentials being passed are invalid or have been incorrectly escaped. The following are a number of different approaches on how you can fix this issue, as some proxies require different implementations.

Proxy Authentication using Requests’ Session Feature

Python’s requests library lets you easily configure the proxy on a per-request basis using a session object.

import requests from urllib3.util import parse_url, Url from urllib.parse import quote proxy_url_credentials = self.add_creds_to_proxy_url('http://someproxyurl.com:8080/', 'some_user', 'some_password') proxies = 'http': proxy_url_credentials, 'https': proxy_url_credentials> session = requests.session() session.proxies.update(proxies) response = session.get('https://google.com/', verify=False) print(response.text) def add_creds_to_proxy_url(self, url, username, password) url_dict = parse_url(url)._asdict() url_dict['auth'] = username + ':' + quote(password, '') return Url(**url_dict).url 

The above code will configure your proxy url so that the credentials are included and the password has been percent-encoded . This is important because passwords can often contain various symbols that are not permitted in an HTTP request. This often results in errors such as:

requests.exceptions.ProxyError Max retries exceeded with url: Failed to establish a new connection [Errno -5] No address associated with hostname [Errno -3] Temporary failure in name resolution 

Python Articles

See more of my articles on Python:

Источник

Читайте также:  Collectors to set java
Оцените статью