Google Translate SSL Error with Threading in Python - python

I translating a pretty big text with google translate and to speed up the process in am calling the split up requests in threads. However, from time to time, the threads throw a SSL handshake error. What is going on?
here is the trace
File "/Users/[PROJECT ROOT]/source/graba/models.py", line 1558, in google_translate_list_strings
service = build('translate', 'v2', developerKey='[MY DEV KEY]')
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/oauth2client/util.py", line 120, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/apiclient/discovery.py", line 193, in build
resp, content = http.request(requested_url)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1588, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1336, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1272, in _conn_request
conn.connect()
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1014, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 80, in _ssl_wrap_socket
cert_reqs=cert_reqs, ca_certs=ca_certs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 372, in wrap_socket
ciphers=ciphers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 134, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 296, in do_handshake
self._sslobj.do_handshake()
SSLError: [Errno 8] _ssl.c:503: EOF occurred in violation of protocol

Related

Web Scraping in HTML to using py-script

<body class="white-vertion black-bg">
<!-- Start Loader -->
<p>
<py-script>
import ssl
from urllib.request import urlopen
from bs4 import BeautifulSoup
context = ssl._create_unverified_context()
result = urlopen("https://blog.naver.com/PostList.naver?blogId=woong3164&categoryNo=0&from=postList", context=context)
bsObj = BeautifulSoup(result.read(), "html.parser")
</py-script>
</p>
I used py-script in HTML to do web scraping. However, this error occurred.
'JsException(PythonError: Traceback (most recent call last):
File "/lib/python3.10/urllib/request.py", line 1348, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/lib/python3.10/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/lib/python3.10/http/client.py", line 1328, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/lib/python3.10/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/lib/python3.10/http/client.py", line 1037, in _send_output
self.send(msg)
File "/lib/python3.10/http/client.py", line 975, in send
self.connect()
File "/lib/python3.10/http/client.py", line 1447, in connect
super().connect()
File "/lib/python3.10/http/client.py", line 941, in connect
self.sock = self._create_connection(
File "/lib/python3.10/socket.py", line 845, in create_connection
raise err
File "/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
BlockingIOError: [Errno 26] Operation in progress
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code
.run(globals, locals)
File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run
coroutine = eval(self.code, globals, locals)
File "", line 6, in
File "/lib/python3.10/urllib/request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "/lib/python3.10/urllib/request.py", line 519, in open
response = self._open(req, data)
File "/lib/python3.10/urllib/request.py", line 536, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/lib/python3.10/urllib/request.py", line 496, in _call_chain
result = func(*args)
File "/lib/python3.10/urllib/request.py", line 1391, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/lib/python3.10/urllib/request.py", line 1351, in do_open
raise URLError(err) urllib.error.URLError: )'
I think this error was caused by ssl.
How can I solve this error?
Your problem is caused by using unsupported Python packages. The package urllib uses APIs (TCP Sockets) that do not exist in the browser. This is not a limitation of PyScript, no browser application can use socket-based APIs.
The solution is to use supported APIs such as fetch or pyfetch.

Slack unable to connect to send an message

I am using python slack module to send a message to a slack channel and i have also installed all the required modules (slack, slackClient, openssl) as well but I am facing with SLL violation. I am not sure if it is related to proxy or not. Any help would be appreciated.
code:
import slack
import ssl
SLACK_API_TOKEN = "xoxb-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client = slack.WebClient(token=SLACK_API_TOKEN)
response = client.chat_postMessage(
channel='#channelName',
text="testing")
if (response["ok"]):
print("Notification sent to Slack")
Error:
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.9/3.9.10/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 "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1285, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1331, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1280, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1040, in _send_output
self.send(msg)
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 980, in send
self.connect()
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1454, in connect
self.sock = self._context.wrap_socket(self.sock,
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/sai/Downloads/Gen3/POC_TEST.py", line 19, in <module>
response = client.chat_postMessage(
File "/usr/local/lib/python3.9/site-packages/slack_sdk/web/legacy_client.py", line 2091, in chat_postMessage
return self.api_call("chat.postMessage", json=kwargs)
File "/usr/local/lib/python3.9/site-packages/slack_sdk/web/legacy_base_client.py", line 167, in api_call
return self._sync_send(api_url=api_url, req_args=req_args)
File "/usr/local/lib/python3.9/site-packages/slack_sdk/web/legacy_base_client.py", line 258, in _sync_send
return self._urllib_api_call(
File "/usr/local/lib/python3.9/site-packages/slack_sdk/web/legacy_base_client.py", line 370, in _urllib_api_call
response = self._perform_urllib_http_request(url=url, args=request_args)
File "/usr/local/lib/python3.9/site-packages/slack_sdk/web/legacy_base_client.py", line 535, in _perform_urllib_http_request
raise err
File "/usr/local/lib/python3.9/site-packages/slack_sdk/web/legacy_base_client.py", line 496, in _perform_urllib_http_request
resp = opener.open(req, timeout=self.timeout) # skipcq: BAN-B310
File "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
response = self._open(req, data)
File "/usr/local/Cellar/python#3.9/3.9.10/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 "/usr/local/Cellar/python#3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/usr/local/Cellar/python#3.9/3.9.10/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 "/usr/local/Cellar/python#3.9/3.9.10/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 EOF occurred in violation of protocol (_ssl.c:1129)>
Thanks

python httplib2 connection refused error10061

How to how to login and establish the connection with the token ..?
i am getting response as --> {"detail":"Authentication credentials were not provided."}
I tried with requests python library.
Also i am getting the error "target machine actively refused as given below"
I tried httplib2 , If any one know Kindly help on this..... :-(
import httplib2
http = httplib2.Http()
resp = http.request("http://www.something.com", "HEAD")[0]
Traceback (most recent call last): File "", line 1, in
resp = http.request("http://www.something.com", "HEAD")[0] File "C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 1050, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File
"C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 854, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers) File
"C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 823, in _conn_request
conn.request(method, request_uri, body, headers) File "C:\Python27\lib\httplib.py", line 1057, in request
self._send_request(method, url, body, headers) File "C:\Python27\lib\httplib.py", line 1097, in _send_request
self.endheaders(body) File "C:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body) File "C:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg) File "C:\Python27\lib\httplib.py", line 859, in send
self.connect() File "C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 715, in connect
raise socket.error, msg error: [Errno 10061] No connection could be made because the target machine actively refused it
http = httplib2.Http()
resp = http.request("http://www.something.com", "GET")[0]
Traceback (most recent call last): File "", line 1, in
resp = http.request("http://www.something.com", "GET")[0] File "C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 1050, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File
"C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 854, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers) File
"C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 823, in _conn_request
conn.request(method, request_uri, body, headers) File "C:\Python27\lib\httplib.py", line 1057, in request
self._send_request(method, url, body, headers) File "C:\Python27\lib\httplib.py", line 1097, in _send_request
self.endheaders(body) File "C:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body) File "C:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg) File "C:\Python27\lib\httplib.py", line 859, in send
self.connect() File "C:\Users\vijayakumar.v\Downloads\python-rest-client-0.2.tar\python-rest-client\python-rest-client\httplib2__init__.py",
line 715, in connect
raise socket.error, msg error: [Errno 10061] No connection could be made because the target machine actively refused it

Connecting to Google Content API via Python

I am kind of new in Google APIs and I am trying to connect to Google Content API via Python. I am using a service account for that.
My script is quite easy and straightforward:
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
credentials = ServiceAccountCredentials.from_p12_keyfile(
'xxxxx.iam.gserviceaccount.com',
'/Users/Downloads/pkey.txt',
scopes='https://www.googleapis.com/auth/content')
http = credentials.authorize(httplib2.Http())
service = build('https://www.googleapis.com/content', 'v2')
In the authorization process I get this error:
Traceback (most recent call last):
File "/Users/PycharmProjects/authorization.py", line 22, in <module>
service = build('https://www.googleapis.com/content', 'v2')
File "build/bdist.macosx-10.11-intel/egg/oauth2client/_helpers.py", line 133, in positional_wrapper
File "build/bdist.macosx-10.11-intel/egg/googleapiclient/discovery.py", line 228, in build
File "build/bdist.macosx-10.11-intel/egg/googleapiclient/discovery.py", line 275, in _retrieve_discovery_doc
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1659, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1399, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1319, in _conn_request
conn.connect()
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1069, in connect
self.ssl_version, self.host)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 97, in _ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 352, in wrap_socket
_context=self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 579, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 816, in do_handshake
match_hostname(self.getpeercert(), self.server_hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 271, in match_hostname
% (hostname, ', '.join(map(repr, dnsnames))))
ssl.CertificateError: hostname 'https%3a%2f%2fwww.googleapis.com%2fcontent.googleapis.com' doesn't match either of '*.googleapis.com', '*.clients6.google.com', '*.cloudendpointsapis.com', 'cloudendpointsapis.com', 'googleapis.com'
Process finished with exit code 1
Does it have to do with my authorisation? Or I am doing something wrong?
Thanks a lot for your help!
You want to provide build() with the api_name, not a URL.
service = build('content', 'v2')
Source: https://developers.google.com/api-client-library/python/apis/

SSL Error with Python Google Drive API

i wrote a simple code, basically the same as the one on https://developers.google.com/drive/quickstart-python and every time after the first attempt i've the same error :
# Traceback (most recent call last):
# File "C:/plug-ins/googleDrive.py", line 35, in <lambda>
# self.authAction = self.menu.addAction( 'Authentication', lambda: self.runAuthentication() )
# File "C:/plug-ins/googleDrive.py", line 46, in runAuthentication
# credentials = self.flow.step2_exchange( unicode(text) )
# File "D:/Python\oauth2client\util.py", line 120, in positional_wrapper
# return wrapped(*args, **kwargs)
# File "D:/Python\oauth2client\client.py", line 1131, in step2_exchange
# headers=headers)
# File "D:/Python\httplib2\__init__.py", line 1597, in request
# (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
# File "D:/Python\httplib2\__init__.py", line 1345, in _request
# (response, content) = self._conn_request(conn, request_uri, method, body, headers)
# File "D:/Python\httplib2\__init__.py", line 1281, in _conn_request
# conn.connect()
# File "D:/Python\httplib2\__init__.py", line 1013, in connect
# self.disable_ssl_certificate_validation, self.ca_certs)
# File "D:/Python\httplib2\__init__.py", line 80, in _ssl_wrap_socket
# cert_reqs=cert_reqs, ca_certs=ca_certs)
# File "C:\python26\ssl.py", line 350, in wrap_socket
# File "C:\python26\ssl.py", line 118, in __init__
# File "C:\python26\ssl.py", line 293, in do_handshake
# ssl.SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol
I run this under Python 2.6.4, httplib2 v0.7.7 and google-api-python-client v1.0b9. If someone have an idea...I will be very grateful.
That version of the google-api-python-client (v1.0b9) is beta 9 of the client library, you should upgrade to the release version, which is simply v1.0.
As for the "EOF occurred in violation of protocol", I have only seen that occur on a flaky network connection, is that the case here?

Categories