Https Connection failure via Proxy Server in Python - python-3.x

I am trying to read and send emails (O365) using OAuth2. This code works fine on my local machine, but
when I am trying to deploy this in a server where there is a proxy to access the internet, I am getting the below error.
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /2d11ad74-bbf6-403f-32c7-6ff0e039a923/oauth2/v2.0/token (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002507067C348>: 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'))
Below is my code I tried:
import settings
from exchangelib import Credentials, Account, DELEGATE, BASIC, \
Configuration, NTLM, Message, Mailbox, FileAttachment, HTMLBody, FaultTolerance, IMPERSONATION
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
from exchangelib import OAuth2LegacyCredentials
credentials = OAuth2LegacyCredentials(
client_id= settings.clientId, client_secret= settings.clientSecret,
tenant_id=settings.tenentId,
username=settings.eUserName, password=settings.ePassword
)
config = Configuration(server = 'smtp.office365.com', credentials=credentials)
account = Account(settings.eUserName, config=config, access_type=DELEGATE)
m = Message(
account=account,
subject='Any Subject OAth2 Authentication',
body=HTMLBody('OAth2 Authentication'),
to_recipients=map(lambda x: Mailbox(email_address=x), ['myEmail#gmail.com'])
)
for item in account.inbox.all()[:10]:
print (item.sender.email_address)
m.send()
print('Email is being sent...')

You probably need to add setup proxy configuration. Here's how to do that in exchangelib:
https://ecederstrand.github.io/exchangelib/#proxies-and-custom-tls-validation

Related

Azure Custom Vision SDK - ClientRequestError

I am trying out an object detection project with the Custom Vision client library. But I get ClientRequestError. Below is my code:-
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateEntry
from msrest.authentication import ApiKeyCredentials
cv_endpoint = "https://westeurope.api.cognitive.microsoft.com"
training_key = "*******************"
training_images = "LegoSimpsons/TrainingImages"
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(credentials=credentials,endpoint=cv_endpoint)
for domain in trainer.get_domains():
print(domain.id, "\t", domain.name)
I get the below error:
ClientRequestError: Error occurred in request., ProxyError:
HTTPSConnectionPool(host='southcentralus.api.cognitive.microsoft.com',
port=443): Max retries exceeded with url:
/customvision/v3.4-preview/training/domains (Caused by
ProxyError('Cannot connect to proxy.',
NewConnectionError('<urllib3.connection.HTTPSConnection object at
0x000001CA81115588>: Failed to establish a new connection: [WinError
10061] No connection could be made because the target machine actively
refused it')))
Seems there is something wrong with your cv_endpoint, If you have created a Custom Vision Training service in your subscription:
you can find the key and endpoint here:
This is my test result:

Query Crate Database by SQLAlchemy Dialect: No more Servers available

I'd like to give it a try with crate. I first tried it by loading some twitter data and query from admin UI successfully. However, when I tried to make a query with SQLAlchemy, I've got the following error.
OperationalError: (crate.client.exceptions.ConnectionError)
No more Servers available, exception from last server: HTTPConnectionPool(host='localhost', port=4200):
Max retries exceeded with url:
/_sql (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f51485b2c18>:
Failed to establish a new connection: [Errno 111] Connection refused'))
[SQL: SELECT * FROM tweets LIMIT 100; ]
(Background on this error at: http://sqlalche.me/e/e3q8)
This is the code I ran that caused the error.
from crate import client
import sqlalchemy as sa
import pandas as pd
engine = sa.create_engine('crate://localhost:4200')
query = (
"SELECT * "
"FROM tweets "
"LIMIT 100; "
)
df = pd.read_sql(query, engine)
Any idea how to fix it (the settings are all default)?
Update:
Crate is running on host system while Jupyter is running in a container on the same machine.
I can access Crate via "http://localhost:4200" on the host machine but can't do "http://xxx.xxx.xxx.xxx:4200" from other machine in the same natwork.
I tried both:
engine = sa.create_engine('crate://10.7.116.222:4200')
engine = sa.create_engine('crate://')
which caused the same error:
Removed server http://127.0.0.1:4200 from active pool
Removed server http://127.0.0.1:4200 from active pool
OperationalError: (crate.client.exceptions.ConnectionError)
No more Servers available, exception from last server:
HTTPConnectionPool(host='127.0.0.1', port=4200):
Max retries exceeded with url:
/_sql (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f5147faaa58>:
Failed to establish a new connection: [Errno 111] Connection refused'))
[SQL: SELECT * FROM tweets LIMIT 10; ]
(Background on this error at: http://sqlalche.me/e/e3q8)
I haven't tried it but according to the documentation 'localhost:4200' is assumed so you could connect using just engine = sa.create_engine('crate://')

Connecting to Sharepoint Via Python - UrlLib Error

The following code (with variables changed) is what I am trying to use in order to contact a sharepoint server (and ultimately download files from). But I appear to be getting an error related to urllib, for which I can't work out the reason for.
from sharepoint import SharePointSite, basic_auth_opener
server_url = "http://sharepoint.domain.com/"
opener = basic_auth_opener(server_url, "domain/username", "password")
site = SharePointSite(server_url, opener)
sp_list = site.lists
for sp_list in site.lists:
print(sp_list.id, sp_list.meta['Title'])
The error is as follows
urllib.error.URLError: <urlopen error [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
Could you try with below code
import requests from requests_ntlm import HttpNtlmAuth
requests.get("http://sharepoint.domain.com", auth=HttpNtlmAuth('DOMAIN\\USERNAME','PASSWORD'))

python-gitlab api SSL bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],

I want to list all the issues in a github repository.
Python3 code:
import gitlab
gl = gitlab.Gitlab('https://git.myinternalsite.com/project', private_token='XXXXXXXXXXXXXXX', api_version=4)
issues = gl.issues.list()
This generates the following error:
SSLError: HTTPSConnectionPool(host='git.zonetrading.com', port=443): Max retries exceeded with url: /cloudquant/user-issues/api/v4/issues (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",),))
Any ideas on how to correct the error?
The problem seems to be a faulty configured webserver.
The TLS certificate is only certified for the domain www.parkingcrew.comand not for git.zonetrading.com this leads to the certificate verify failederror.
To fix this you have to request a new certificate which includes the target domain, in this case git.zonetrading.com.
To confirm this is the only error, you can turn off the certificate verification in the client using the ssl_verify parameter.
gl = gitlab.Gitlab('https://git.myinternalsite.com/project', private_token='XXXXXXXXXXXXXXX', api_version=4, ssl_verify=False)
Thanks for the feedback everyone.
Here is how I ended up solving this:
Additional info... our enterprise github doesn't have a proper certificate, so warnings were generated.
##########################################
# Dump one ticket to screen so you can see
# all the fields available.
##########################################
import pycurl
from io import BytesIO
buffer = BytesIO()
c = pycurl.Curl()
#
# using a private token from git. Had to register my token
# as a function within the github user profile settings
#
private_token = 'private_token=XXXXXMyPrivateTokenXXXXX'
#
# projects/3 - I had to dump the project list to find the id number
# of the project that I wanted to get all the issues for
#
GitAPIurl = 'https://git.****MyDomain***.com/api/v4/projects/3/issues?{}'.format(private_token)
c.setopt(c.URL,GitAPIurl)
# turn off SSL verification because we don't have a proper SSL Certification
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = []
body = buffer
# Body is a byte string.
# We have to know the encoding in order to print it to a text file
# such as standard output.
foo = {}
dictionary = json.loads(buffer.getvalue())
foo = dictionary[0]
print(foo)

Connection refused when using zeep SSL

I am trying to access a SOAP server using zeep. My server uses SSL with a custom certificate, and connection to that server works, with my cert, or ignoring it:
python -mzeep "https://<server-ip>/servicemanager/1?wsdl" --no-verify
I get a long list of Prefixes, Global elements, Global types, Bindings and Service. The latter one says:
Service: ServiceManager
Port: servicemanager_1 (Soap11Binding: {http://soap.client.<snipped>.at}servicemanager_1Binding)
Operations:
getServices() -> return: ns0:service[]
So, from what I can say by now, I can create a client object and call it's service named getServices().
from zeep import CachingClient as Client
from zeep.wsse.signature import Signature
from zeep.transports import Transport
from requests import Session, Request
session = Session()
session.verify = False
transport = Transport(session=session)
c = Client('https://<server-ip>/servicemanager/1?wsdl', transport=transport)
c.service.getServices()
But that leads to an error in urllib3 (~/.virtualenvs/soap/lib/python3.5/site-packages/urllib3/util/connection.py):
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
[...]
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='localhost',
port=443): Max retries exceeded with url: /servicemanager/1 (Caused by
NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object
at 0x7f4e2a6f7d30>: Failed to establish a new connection: [Errno 111]
Connection refused',))
It does not matter if I ignore the SSL verification, or provide a CA_BUNDLE. both are accepted, the client is created, but I can't call the getServices() method.
What did I forget here? I don't think this is a zeep problem, as the underlying urllib3 throws the exception. But I tried for hours and searched the internet for a solution, without success.
Apart of the XML I get from the endpoint is:
<service name="ServiceManager">
<port name="servicemanager_1" binding="tns:servicemanager_1Binding">
<soap:address location="http://localhost/servicemanager/1"/>
</port>
</service>
And I don't know why it returns a "localhost" there - is zeep using that for its call? Then I would understand why permanent errors occur.
Any hints?
To change the endpoint address I use it this way:
client.service._binding_options['address'] = 'https://mynewaddress.com/service.wsdl'
As always, after days of searching, in the moment I ask at Stackoverflow, the answer came up through other channels.
If anyone has the same problems, here is the solution. My server provides me with the WSDL file, like said above:
<service name="ServiceManager">
<port name="servicemanager_1" binding="tns:servicemanager_1Binding">
<soap:address location="http://localhost/servicemanager/1"/>
</port>
</service>
And there it stands: localhost. Zeep (IMHO correctly) uses that service endpoint to communicate then with the server.
What I did for testing: I SSH-tunnelled the ports 80/443 to localhost, so zeep thought it talked to localhost.
And Shazaam, it worked.
So my server was the culprit - too bad I can't change that, as I have no control over it.
But now a workaround is possible.

Resources