Python3 exchangelib connect error: _ssl.c:749? - python

Python3 exchangelib connect error: _ssl.c:749?
I am trying to connect to my company's Exchange server with exchangelib.
I want to see all the mails I have received on MS Exchange / OWA.
When I run the code below, I get:
File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
chunked=chunked)
File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "C:\Program Files\Python36\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
conn.connect()
File "C:\Program Files\Python36\lib\site-packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
File "C:\Program Files\Python36\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Program Files\Python36\lib\ssl.py", line 401, in wrap_socket
_context=self, _session=session)
File "C:\Program Files\Python36\lib\ssl.py", line 808, in __init__
self.do_handshake()
File "C:\Program Files\Python36\lib\ssl.py", line 1061, in do_handshake
self._sslobj.do_handshake()
File "C:\Program Files\Python36\lib\ssl.py", line 683, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
from exchangelib import DELEGATE, Account, Credentials,NTLM,Configuration,Version,Build
creds = Credentials(username = 'inter.com\\login',password='password')
config = Configuration(service_endpoint='https://alpha.com/EWS/Exchange.asmx',credentials=creds,auth_type=NTLM)
account = Account(primary_smtp_address='login#alpha.com',config=config,autodiscover=False,access_type=DELEGATE,verify_ssl=False)
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
Can you please let me know how to use certificates with exchangelib?
How to fix it?
Thanks for your help!

Have you tried disabling ssl verification in your config ?
config = Configuration(service_endpoint ='...', credentials=credentials, auth_type=NTLM, verify_ssl=False)
The sources seem to talk about a parameter like this.

Related

OpenSearch Python Client Connection SSL Error

I tried connecting to opensearch using python client but getting SSL Error.
OpenSearch Version: 2.0.1
Python Version:3.6
public IP of the opensearch host: 10.83.5.67
Here is the python code for connecting the client to opensearch:
from opensearchpy import *
host = '10.83.5.67'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
ca_certs_path = '/etc/opensearch/cert.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_compress = True, # enables gzip compression for request bodies
http_auth = auth,
use_ssl = True,
verify_certs = True,
ssl_assert_hostname = False,
ssl_show_warn = False,
ca_certs = ca_certs_path
)
The certificates used in opensearch.yml file are:
plugins.security.ssl.transport.pemcert_filepath: /etc/opensearch/cert.pem
plugins.security.ssl.transport.pemkey_filepath: /etc/opensearch/privkey.pem
plugins.security.ssl.transport.pemtrustedcas_filepath: /etc/opensearch/fullchain.pem
plugins.security.ssl.http.pemcert_filepath: /etc/opensearch/cert.pem
plugins.security.ssl.http.pemkey_filepath: /etc/opensearch/privkey.pem
plugins.security.ssl.http.pemtrustedcas_filepath: /etc/opensearch/fullchain.pem
This is the error I am getting:
[root#ip-10-92-3-240 pyscripts]# python3 ospy4.py
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
conn.connect()
File "/usr/lib/python3.6/site-packages/urllib3/connection.py", line 358, in connect
ssl_context=context)
File "/usr/lib/python3.6/site-packages/urllib3/util/ssl_.py", line 367, in ssl_wrap_socket
return context.wrap_socket(sock)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 776, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/opensearchpy/connection/http_urllib3.py", line 250, in perform_request
method, url, body, retries=Retry(False), headers=request_headers, **kw
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3.6/site-packages/urllib3/util/retry.py", line 344, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3.6/site-packages/urllib3/packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "/usr/lib/python3.6/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
conn.connect()
File "/usr/lib/python3.6/site-packages/urllib3/connection.py", line 358, in connect
ssl_context=context)
File "/usr/lib/python3.6/site-packages/urllib3/util/ssl_.py", line 367, in ssl_wrap_socket
return context.wrap_socket(sock)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 776, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ospy4.py", line 31, in <module>
response = client.indices.create(index_name, body=index_body)
File "/usr/local/lib/python3.6/site-packages/opensearchpy/client/utils.py", line 177, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/usr/local/lib/python3.6/site-packages/opensearchpy/client/indices.py", line 125, in create
"PUT", _make_path(index), params=params, headers=headers, body=body
File "/usr/local/lib/python3.6/site-packages/opensearchpy/transport.py", line 405, in perform_request
raise e
File "/usr/local/lib/python3.6/site-packages/opensearchpy/transport.py", line 375, in perform_request
timeout=timeout,
File "/usr/local/lib/python3.6/site-packages/opensearchpy/connection/http_urllib3.py", line 261, in perform_request
raise SSLError("N/A", str(e), e)
opensearchpy.exceptions.SSLError: ConnectionError([SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)) caused by: SSLError([SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897))
[root#ip-10-92-3-240 pyscripts]#
Not sure what's wrong I am using the same certificate in opensearch-dashboard.yml file, but can't connect using python client.
Any suggestion would be really helpful.
Thanks

ConnectionResetError while running through cron

I'm sending a simple 'post' request through the 'requests' module. It works fine when execute it directly through the linux terminal. However, when I set it up through the crontab, the log is indicating and error.
If I execute the below through the terminal, it works fine.
'/usr/bin/python3.6 /location/sa/tb/uc/md/se/sea.py'
If I setup the crontab as follows, I get an error.
/usr/bin/python3.6 /location/sa/tb/uc/md/se/sea.py >> ~/Test_log.log 2>&1
Below is the error message:
File
"/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py",
line 600, in urlopen
chunked=chunked) File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py",
line 343, in _make_request
self._validate_conn(conn) File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py",
line 839, in validate_conn
conn.connect() File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line
344, in connect
ssl_context=context) File "/usr/local/lib/python3.6/site-packages/urllib3/util/ssl.py", line
345, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname) File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session) File "/usr/lib64/python3.6/ssl.py", line 776, in init
self.do_handshake() File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake() File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake() ConnectionResetError: [Errno 104] Connection reset by peer
What did I try?
Tried adding absolute path inside the script.
Added a proxy to the headers, but no go.
Any help would be appreciated.
Some servers don't start re-listen immediately (check_mk flag), while calling multiple requests from a single connection. One of the reason is to avoid DoS attacks and service availability to all users.
Since your crontab made your script to call the same API multiple times using a single connection, I'd suggest you to add a void timer before making a request, e.g. add time.sleep(0.01) just before calling the API.

Trouble for downloading mp3 file with Python

I'm new to coding and have been stuck at one point trying to download an mp3 file.
I can access the file but not download it; it produces a variety of problems while executing this code's downloading mp3 file portion. Please, could you assist me?
I'm using the MAC platform with PyCharm.
CODE STARTS FROM HERE
import os
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
import urllib.request
Sample Audio URL
url = 'https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3'
path = os.path.abspath(os.getcwd())
creating a webdriver
options = webdriver.ChromeOptions()
driver: WebDriver = webdriver.Chrome(options=options, executable_path='/Users/sam/Downloads/chromedriver')
driver.get(url)
get the mp3 audio file
src = driver.find_element_by_xpath("/html/body/video/source").get_attribute("src")
print("[Audio File Path:] ",src)
downloading mp3 file
urllib.request.urlretrieve(src, path+"//sample.mp3")
#---------- END OF CODE --------------
ERRORS
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1346, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1253, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1299, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1248, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1008, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 948, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1422, in connect
self.sock = self._context.wrap_socket(self.sock,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sam/PycharmProjects/BB/practice.py", line 35, in <module>
urllib.request.urlretrieve(src, path+"//sample.mp3")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 239, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1389, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1349, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)>
Process finished with exit code 1
The issue you're facing is due to the missing certificates of the python library causing issues with the urllib. Please refer to the below url in order to resolve the certificate issue :
brew installation of Python 3.6.1: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

python2.7: [SSL: UNKNOWN_PROTOCOL] unknown protocol

I'm trying to install ROS from source.
When I execute the command of installation, I get such an error:
Traceback (most recent call last):
File "/home/zyh/ros_catkin_ws/install_isolated/share/ros/core/rosbuild/bin/download_checkmd5.py", line 126, in <module>
sys.exit(main())
File "/home/zyh/ros_catkin_ws/install_isolated/share/ros/core/rosbuild/bin/download_checkmd5.py", line 73, in main
urllib.urlretrieve('https://github.com/assimp/assimp/archive/v3.1.1.zip', dest)
File "/usr/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/usr/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/usr/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 443, in open_https
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 1038, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 882, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 844, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1263, in connect
server_hostname=server_hostname)
File "/usr/lib/python2.7/ssl.py", line 363, in wrap_socket
_context=self)
File "/usr/lib/python2.7/ssl.py", line 611, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 840, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:661)
/home/zyh/ros_catkin_ws/install_isolated/share/mk/download_unpack_build.mk:37: recipe for target 'build/assimp-3.1.1/unpacked' failed
make[3]: *** [build/assimp-3.1.1/unpacked] Error 1
I don't know how to solve this issue. Maybe it's because I worked behind a proxy? If so, how to make urllib.urlretrieve work behind the proxy?
Add proxy settings to your global environment to see if it fixes the problem.
sudo gedit /etc/environment
Then add these two lines
http_proxy=http://your_proxy.com:443
https_proxy=https://your_proxy.com:443

Unable to access wikipedia API due to SSL-certificate error

import requests
spiders = requests.get('https://en.wikipedia.org/w/api.php')
print(spiders)
With above code I am getting following error:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 554, in urlopen
self._prepare_proxy(conn)
File "C:\Python\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 750, in _prepare_proxy
conn.connect()
File "C:\Python\lib\site-packages\requests\packages\urllib3\connection.py", line 252, in connect
ssl_version=resolved_ssl_version)
File "C:\Python\lib\site-packages\requests\packages\urllib3\util\ssl_.py", line 305, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Python\lib\ssl.py", line 376, in wrap_socket
_context=self)
File "C:\Python\lib\ssl.py", line 747, in __init__
self.do_handshake()
File "C:\Python\lib\ssl.py", line 983, in do_handshake
self._sslobj.do_handshake()
File "C:\Python\lib\ssl.py", line 628, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
Using a certificate I still get an error:
import requests
spiders = requests.get('https://en.wikipedia.org/w/api.php',cert = 'c:\python\lib\site-packages\certifi\cacert.pem',verify = True)
print(spiders)
Traceback (most recent call last):
File "C:/Python/Files/3.py", line 3, in <module>
spiders = requests.get('https://en.wikipedia.org/w/api.php',cert = 'c:\python\lib\site-packages\certifi\cacert.pem',verify = True)
File "C:\Python\lib\site-packages\requests\api.py", line 67, in get
return request('get', url, params=params, **kwargs)
File "C:\Python\lib\site-packages\requests\api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python\lib\site-packages\requests\sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python\lib\site-packages\requests\sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "C:\Python\lib\site-packages\requests\adapters.py", line 447, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2824)
Even when I have followed some tutorial verbatim I have encountered this issue when the instructor would have access w/o issue. Any ideas?
u can ignore SSL like this:
import requests
spiders = requests.get('https://en.wikipedia.org/w/api.php',verify = False)
print(spiders)
other solution:
from urllib.request import Request, urlopen
rq = Request('https://en.wikipedia.org/w/api.php')
response = urlopen(rq).read()

Categories