Why HTTPSConnectionPool doesn't work when PoolManager does? - python

I have tested a 'POST' request with both PoolManager and HTTPSConnectionPool. The first one works, the other throw me a :
urllib3.exceptions.MaxRetryError:
HTTPSConnectionPool(host='https://some.url.com', port=443):
Max retries exceeded with url: /some-api (Caused by <class 'socket.gaierror'>:
[Errno -2] Name or service not known)
Here's my code for PoolManager:
import urllib3
HOST = 'https://some.url.com'
PORT = 443
PATH = '/some-api'
xml_request = '<some xml tree/>'
manager = urllib3.PoolManager()
res = manager.request('POST', HOST+PATH, {'req':xml_request})
and for HTTPSConnectonPool:
manager = urllib3.HTTPSConnectionPool(HOST, port=PORT)
res = manager.request('POST', PATH, {'req':xml_request})

https://some.url.com is not a hostname or IP address, it's a URL. So you're feeding the wrong information to HTTPSConnectionPool.
Furthermore, PoolManager and HTTPSConnectionPool are not at the same abstraction level. PoolManager manages ConnectionPool instances for you.

Related

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!

Python Requests Post - HTTPSConnectionPool Max retries exceeded

Hi I have a simple code hosted on my Flask server to POST to my API
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
try:
url = "apiv2.xxxxxxxx.com/testpost"
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
response = session.post(url)
except Exception as e:
return str(e)
However, it caught an exception and the error message is as follows:
HTTPSConnectionPool(host='apiv2.drobaio.com', port=443): Max retries exceeded with url: /authuser/$2b$10$wLkkKBi9g/gAAAAABgqr74HzvCs09eVY4O4YvkvXlf6lNe0uk9QG3Qp4g0lKzmgVFwLdQ50K1hptKKyguR2RrZJNJJnOlUs0LnyaqgefZ04g==/gAAAAABgqr74tJyvGCcFEpw3fdcKALlZUv-RHi3u0hdJJtgUDCE0Ga3hqoilUPL6vu3iTDzt2NDfX8UgfrcgQQFr3IFnfSDq_g== (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))
Does anyone know what's wrong? I don't think there's an issue with my code as I tried executing this on my own PC and it works.

Get HTML content from onion links using the request library (Python 3)

I am trying to get the html code of onion websites using the requests library (or urllib.request). I tried diverse methods but none of them seemed to work properly.
At first, I simply tried to connect to a proxy using the requests library and get the HTML code of the facebook deep web:
import requests
session = requests.session()
session.proxie = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
r = requests.get('https://facebookcorewwwi.onion/')
print(r.text)
However, when I do this, the connection to the proxy doesn't work (my IP stays the same with or without the proxy).
I get the following error:
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='facebookcorewwwi.onion', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x109e8b198>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
After doing some research, I saw someone who tried to do a similar thing and the solution was to connect to the proxy before importing the requests/urllib.request library.
So I tried connecting using the libraries socks and socket:
import socks
import socket
def create_connection(address, timeout=None, source_address=None):
sock = socks.socksocket()
sock.connect(address)
return sock
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
# patch the socket module
socket.socket = socks.socksocket
socket.create_connection = create_connection
import urllib.request
with urllib.request.urlopen('https://facebookcorewwwi.onion/') as response:
html = response.read()
print(html)
When I do this, my connection the the proxy gets refused:
urllib.error.URLError: <urlopen error Error connecting to SOCKS5 proxy 127.0.0.1:9050: [Errno 61] Connection refused>
I tried to use the requests library instead like follow (simply replace it from the line that says import urllib.request)
import requests
r = requests.get('https://facebookcorewwwi.onion/')
print(r.text)
But here I get this error:
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='facebookcorewwwi.onion', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x10d93ee80>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
It seem that no matter what I do my connection to a proxy gets refused. Does anyone have an alternative solution or a way to fix this?

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'))

Error 10054 when connecting google service

I tried to connect to google api and got this error
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: /speech-api/v1/recognize?xjerr=1&client=chromium&lang=ru-RU (Caused by <class 'socket.error'>: [Errno 10054] An existing connection was forcibly closed by the remote host)
this is my code, i used library urllib3,urllib2,requests but it did not help
import requests
recording = 'C:/Users/sborovskiy/PycharmProjects/VoiceManager/com/work/voiceManager/output.flac'
url = 'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=ru-RU'
headers = {'Content-Type' : 'audio/x-flac; rate=16000', 'User-Agent': 'Netscape 6.0'}
files = {'output.flac' : open(recording, 'rb')}
response = requests.post(url = url, data = files, headers = headers)
internet not give a specific answer

Categories