unable to connect HTTPS url in python - python

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
requests.get('url', auth=HTTPBasicAuth('XXXX','XXXX'))
I am getting Below error
(Caused by ProxyError('Cannot connect to proxy.', error(111, 'Connection refused')
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='XXXX', port=443): Max retries exceeded with url

According to the error you use a HTTPBasicAuth instead Proxy(maybe that corporate proxy?).
Try to setup proxy credentials before the request.
$ export http_proxy='http://myproxy.example.com:1234'
$ python request.py # Using http://myproxy.example.com:1234 as a proxy
Please let me know if you use a corporate proxy.

Related

How to disable SSL verification for OAuth client of Authlib Python library?

I'm using Python 3.9.12 on Windows 10. My goal is to connect to KeyCloak server through browser to fetch access token. I'm using Authlib 0.15.5 to connect to the server to fetch the authentication URL. Below is the code.
from authlib.integrations.flask_client import OAuth
oauth_client = OAuth()
oauth_client.register(
name=_configuration.oauht2_provider,
client_id=_configuration.oauth2_client_id,
client_secret=_configuration.oauth2_client_secret,
authorize_url=_configuration.oauht2_authorize_url,
authorize_params=_configuration.oauht2_authorize_params,
refresh_token_url=_configuration.oauht2_refresh_token_url,
refresh_token_params=_configuration.oauht2_refresh_token_param,
access_token_url=_configuration.oauht2_access_token_url,
access_token_params=_configuration.oauht2_access_token_params,
client_kwargs={"scope": _configuration.oauht2_scope},
server_metadata_url=_configuration.oauht2_open_id_url)
oauth_client.init_app(app=_app)
_oauth_client = oauth_client.create_client(_configuration.oauht2_provider)
redirect_url = _oauth_client.create_authorization_url(_configuration.oauth2_client_redirect_url, verify=False)['url']
The create_authorization_url is throwing this error
HTTPSConnectionPool(host='keycloak-xxxx-xxxxxxx-xxx.xx.xxxx-xxxxx-xxx.xx.xx.xx.x', port=443): Max retries exceeded with url: /auth/realms/WXYZ/.well-known/uma2-configuration (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)')))
How can I disable SSL certification verification in the above code? Thank you.
I tried adding verify=False argument to the create_authorization_url, however, it did not work.
redirect_url = _oauth_client.create_authorization_url(_configuration.oauth2_client_redirect_url, verify=False)['url']

Proxy authentication with python requests

I am using the following code. I have removed the actual 'username' and 'password' and 'proxy_address' objects for privacy reasons.
auth = HTTPProxyAuth('username', 'password')
r = requests.get(hyperlink, proxies={'http': proxy_address, 'https': proxy_address},
auth=auth, timeout=5)
I get the following error:
HTTPSConnectionPool(host='bafybeihpjhkeuiq3k6nqa3fkgeigeri7iebtrsuyuey5y6vy36n345xmbi.ipfs.dweb.link', port=443): Max retries exceeded with url: /45 (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')))
What should I do to resolve this issue? I have tried looking into the documentation for python Requests but I haven't been able to find a solution for my implementation. Thanks!

Get "requests.exceptions.SSLError" in sending requests to the dropbox api using flask application

I am trying to make requests to the dropbox api in my flask application. I am getting this error:
requests.exceptions.SSLError
The detail of error is as below:
Detailed Error message -
requests.exceptions.SSLError: >HTTPSConnectionPool(host='api.dropboxapi.com', port=443): Max retries >exceeded with url: /2/files/list_folder (Caused by >SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] >certificate verify failed: unable to get local issuer certificate >(_ssl.c:1129)')))
I understood SSl certificate is missing but how to establish one.
P.S. I am not using the requests python package
client = dropbox.Dropbox(dropbox_access_token)
print("[SUCCESS] dropbox account linked")
Error while executing this statement:
client.files_upload(open(computer_path, "rb").read(), dropbox_path)<-----
print("[UPLOADED] {}".format(computer_path))

Requests giving errors while using HTTP proxies

So, I was sending a request using the requests library in Python 3.9.1. The problem is, when I tried to use an HTTP proxy it gave me this error:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002B08D6BC9A0>: Failed to establish a new connection:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond')))
This my code, I would appreciate any help.:
import requests
for proxy in open('proxies.txt','r').readlines():
proxies = {
'http': f'http://{proxy}',
'https': f'http://{proxy}'
}
e = requests.get('https://google.com/robots.txt',proxies=proxies)
open('uwu.txt','a').write(e.text)
print(e.text)
I am pretty sure it is not problem with my proxies as they are really good private proxies with 100 gigs of bandwidth. (from zenum.io).

Failed to establish a new connection error using Python requests Errno -2 Name or service unknown

I am trying to make a request to an API with Python. I am able to make the request with curl without issue but I have something wrong with my Python request.
Why does this code,
import requests
from requests.auth import HTTPBasicAuth
emailadd = 'user123#example.com'
domain = 'example.com'
spantoken = 'omitted'
def checkUserLicensed(useremail):
url = ('https://api.spannigbackup.com/v1/users/' + useremail)
print(url)
response = requests.get(url, auth=(domain,spantoken))
print(response)
checkUserLicensed(emailadd)
Return this error
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.spannigbackup.com', port=443): Max retries exceeded with url: /v1/users/user123#example.com
(Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f73ca323748>: Failed to establish a new connection: [Errno -2] Name or service not known'))

Categories