Python Request module, getting SSLError even with verify=False - python

When I tried to get the web page with Python Requests module for the first time on Elementary OS, I faced with SSLError. There is simple solution:
Python 3, trying:
import requests
page = requests.get('https://api.github.com/events')
Getting SSLError:
/usr/local/bin/python3 /home/led/PycharmProjects/urllib_p/urllib_p.py
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 376, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 549, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 251, in _get_conn
return conn or self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 764, in _new_conn
raise SSLError("Can't connect to HTTPS URL because the SSL "
requests.packages.urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/led/PycharmProjects/urllib_p/urllib_p.py", line 5, in <module>
page = requests.get('https://api.github.com/events')
File "/usr/lib/python3/dist-packages/requests/api.py", line 67, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 447, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
verify=False make no sense (same Error)

Go to Python_directory/Modules (Python-3.6.4/Modules in my case).
Open Setup file with text editor and search for SSL. You will find this:
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
#_ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto
Uncomment last four lines.
In terminal: go to Python_directory, then:
sudo ./configure
sudo make
sudo make install

I fixed this with those commands:
sudo cp -f /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 /usr/local/lib/
sudo cp -f /usr/lib/x86_64-linux-gnu/libssl.so.1.1 /usr/local/lib/
sudo ln -sfn /usr/lib/x86_64-linux-gnu/libcrypto.a /usr/local/lib/
sudo ln -sfn /usr/lib/x86_64-linux-gnu/libssl.a /usr/local/lib/
sudo ln -sfn /usr/lib/x86_64-linux-gnu/libssl.so.1.1 /usr/local/lib/

Related

Request Library and Docker Container: [Errno 111] Connection refused

I wrote a basic flask app like this:
import flask
from flask import request, jsonify,render_template
app = flask.Flask(__name__)
app.config["DEBUG"] = False
#app.route('/')
def home():
a=dict()
a["araba"]="tyt"
a["araba2"]="rnt"
return a
if __name__ == "__main__":
app.run(host='0.0.0.0')
It's working with docker container localhost:5003. Also its basic request code is here:
import requests as req
resp = req.get("http://localhost:5003/")
print(resp.text)
When I use this request code with PyCharm it can request and gets a response. But when I want to use it with docker container I am getting this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 96, in create_connection
raise err
File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 86, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 239, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/usr/local/lib/python3.8/http/client.py", line 1256, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/lib/python3.8/http/client.py", line 1302, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.8/http/client.py", line 1251, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.8/http/client.py", line 1011, in _send_output
self.send(msg)
File "/usr/local/lib/python3.8/http/client.py", line 951, in send
self.connect()
File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 205, in connect
conn = self._new_conn()
File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f1cb4cebac0>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/local/lib/python3.8/site-packages/urllib3/util/retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=5003): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1cb4cebac0>: Failed to establish a new connection: [Errno 111] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./request.py", line 3, in <module>
resp = req.get("http://localhost:5003/")
File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5003): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1cb4cebac0>: Failed to establish a new connection: [Errno 111] Connection refused'))
What am I doing wrong?
How to fix
In your Dockerfile, ensure that you are running the Flask app by setting the FLASK_APP environment variable to the name of your app file ​(request.py in your case, default is app.py) and overriding the default port that Flask listens on:
ENV FLASK_APP=request.py
ENV FLASK_RUN_PORT=5003
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
Build your Docker container:
$ docker build -t request-example:latest .
Run your Docker image as a container, symmetrically exposing 5003 as the port for simplicity:
$ docker run -p 5003:5003 request-example:latest
Test that the Flask app is now accessible on your local machine's localhost:
$ curl http://localhost:5003/
Or:
import requests as req
resp = req.get("http://localhost:5003/")
print(resp.text)
Suggestions
Create a requirements.txt that includes all your third-party libraries:
flask
requests
jsonify
Rewrite your Dockerfile to install whatever is in requirements.txt, rather than just the requests library:
FROM python:3.8
WORKDIR /request
COPY requirements.txt requirements.txt
COPY request.py request.py
RUN pip install -r requirements.txt
ENV FLASK_APP=request.py
ENV FLASK_RUN_PORT=5003
CMD [ "python", "-m" , "flask", "run", "--host=0.0.0.0"]

ERROR:ddtrace.internal.writer:failed to send traces to Datadog Agent at http://localhost:8126: OSError: [Errno 99] Cannot assign requested address

I want to set up the datadog agent to monitor my python application running inside a docker container. I have created the docker image tagged as datadog_app:v_1.
Below is my docker file:
FROM ubuntu:18.04
WORKDIR /app
RUN apt-get update --no-install-recommends
RUN apt-get install -y build-essential python3.6 python3.6-dev python3-pip python3-setuptools --no-install-recommends
RUN pip3 install Cython
RUN pip3 install ddtrace
RUN apt-get install -y curl
ADD ./datadog_app.py /app/handler.py
RUN DD_AGENT_MAJOR_VERSION=7 DD_API_KEY=my_api_key DD_SITE="datadoghq.com" bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script.sh)"
CMD ddtrace-run python3 handler.py
Below is the code for the datadog_app.py file:
from ddtrace import tracer
import time
#tracer.wrap(service="addition")
def handler():
time.sleep(2)
print(1+2)
handler()
Now, I am trying to run a docker container from the "datadog_app:v_1: image in interactive and making sure to expose the port 8126 way by using the following command:
docker run -it -p 8126:8126 datadog_app:v_1
I am getting the following error:
ERROR:ddtrace.internal.writer:failed to send traces to Datadog Agent at http://localhost:8126
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tenacity/__init__.py", line 407, in __call__
result = fn(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/ddtrace/internal/writer.py", line 356, in _send_payload
response = self._put(payload, headers)
File "/usr/local/lib/python3.6/dist-packages/ddtrace/internal/writer.py", line 332, in _put
conn.request("PUT", self._endpoint, data, headers)
File "/usr/lib/python3.6/http/client.py", line 1281, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1327, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1276, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1042, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 980, in send
self.connect()
File "/usr/lib/python3.6/http/client.py", line 952, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/ddtrace/internal/writer.py", line 458, in flush_queue
self._retry_upload(self._send_payload, encoded, n_traces)
File "/usr/local/lib/python3.6/dist-packages/tenacity/__init__.py", line 404, in __call__
do = self.iter(retry_state=retry_state)
File "/usr/local/lib/python3.6/dist-packages/tenacity/__init__.py", line 361, in iter
raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x7f32383f0a58 state=finished raised OSError>]
I have also tried running the following command:
docker run -it --expose=8126 datadog_app:v_1
But I am getting the same error.
My goal is to run the datadog-agent inside the same container that contains my python application. Can someone explain what is wrong with my code or my run commands?

Urllib3 socks5 proxy error: 'socks' has no attribute 'create_connection'

I need Robobrowser to use Tor, easiest way i thought would be like this: Robobrowser with Sessions.
But i encountered a strange problem with requests, or more specific urllib3:
AttributeError: module 'socks' has no attribute 'create_connection'
As documented in Urllib3 Documentation create_connection is an attribute from socks.
Urllib3 version: 1.19.1
Requests version: 2.12.4
PySocks version: 1.6.5
import requests
session = requests.Session()
session.proxies = {'http':'socks5://127.0.0.1:9050'}
response = session.get('http://www.icanhazip.com', timeout=2)
print(response.text)
edit:
Stacktrace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 501, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 423, in send
timeout=timeout
File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
chunked=chunked)
File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py", line 361, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py", line 163, in connect
conn = self._new_conn()
File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/socks.py", line 79, in _new_conn
conn = socks.create_connection(
AttributeError: module 'socks' has no attribute 'create_connection'
It is because your python instance can't find the required socks libraries.
The libraries in /usr/lib/python* are installed via sudo apt-get install python3-requests etc.
However apt-get can't install the required pysocks libraries that the urllib3 uses. sudo apt-get install python3-socks won't work.
What will work is if you install it via sudo -H pip3 install pysocks urllib3[socks] requests[socks]
This will install it under here sudo ls -lrtah /usr/local/lib/python*/dist-packages/
Make sure you terminal session has /usr/lib:/usr/local/lib in the system path. If your python instance still can't find your socks library, then make sure both paths are part of your ~/.bashrc PYTHON_PATH export.

Error downloading a file using python package requests with https

I'm a begginer in Python and have been struggling with downloading a file using the package requests. My code works for an url with http, but not for https. The code is the following:
import requests
url = 'https://firms.modaps.eosdis.nasa.gov/active_fire/shapes/zips/Central_America_24h.zip'
print "downloading with requests"
r = requests.get(url)
with open("Central_America_24h.zip", "wb") as code:
code.write(r.content)
I get the following errors:
Warning (from warnings module):
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\packages\urllib3\util\ssl_.py", line 90
InsecurePlatformWarning
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
Traceback (most recent call last):
File "C:\Users\lmixco\Downloads\descarga.py", line 8, in <module>
r = requests.get(url)
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\api.py", line 68, in get
return request('get', url, **kwargs)
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\sessions.py", line 594, in send
history = [resp for resp in gen] if allow_redirects else []
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\sessions.py", line 196, in resolve_redirects
**adapter_kwargs
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\ArcGIS10.1\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
SSLError: [Errno 8] _ssl.c:503: EOF occurred in violation of protocol
I have been looking for a solution, but have not found anything. Any help will be appreciated. Thanks in advance.
you need to install a package , use this
pip install requests[security]
for debain / ubuntu , install this before installing above package
apt-get install python-dev libffi-dev libssl-dev

Proxy Error in nova docker installation

When I install nova-docker from github repositories it runs setup.py; from that script some packages are downloaded and installed but after that it raises a ProxyError during the installation.
Downloading unittest2-1.0.1-py2.py3-none-any.whl (95kB): 95kB downloaded
Downloading/unpacking linecache2 (from traceback2->testtools>=0.9.22->fixtures>=0.3.14->oslo.concurrency>=0.1.0)
Cleaning up...
Exception:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python2.7/site-packages/pip/commands/install.py", line 298, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/lib/python2.7/site-packages/pip/req.py", line 1202, in prepare_files
do_download,
File "/usr/lib/python2.7/site-packages/pip/req.py", line 1380, in unpack_url
self.session,
File "/usr/lib/python2.7/site-packages/pip/download.py", line 546, in unpack_http_url
resp = session.get(target_url, stream=True)
File "/usr/lib/python2.7/site-packages/pip/_vendor/requests/sessions.py", line 468, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python2.7/site-packages/pip/download.py", line 237, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/pip/_vendor/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/site-packages/pip/_vendor/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/site-packages/pip/_vendor/requests/adapters.py", line 378, in send
raise ProxyError(e)
ProxyError: ('Cannot connect to proxy.', gaierror(-2, 'Name or service not known'))
Storing debug log for failure in /root/.pip/pip.log
error: /bin/python -m pip.__init__ install 'pbr>=0.5.21,<1.0' 'Babel>=1.3' 'six>=1.7.0' 'oslo.serialization>=1.0.0 # Apache-2.0' 'oslo.utils>=1.0.0 # Apache-2.0' 'oslo.config>=1.4.0 # Apache-2.0' 'oslo.concurrency>=0.1.0 # Apache-2.0' 'docker-py>=0.5.1' returned 2
put proxy setting in bash file of centos.
export http_proxy="http://PROXY=USERNAME=HERE:PROXY=PASSWORD=HERE#URL:PORT"
export https_proxy="https://PROXY=USERNAME=HERE:PROXY=PASSWORD=HERE#URL:PORT"
export ftp_proxy="ftp://PROXY=USERNAME=HERE:PROXY=PASSWORD=HERE#URL:PORT"

Categories

Resources