Im pulling daily all the emails from some 8 different postboxes via exchangelib. I was using it the whole week but now the code seems to be throttled by the exchangeserver as the error below gets thrown out while its trying to grab the first email. So I want to learn how to handle it and dont get throttled anymore. I already implemented one retry policy
credentials = Credentials(username='username', password='password')
config = Configuration(retry_policy=FaultTolerance(max_wait=600), credentials=credentials)
for that im using following code:
while True:
try:
for shared_postbox in tqdm(shared_postboxes):
account = Account(shared_postbox, credentials=credentials, config = config, autodiscover=True)
top_folder = account.root
email_folders = [f for f in top_folder.walk() if isinstance(f, Messages)]
for folder in tqdm(email_folders):
#added item_class in filter and removed order by
#for m in folder.all().only('text_body', 'datetime_received', "sender").filter(datetime_received__range=(start_of_month,end_of_month), sender__exists=True):
#when since statement is needed
for m in folder.all().only('text_body', 'datetime_received', "sender").filter(datetime_received__gt=midnight, sender__exists=True):
try:
senderdomain = ExtractingDomain(m.sender.email_address)
senderdomains.append(senderdomain)
except:
print("could not extract domain")
else:
if senderdomain in domains_of_interest:
postboxname = account.identity.primary_smtp_address
body = m.text_body
emails.append(body)
sender.append(senderdomain)
postbox.append(postboxname)
received.append(m.datetime_received)
#else:
# print("nicht in domains of interest")
account.protocol.close()
except RateLimitError as e:
time.sleep(60)
following error code i get:
RateLimitError Traceback (most recent call last)
Input In [4], in <cell line: 77>()
81 account = Account(shared_postbox, credentials=credentials, config = config, autodiscover=True)
---> 82 top_folder = account.root
83 email_folders = [f for f in top_folder.walk() if isinstance(f, Messages)]
File ~\.conda\envs\python383\lib\site-packages\cached_property.py:74, in threaded_cached_property.__get__(self, obj, cls)
72 except KeyError:
73 # if not, do the calculation and release the lock
---> 74 return obj_dict.setdefault(name, self.func(obj))
File ~\.conda\envs\python383\lib\site-packages\exchangelib\account.py:349, in Account.root(self)
347 #threaded_cached_property
348 def root(self):
--> 349 return Root.get_distinguished(account=self)
File ~\.conda\envs\python383\lib\site-packages\exchangelib\folders\roots.py:114, in RootOfHierarchy.get_distinguished(cls, account)
113 try:
--> 114 return cls.resolve(
115 account=account, folder=cls(account=account, name=cls.DISTINGUISHED_FOLDER_ID, is_distinguished=True)
116 )
117 except MISSING_FOLDER_ERRORS:
File ~\.conda\envs\python383\lib\site-packages\exchangelib\folders\base.py:512, in BaseFolder.resolve(cls, account, folder)
509 #classmethod
510 def resolve(cls, account, folder):
511 # Resolve a single folder
--> 512 folders = list(FolderCollection(account=account, folders=[folder]).resolve())
513 if not folders:
File ~\.conda\envs\python383\lib\site-packages\exchangelib\folders\collections.py:335, in FolderCollection.resolve(self)
334 additional_fields = self.get_folder_fields(target_cls=self._get_target_cls())
--> 335 yield from self.__class__(account=self.account, folders=resolveable_folders).get_folders(
336 additional_fields=additional_fields
337 )
File ~\.conda\envs\python383\lib\site-packages\exchangelib\folders\collections.py:403, in FolderCollection.get_folders(self, additional_fields)
399 additional_fields.update(
400 (FieldPath(field=BaseFolder.get_field_by_fieldname(f)) for f in self.REQUIRED_FOLDER_FIELDS)
401 )
--> 403 yield from GetFolder(account=self.account).call(
404 folders=self.folders,
405 additional_fields=additional_fields,
406 shape=ID_ONLY,
407 )
File ~\.conda\envs\python383\lib\site-packages\exchangelib\services\get_folder.py:43, in GetFolder._elems_to_objs(self, elems)
42 def _elems_to_objs(self, elems):
---> 43 for folder, elem in zip(self.folders, elems):
44 if isinstance(elem, Exception):
File ~\.conda\envs\python383\lib\site-packages\exchangelib\services\common.py:246, in EWSService._chunked_get_elements(self, payload_func, items, **kwargs)
245 log.debug("Processing chunk %s containing %s items", i, len(chunk))
--> 246 yield from self._get_elements(payload=payload_func(chunk, **kwargs))
File ~\.conda\envs\python383\lib\site-packages\exchangelib\services\common.py:266, in EWSService._get_elements(self, payload)
263 try:
264 # Create a generator over the response elements so exceptions in response elements are also raised
265 # here and can be handled.
--> 266 yield from self._response_generator(payload=payload)
267 return
File ~\.conda\envs\python383\lib\site-packages\exchangelib\services\common.py:228, in EWSService._response_generator(self, payload)
223 """Send the payload to the server, and return the response.
224
225 :param payload: payload as an XML object
226 :return: the response, as XML objects
227 """
--> 228 response = self._get_response_xml(payload=payload)
229 if self.supports_paging:
File ~\.conda\envs\python383\lib\site-packages\exchangelib\services\common.py:343, in EWSService._get_response_xml(self, payload, **parse_opts)
342 log.debug("Trying API version %s", api_version)
--> 343 r = self._get_response(payload=payload, api_version=api_version)
344 if self.streaming:
345 # Let 'requests' decode raw data automatically
File ~\.conda\envs\python383\lib\site-packages\exchangelib\services\common.py:298, in EWSService._get_response(self, payload, api_version)
297 session = self.protocol.get_session()
--> 298 r, session = post_ratelimited(
299 protocol=self.protocol,
300 session=session,
301 url=self.protocol.service_endpoint,
302 headers=self._extra_headers(session),
303 data=wrap(
304 content=payload,
305 api_version=api_version,
306 account_to_impersonate=self._account_to_impersonate,
307 timezone=self._timezone,
308 ),
309 stream=self.streaming,
310 timeout=self.timeout or self.protocol.TIMEOUT,
311 )
312 self._handle_response_cookies(session)
File ~\.conda\envs\python383\lib\site-packages\exchangelib\util.py:880, in post_ratelimited(protocol, session, url, headers, data, allow_redirects, stream, timeout)
879 total_wait = time.monotonic() - t_start
--> 880 if protocol.retry_policy.may_retry_on_error(response=r, wait=total_wait):
881 r.close() # Release memory
File ~\.conda\envs\python383\lib\site-packages\exchangelib\protocol.py:780, in FaultTolerance.may_retry_on_error(self, response, wait)
778 if wait > self.max_wait:
779 # We lost patience. Session is cleaned up in outer loop
--> 780 raise RateLimitError(
781 "Max timeout reached", url=response.url, status_code=response.status_code, total_wait=wait
782 )
783 if response.status_code == 401:
784 # EWS sometimes throws 401's when it wants us to throttle connections. OK to retry.
RateLimitError: Max timeout reached (gave up after 634.031 seconds. URL https://outlook.office365.com/EWS/Exchange.asmx returned status code 401)
When I looked into it, I saw that exchangelib has a function to handle the throttle policy but I don't know to implement it. could the function
def post_ratelimited(protocol, session, url, headers, data, stream=False, timeout=None)
help me in this case? I found this function in their documentation.
You defined a policy the tells exchangelib to retry up to 600 seconds. The code threw an exception after waiting for more than 600 seconds. That's how it's supposed to work.
If you want the code to retry for a longer period, then increase the max_wait value.
Guide to EWS throttling and how to handle it is here: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-throttling-in-exchange
Related
Trying to load a model from past run in mlflow, in jupyterlab, never finishes. After waiting for hours, interrupting the run throws the below state.
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
Input In [21], in <cell line: 2>()
1 logged_model = 'runs:/7f6932baef144fa69847ba11ef66f8e6/model/'
----> 2 loaded_model = mlflow.tensorflow.load_model(logged_model)
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/tensorflow/__init__.py:397, in load_model(model_uri, dst_path)
360 def load_model(model_uri, dst_path=None):
361 """
362 Load an MLflow model that contains the TensorFlow flavor from the specified path.
363
(...)
395 for _, output_signature in signature_definition.outputs.items()]
396 """
--> 397 local_model_path = _download_artifact_from_uri(artifact_uri=model_uri, output_path=dst_path)
398 flavor_conf = _get_flavor_configuration(local_model_path, FLAVOR_NAME)
399 _add_code_from_conf_to_system_path(local_model_path, flavor_conf)
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/tracking/artifact_utils.py:95, in _download_artifact_from_uri(artifact_uri, output_path)
92 parsed_uri = parsed_uri._replace(path=posixpath.dirname(parsed_uri.path))
93 root_uri = prefix + urllib.parse.urlunparse(parsed_uri)
---> 95 return get_artifact_repository(artifact_uri=root_uri).download_artifacts(
96 artifact_path=artifact_path, dst_path=output_path
97 )
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/store/artifact/runs_artifact_repo.py:125, in RunsArtifactRepository.download_artifacts(self, artifact_path, dst_path)
110 def download_artifacts(self, artifact_path, dst_path=None):
111 """
112 Download an artifact file or directory to a local directory if applicable, and return a
113 local path for it.
(...)
123 :return: Absolute path of the local filesystem location containing the desired artifacts.
124 """
--> 125 return self.repo.download_artifacts(artifact_path, dst_path)
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/store/artifact/artifact_repo.py:242, in ArtifactRepository.download_artifacts(self, artifact_path, dst_path)
240 # Check if the artifacts points to a directory
241 if self._is_directory(artifact_path):
--> 242 dst_local_path, inflight_downloads = async_download_artifact_dir(
243 src_artifact_dir_path=artifact_path, dst_local_dir_path=dst_path
244 )
245 else:
246 inflight_downloads = async_download_artifact(
247 src_artifact_path=artifact_path, dst_local_dir_path=dst_path
248 )
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/store/artifact/artifact_repo.py:208, in ArtifactRepository.download_artifacts.<locals>.async_download_artifact_dir(src_artifact_dir_path, dst_local_dir_path)
206 for file_info in dir_content:
207 if file_info.is_dir:
--> 208 inflight_downloads += async_download_artifact_dir(
209 src_artifact_dir_path=file_info.path,
210 dst_local_dir_path=dst_local_dir_path,
211 )[2]
212 else:
213 inflight_downloads += async_download_artifact(
214 src_artifact_path=file_info.path,
215 dst_local_dir_path=dst_local_dir_path,
216 )
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/store/artifact/artifact_repo.py:199, in ArtifactRepository.download_artifacts.<locals>.async_download_artifact_dir(src_artifact_dir_path, dst_local_dir_path)
195 local_dir = os.path.join(dst_local_dir_path, src_artifact_dir_path)
196 inflight_downloads = []
197 dir_content = [ # prevent infinite loop, sometimes the dir is recursively included
198 file_info
--> 199 for file_info in self.list_artifacts(src_artifact_dir_path)
200 if file_info.path != "." and file_info.path != src_artifact_dir_path
201 ]
202 if not dir_content: # empty dir
203 if not os.path.exists(local_dir):
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/mlflow/store/artifact/sftp_artifact_repo.py:94, in SFTPArtifactRepository.list_artifacts(self, path)
92 artifact_dir = self.path
93 list_dir = posixpath.join(artifact_dir, path) if path else artifact_dir
---> 94 if not self.sftp.isdir(list_dir):
95 return []
96 artifact_files = self.sftp.listdir(list_dir)
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/pysftp/__init__.py:652, in Connection.isdir(self, remotepath)
650 self._sftp_connect()
651 try:
--> 652 result = S_ISDIR(self._sftp.stat(remotepath).st_mode)
653 except IOError: # no such file
654 result = False
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/sftp_client.py:493, in SFTPClient.stat(self, path)
491 path = self._adjust_cwd(path)
492 self._log(DEBUG, "stat({!r})".format(path))
--> 493 t, msg = self._request(CMD_STAT, path)
494 if t != CMD_ATTRS:
495 raise SFTPError("Expected attributes")
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/sftp_client.py:822, in SFTPClient._request(self, t, *arg)
820 def _request(self, t, *arg):
821 num = self._async_request(type(None), t, *arg)
--> 822 return self._read_response(num)
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/sftp_client.py:852, in SFTPClient._read_response(self, waitfor)
850 while True:
851 try:
--> 852 t, data = self._read_packet()
853 except EOFError as e:
854 raise SSHException("Server connection dropped: {}".format(e))
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/sftp.py:201, in BaseSFTP._read_packet(self)
200 def _read_packet(self):
--> 201 x = self._read_all(4)
202 # most sftp servers won't accept packets larger than about 32k, so
203 # anything with the high byte set (> 16MB) is just garbage.
204 if byte_ord(x[0]):
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/sftp.py:185, in BaseSFTP._read_all(self, n)
183 break
184 else:
--> 185 x = self.sock.recv(n)
187 if len(x) == 0:
188 raise EOFError()
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/channel.py:699, in Channel.recv(self, nbytes)
686 """
687 Receive data from the channel. The return value is a string
688 representing the data received. The maximum amount of data to be
(...)
696 if no data is ready before the timeout set by `settimeout`.
697 """
698 try:
--> 699 out = self.in_buffer.read(nbytes, self.timeout)
700 except PipeTimeout:
701 raise socket.timeout()
File ~/.conda/envs/tensorflow/lib/python3.8/site-packages/paramiko/buffered_pipe.py:160, in BufferedPipe.read(self, nbytes, timeout)
158 while (len(self._buffer) == 0) and not self._closed:
159 then = time.time()
--> 160 self._cv.wait(timeout)
161 if timeout is not None:
162 timeout -= time.time() - then
File ~/.conda/envs/tensorflow/lib/python3.8/threading.py:302, in Condition.wait(self, timeout)
300 try: # restore state no matter what (e.g., KeyboardInterrupt)
301 if timeout is None:
--> 302 waiter.acquire()
303 gotit = True
304 else:
KeyboardInterrupt:
The mlflow tracking server is working properly for all the other operations. I am able to log params, metrics and artifacts. But I am not able to load a model or retrive any of the artifacts.
Update:
Looks like a bug as per https://github.com/mlflow/mlflow/issues/5656.
Please upgrade mlflow, their is some issue with version 1.26.0
pip install mlflow==1.27.0
Assuming you are also using above version
While I was scraping Tweets about a certain hashtag using twint, I received the error below. Can anyone explain me why this error occurs and how can I fix the problem? Because the tweets to a certain date were scraped, however, the other tweets before that day could not be scraped because of this error.
Thank you for your help already!
---------------------------------------------------------------------------
ClientPayloadError Traceback (most recent call last)
<ipython-input-8-f28f8e9aab1e> in <module>
----> 1 twint.run.Search(c)
~/.local/lib/python3.8/site-packages/twint/run.py in Search(config, callback)
408 config.Followers = False
409 config.Profile = False
--> 410 run(config, callback)
411 if config.Pandas_au:
412 storage.panda._autoget("tweet")
~/.local/lib/python3.8/site-packages/twint/run.py in run(config, callback)
327 raise
328
--> 329 get_event_loop().run_until_complete(Twint(config).main(callback))
330
331
~/opt/anaconda3/lib/python3.8/asyncio/base_events.py in run_until_complete(self, future)
614 raise RuntimeError('Event loop stopped before Future completed.')
615
--> 616 return future.result()
617
618 def stop(self):
~/.local/lib/python3.8/site-packages/twint/run.py in main(self, callback)
233 task.add_done_callback(callback)
234
--> 235 await task
236
237 async def run(self):
~/.local/lib/python3.8/site-packages/twint/run.py in run(self)
284 elif self.config.TwitterSearch:
285 logme.debug(__name__ + ':Twint:main:twitter-search')
--> 286 await self.tweets()
287 else:
288 logme.debug(__name__ + ':Twint:main:no-more-tweets')
~/.local/lib/python3.8/site-packages/twint/run.py in tweets(self)
215
216 async def tweets(self):
--> 217 await self.Feed()
218 # TODO : need to take care of this later
219 if self.config.Location:
~/.local/lib/python3.8/site-packages/twint/run.py in Feed(self)
60 # this will receive a JSON string, parse it into a `dict` and do the required stuff
61 try:
---> 62 response = await get.RequestUrl(self.config, self.init)
63 except TokenExpiryException as e:
64 logme.debug(__name__ + 'Twint:Feed:' + str(e))
~/.local/lib/python3.8/site-packages/twint/get.py in RequestUrl(config, init)
133 _serialQuery = _url
134
--> 135 response = await Request(_url, params=params, connector=_connector, headers=_headers)
136
137 if config.Debug:
~/.local/lib/python3.8/site-packages/twint/get.py in Request(_url, connector, params, headers)
159 logme.debug(__name__ + ':Request:Connector')
160 async with aiohttp.ClientSession(connector=connector, headers=headers) as session:
--> 161 return await Response(session, _url, params)
162
163
~/.local/lib/python3.8/site-packages/twint/get.py in Response(session, _url, params)
166 with timeout(120):
167 async with session.get(_url, ssl=True, params=params, proxy=httpproxy) as response:
--> 168 resp = await response.text()
169 if response.status == 429: # 429 implies Too many requests i.e. Rate Limit Exceeded
170 raise TokenExpiryException(loads(resp)['errors'][0]['message'])
~/opt/anaconda3/lib/python3.8/site-packages/aiohttp/client_reqrep.py in text(self, encoding, errors)
1074 """Read response payload and decode."""
1075 if self._body is None:
-> 1076 await self.read()
1077
1078 if encoding is None:
~/opt/anaconda3/lib/python3.8/site-packages/aiohttp/client_reqrep.py in read(self)
1030 if self._body is None:
1031 try:
-> 1032 self._body = await self.content.read()
1033 for trace in self._traces:
1034 await trace.send_response_chunk_received(
~/opt/anaconda3/lib/python3.8/site-packages/aiohttp/streams.py in read(self, n)
368 blocks = []
369 while True:
--> 370 block = await self.readany()
371 if not block:
372 break
~/opt/anaconda3/lib/python3.8/site-packages/aiohttp/streams.py in readany(self)
390 # without feeding any data
391 while not self._buffer and not self._eof:
--> 392 await self._wait("readany")
393
394 return self._read_nowait(-1)
~/opt/anaconda3/lib/python3.8/site-packages/aiohttp/streams.py in _wait(self, func_name)
304 if self._timer:
305 with self._timer:
--> 306 await waiter
307 else:
308 await waiter
ClientPayloadError: Response payload is not completed
Not sure if you found a response to your problem but thought I would add this here for anyone looking in the future:
https://github.com/twintproject/twint/issues/1099
Essentially the above link suggests using a try / except block to capture the error and try again, if that works for your code.
I have also found that twint works better using python3.6 - might help!
Good luck :)
I am trying to translate a text to English, and I am using googletrans package version = 3.0.0, Python 3.8.5. But I got this error. I am using a company laptop and I don't have admin rights on it Can anyone help me with this?
This is the code that I used just for a test:
from googletrans import Translator, constants
translator = Translator()
translation = translator.translate("Hola Mundo")
And this is the error thrown:
---------------------------------------------------------------------------
ConnectError Traceback (most recent call last)
<ipython-input-54-93d54b4e73a5> in <module>
1 from googletrans import Translator, constants
2 translator = Translator()
----> 3 translation = translator.translate("Hola Mundo")
~\Anaconda3\lib\site-packages\googletrans\client.py in translate(self, text, dest, src, **kwargs)
180
181 origin = text
--> 182 data = self._translate(text, dest, src, kwargs)
183
184 # this code will be updated when the format is changed.
~\Anaconda3\lib\site-packages\googletrans\client.py in _translate(self, text, dest, src, override)
76
77 def _translate(self, text, dest, src, override):
---> 78 token = self.token_acquirer.do(text)
79 params = utils.build_params(query=text, src=src, dest=dest,
80 token=token, override=override)
~\Anaconda3\lib\site-packages\googletrans\gtoken.py in do(self, text)
192
193 def do(self, text):
--> 194 self._update()
195 tk = self.acquire(text)
196 return tk
~\Anaconda3\lib\site-packages\googletrans\gtoken.py in _update(self)
52 return
53
---> 54 r = self.client.get(self.host)
55
56 raw_tkk = self.RE_TKK.search(r.text)
~\Anaconda3\lib\site-packages\httpx\_client.py in get(self, url, params, headers, cookies, auth, allow_redirects, timeout)
753 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
754 ) -> Response:
--> 755 return self.request(
756 "GET",
757 url,
~\Anaconda3\lib\site-packages\httpx\_client.py in request(self, method, url, data, files, json, params, headers, cookies, auth, allow_redirects, timeout)
598 cookies=cookies,
599 )
--> 600 return self.send(
601 request, auth=auth, allow_redirects=allow_redirects, timeout=timeout,
602 )
~\Anaconda3\lib\site-packages\httpx\_client.py in send(self, request, stream, auth, allow_redirects, timeout)
618 auth = self.build_auth(request, auth)
619
--> 620 response = self.send_handling_redirects(
621 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,
622 )
~\Anaconda3\lib\site-packages\httpx\_client.py in send_handling_redirects(self, request, auth, timeout, allow_redirects, history)
645 raise TooManyRedirects()
646
--> 647 response = self.send_handling_auth(
648 request, auth=auth, timeout=timeout, history=history
649 )
~\Anaconda3\lib\site-packages\httpx\_client.py in send_handling_auth(self, request, history, auth, timeout)
682 request = next(auth_flow)
683 while True:
--> 684 response = self.send_single_request(request, timeout)
685 if auth.requires_response_body:
686 response.read()
~\Anaconda3\lib\site-packages\httpx\_client.py in send_single_request(self, request, timeout)
712 headers,
713 stream,
--> 714 ) = transport.request(
715 request.method.encode(),
716 request.url.raw,
~\Anaconda3\lib\site-packages\httpcore\_sync\connection_pool.py in request(self, method, url, headers, stream, timeout)
150
151 try:
--> 152 response = connection.request(
153 method, url, headers=headers, stream=stream, timeout=timeout
154 )
~\Anaconda3\lib\site-packages\httpcore\_sync\connection.py in request(self, method, url, headers, stream, timeout)
63 "open_socket origin=%r timeout=%r", self.origin, timeout
64 )
---> 65 self.socket = self._open_socket(timeout)
66 self._create_connection(self.socket)
67 elif self.state in (ConnectionState.READY, ConnectionState.IDLE):
~\Anaconda3\lib\site-packages\httpcore\_sync\connection.py in _open_socket(self, timeout)
83 ssl_context = self.ssl_context if scheme == b"https" else None
84 try:
---> 85 return self.backend.open_tcp_stream(
86 hostname, port, ssl_context, timeout
87 )
~\Anaconda3\lib\site-packages\httpcore\_backends\sync.py in open_tcp_stream(self, hostname, port, ssl_context, timeout)
137 sock, server_hostname=hostname.decode("ascii")
138 )
--> 139 return SyncSocketStream(sock=sock)
140
141 def create_lock(self) -> SyncLock:
~\Anaconda3\lib\contextlib.py in __exit__(self, type, value, traceback)
129 value = type()
130 try:
--> 131 self.gen.throw(type, value, traceback)
132 except StopIteration as exc:
133 # Suppress StopIteration *unless* it's the same exception that
~\Anaconda3\lib\site-packages\httpcore\_exceptions.py in map_exceptions(map)
10 for from_exc, to_exc in map.items():
11 if isinstance(exc, from_exc):
---> 12 raise to_exc(exc) from None
13 raise
14
ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123) ```
I used the same code on another laptop and I got another Error. I want to translate a text which I don't know in which language is(different files in different languages) to English and I am not sure this is the best package to use for this so even some other packages that you are using and you are sure that works would be great. Thanks
I won't recommend you to use this library.As it is stated in documentation for this library:
Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don’t care about stability.)
Don't you consider using official Google Python client for Google Translate API?https://cloud.google.com/translate/docs/reference/libraries/v2/python
I'm trying to run a following code to recognize an audio file. The code is just a compilation from different official examples. But it doesn't work.
import os
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from google.oauth2 import service_account
import io
def transcribe_file(speech_file):
client = speech.SpeechClient(credentials=credentials)
with io.open(speech_file, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='ru-RU')
response = client.long_running_recognize(config, audio)
for result in response.results:
print(u'Transcript: {}'.format(result.alternatives[0].transcript))
audio_folder_path = 'data_wav'
all_audios = os.listdir(audio_folder_path)
file_name = os.path.join(audio_folder_path, all_audios[0])
credentials = service_account.Credentials.from_service_account_file("google_aut.json")
transcribe_file(file_name)
I use Anaconda 4.7.12 for Python 3.7 under Windows 10, google-cloud-speech v 1.2.0, google-auth v 1.6.3
The error I get every time is
_Rendezvous Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\grpc_helpers.py
in error_remapped_callable(*args, **kwargs)
56 try:
---> 57 return callable_(*args, **kwargs)
58 except grpc.RpcError as exc:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\grpc_channel.py
in call(self, request, timeout, metadata, credentials,
wait_for_ready, compression)
564 wait_for_ready, compression)
--> 565 return _end_unary_response_blocking(state, call, False, None)
566
~\AppData\Local\Continuum\anaconda3\lib\site-packages\grpc_channel.py
in _end_unary_response_blocking(state, call, with_call, deadline)
466 else:
--> 467 raise _Rendezvous(state, None, None, deadline)
468
_Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "failed to connect to all addresses"
debug_error_string = "{"created":"#1569838382.864000000","description":"Failed to pick
subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3818,"referenced_errors":[{"created":"#1569838382.863000000","description":"failed
to connect to all
addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":395,"grpc_status":14}]}"
>
The above exception was the direct cause of the following exception:
ServiceUnavailable Traceback (most recent call
last) in
----> 1 transcribe_file(file_name)
in transcribe_file(speech_file)
20
21 # [START speech_python_migration_sync_response]
---> 22 response = client.long_running_recognize(config, audio)
23 # [END speech_python_migration_sync_request]
24 # Each result is for a consecutive portion of the audio. Iterate through
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\speech_v1\gapic\speech_client.py
in long_running_recognize(self, config, audio, retry, timeout,
metadata)
339 )
340 operation = self._inner_api_calls["long_running_recognize"](
--> 341 request, retry=retry, timeout=timeout, metadata=metadata
342 )
343 return google.api_core.operation.from_gapic(
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\gapic_v1\method.py
in call(self, *args, **kwargs)
141 kwargs["metadata"] = metadata
142
--> 143 return wrapped_func(*args, **kwargs)
144
145
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\retry.py
in retry_wrapped_func(*args, **kwargs)
271 sleep_generator,
272 self._deadline,
--> 273 on_error=on_error,
274 )
275
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\retry.py
in retry_target(target, predicate, sleep_generator, deadline,
on_error)
180 for sleep in sleep_generator:
181 try:
--> 182 return target()
183
184 # pylint: disable=broad-except
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\timeout.py
in func_with_timeout(*args, **kwargs)
212 """Wrapped function that adds timeout."""
213 kwargs["timeout"] = next(timeouts)
--> 214 return func(*args, **kwargs)
215
216 return func_with_timeout
~\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\grpc_helpers.py
in error_remapped_callable(*args, **kwargs)
57 return callable_(*args, **kwargs)
58 except grpc.RpcError as exc:
---> 59 six.raise_from(exceptions.from_grpc_error(exc), exc)
60
61 return error_remapped_callable
~\AppData\Local\Continuum\anaconda3\lib\site-packages\six.py in
raise_from(value, from_value)
ServiceUnavailable: 503 failed to connect to all addresses
How can I fix it?
This could be failing due to the credentials. Let's try few things:
Ensure that your service account key is correct, you should have something like this:
from google.oauth2 import service_account
credentials = service_account.Credentials. from_service_account_file('service_account_key.json')
speech = speech.SpeechClient(credentials=credentials)
OR
speech = speech_v1.SpeechClient(credentials=credentials)
Use a Scope:
credentials = service_account.Credentials.from_service_account_file(
credentials_json,
scopes=['https://www.googleapis.com/auth/cloud-platform'])
More info here.
In this thread was solve by using a single instance of a session client object for multiple requests.
This could be either a network issue as Dustin said. More info here 503 Service Unavailable
Please let us know if you manage to solve this error.
I am still new to python and can't figure out how to handle this error and what to do with it to avoid it even after trying to understand the different methods of the Requests module and reading out there.
Here's the simple request I use where line loops through a text file with the different URL I'm trying to access and d a list of dictionary containing the many URLs I'm using as proxies.
import requests
import collections
# [...]
d = collections.deque(proxies)
with requests.session() as r:
d.rotate(-1)
page = r.get(line.rstrip(), proxies=d[0])
It works perfectly until one of the proxies from the list timeout for some reason and force the script to raise this error:
ProxyError Traceback (most recent call last)
C:\Python27\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195 else:
196 filename = fname
--> 197 exec compile(scripttext, filename, 'exec') in glob, loc
198 else:
199 def execfile(fname, *where):
C:\Users\Christopher Fargere\desktop\python\quick_scraper.py in <module>()
72 with requests.session() as r:
73 d.rotate(-1)
---> 74 page = r.get(line.rstrip(), proxies=d[0])
75 print d[0]
76 print page.status_code
C:\Python27\lib\site-packages\requests\sessions.pyc in get(self, url, **kwargs)
393
394 kwargs.setdefault('allow_redirects', True)
--> 395 return self.request('GET', url, **kwargs)
396
397 def options(self, url, **kwargs):
C:\Python27\lib\site-packages\requests\sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert)
381 'allow_redirects': allow_redirects,
382 }
--> 383 resp = self.send(prep, **send_kwargs)
384
385 return resp
C:\Python27\lib\site-packages\requests\sessions.pyc in send(self, request, **kwargs)
484 start = datetime.utcnow()
485 # Send the request
--> 486 r = adapter.send(request, **kwargs)
487 # Total elapsed time of the request (approximately)
488 r.elapsed = datetime.utcnow() - start
C:\Python27\lib\site-packages\requests\adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
379
380 except _ProxyError as e:
--> 381 raise ProxyError(e)
382
383 except (_SSLError, _HTTPError) as e:
ProxyError: Cannot connect to proxy. Socket error: [Errno 11001] getaddrinfo failed.
I would love to implement an IF condition when an error is raised that pops out the proxy out of the d list and retry the same URL. I'm sure its very simple but can't understand how the errors are raised in Python. :(
To catch an exception, use exception handling; catch the ProxyError thrown:
from requests.exceptions import ProxyError
with requests.session() as r:
page = None
for _ in range(len(d)):
d.rotate(-1)
try:
page = r.get(line.rstrip(), proxies=d[0])
except ProxyError:
# ignore proxy exception, move to next proxy
pass
else:
# success, break loop
break
if page is None:
# none of the proxies worked
raise ProxyError
This tries, at most, all your proxies in d, one by one. If none of them worked, we raise the ProxyError again, as you probably want to know that all your proxies failed at that time.