Python Requests Post - HTTPSConnectionPool Max retries exceeded - python

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.

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!

ElectrumX server jsonRPC authentication

Unable to connect to electrum server
Error:
HTTPConnectionPool(host='electrum.eff.ro', port=50002): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
Connection is done in python:
electrum wallet jsonRPC authentication
import requests
import json
def main():
url = "http://electrum.eff.ro:50002"
payload = json.dumps(
{
"id": 0,
"method": "server.version",
"params": ["1.9.5", "0.6"]
}
)
headers = {'content-type': "application/json", 'cache-control': "no-cache"}
try:
response = requests.request("POST", url, data=payload, headers=headers, auth=(rpc_user, rpc_password))
return json.loads(response.text)
except requests.exceptions.RequestException as e:
print(e)
except:
print('No response from Wallet, check Bitcoin is running on this machine')
rpc_user = 'foo'
rpc_password = 'bar'
if __name__ == "__main__":
answer = main()
Such an error is constantly displayed.
UPD:
Server:
url = "http://fortress.qtornado.com:443"
error:
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
if connect HTTPS:
url = "https://fortress.qtornado.com:443"
error:
HTTPSConnectionPool(host='fortress.qtornado.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1051)')))
How to connect using ssl certificate?
An example of a solution to a problem:
https://github.com/cluelessperson/grappler
Bitcoin Testnet
import grappler
from base58 import b58decode_check
from binascii import hexlify
from hashlib import sha256
import codecs
a = grappler.ElectrumXConnector(
# host="fortress.qtornado.com", # bitcoin mainnet
host='tn.not.fyi',
port=55002,
# port=443,
ssl=True,
timeout=5
)
OP_DUP = b'76'
OP_HASH160 = b'a9'
BYTES_TO_PUSH = b'14'
OP_EQUALVERIFY = b'88'
OP_CHECKSIG = b'ac'
DATA_TO_PUSH = lambda address: hexlify(b58decode_check(address)[1:])
sig_script_raw = lambda address: b''.join((OP_DUP, OP_HASH160, BYTES_TO_PUSH, DATA_TO_PUSH(address), OP_EQUALVERIFY, OP_CHECKSIG))
script_hash = lambda address: sha256(codecs.decode(sig_script_raw(address), 'hex_codec')).digest()[::-1].hex()
a.send("server.version")
a.send("server.banner")
a.send('blockchain.scripthash.get_balance', script_hash('mksHkTDsauAP1L79rLZUQA3u36J3ntLtJx'))
a.send('blockchain.scripthash.get_mempool', script_hash('mksHkTDsauAP1L79rLZUQA3u36J3ntLtJx'))
a.send('blockchain.scripthash.subscribe', script_hash('mksHkTDsauAP1L79rLZUQA3u36J3ntLtJx'))

Python requests Max retries exceeded with dynatrace SAAS url

Trying to get User session data from Dynatrace SAAS using python3 script. The get Request is giving me error Max retries exceeded with url: Failed to establish a new connection:
I'm not sure if I'm passing the token or proxy wrong.
import requests
from requests.exceptions import ConnectionError
try:
response = requests.get('https://jbu0001.live.dynatrace.com/api/v1/userSessionQueryLanguage/table?SELECT%20*%20from%20usersession&explain=true',
headers={'Authorization': 'Api-Token XXXXXXXX'}, proxies={'http': 'http://proxy.com:PORT'}, verify=False)
if response.status_code == 200:
response.text
except ConnectionError as e:
print(e)
ERR0R
HTTPSConnectionPool(host='jbu0000.live.dynatrace.com', port=443): Max retries exceeded with url: /api/v1/userSessionQueryLanguage/table?SELECT%20*%20from%20usersession&explain=true (Caused by NewConnectionError('<urllib3.conne
ction.VerifiedHTTPSConnection object at 0x00000160212D5B38>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
BUT
I'm able to get data using CURL with proxy from same machine.
curl -X GET "https://jbu00XXX.live.dynatrace.com/api/v1/userSessionQueryLanguage/table?query=select%20*%20from%20usersession&explain=true" -H "accept: application/json" -H "Authorization: Api-Token XXXXXXXXX" --proxy http://proxy.com:PORT
Thanks in advance!
Are you sure that proxy is under http not https? If so, try sending api-token as:
headers={'Authorization': r'Api-Token XXXXXXXX'}
or
headers={'Authorization': r'XXXXXXXX'}
prefix r assures that you are sending raw string data.

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

Categories