Sagemaker Endpoint BrokenPipeError at DeepAR Prediction - python

I've created an SageMaker Endpoint from a trained DeepAR-Model using following code:
job_name = estimator.latest_training_job.job_name
endpoint_name = sagemaker_session.endpoint_from_job(
job_name=job_name,
initial_instance_count=1,
instance_type="ml.m4.xlarge",
image_uri=image_uri,
role=role
)
Now I want to test my model using a test.json-Dataset (66.2MB).
I've created that file according to various tutorials/sample-notebooks (same as train.json, but with prediction-length-less values.
For that, I've written the following code:
class DeepARPredictor(sagemaker.predictor.Predictor):
def set_prediction_parameters(self, freq, prediction_length):
self.freq = freq
self.prediction_length = prediction_length
def predict(self, ts, num_samples=100, quantiles=["0.1", "0.5", "0.9"]):
prediction_times = [x.index[-1] + pd.Timedelta(1, unit=self.freq) for x in ts]
req = self.__encode_request(ts, num_samples, quantiles)
res = super(DeepARPredictor, self).predict(req, initial_args={"ContentType": "application/json"})
return self.__decode_response(res, prediction_times)
def __encode_request(self, ts, num_samples, quantiles):
instances = [{"start": str(ts[k].index[0]), "target": list(ts[k])} for k in range(len(ts))]
configuration = {
"num_samples": num_samples,
"output_types": ["quantiles"],
"quantiles": quantiles,
}
http_request_data = {"instances": instances, "configuration": configuration}
return json.dumps(http_request_data).encode( "utf-8")
def __decode_response(self, response, prediction_times):
response_data = json.loads(response.decode("utf-8"))
list_of_df = []
for k in range(len(prediction_times)):
prediction_index = pd.date_range(
start=prediction_times[k], freq=self.freq, periods=self.prediction_length
)
list_of_df.append(
pd.DataFrame(data=response_data["predictions"][k]["quantiles"], index=prediction_index)
)
return list_of_df
But after running the following block:
predictor = DeepARPredictor(endpoint_name=endpoint_name, sagemaker_session=sagemaker_session)
predictor.set_prediction_parameters(freq, prediction_length)
list_of_df = predictor.predict(time_series_training)
I've getting a BrokenPipeError:
---------------------------------------------------------------------------
BrokenPipeError Traceback (most recent call last)
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
676 headers=headers,
--> 677 chunked=chunked,
678 )
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
391 else:
--> 392 conn.request(method, url, **httplib_request_kw)
393
~/anaconda3/envs/python3/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1261 """Send a complete request to the server."""
-> 1262 self._send_request(method, url, body, headers, encode_chunked)
1263
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/awsrequest.py in _send_request(self, method, url, body, headers, *args, **kwargs)
92 rval = super(AWSConnection, self)._send_request(
---> 93 method, url, body, headers, *args, **kwargs)
94 self._expect_header_set = False
~/anaconda3/envs/python3/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1307 body = _encode(body, 'body')
-> 1308 self.endheaders(body, encode_chunked=encode_chunked)
1309
~/anaconda3/envs/python3/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1256 raise CannotSendHeader()
-> 1257 self._send_output(message_body, encode_chunked=encode_chunked)
1258
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/awsrequest.py in _send_output(self, message_body, *args, **kwargs)
119 message_body = None
--> 120 self.send(msg)
121 if self._expect_header_set:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/awsrequest.py in send(self, str)
203 return
--> 204 return super(AWSConnection, self).send(str)
205
~/anaconda3/envs/python3/lib/python3.6/http/client.py in send(self, data)
995 try:
--> 996 self.sock.sendall(data)
997 except TypeError:
~/anaconda3/envs/python3/lib/python3.6/ssl.py in sendall(self, data, flags)
974 while count < amount:
--> 975 v = self.send(byte_view[count:])
976 count += v
~/anaconda3/envs/python3/lib/python3.6/ssl.py in send(self, data, flags)
943 self.__class__)
--> 944 return self._sslobj.write(data)
945 else:
~/anaconda3/envs/python3/lib/python3.6/ssl.py in write(self, data)
641 """
--> 642 return self._sslobj.write(data)
643
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
ProtocolError Traceback (most recent call last)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/httpsession.py in send(self, request)
319 decode_content=False,
--> 320 chunked=self._chunked(request.headers),
321 )
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
726 retries = retries.increment(
--> 727 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
728 )
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
378 # Disabled, indicate to re-raise the error.
--> 379 raise six.reraise(type(error), error, _stacktrace)
380
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/packages/six.py in reraise(tp, value, tb)
733 if value.__traceback__ is not tb:
--> 734 raise value.with_traceback(tb)
735 raise value
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
676 headers=headers,
--> 677 chunked=chunked,
678 )
~/anaconda3/envs/python3/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
391 else:
--> 392 conn.request(method, url, **httplib_request_kw)
393
~/anaconda3/envs/python3/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1261 """Send a complete request to the server."""
-> 1262 self._send_request(method, url, body, headers, encode_chunked)
1263
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/awsrequest.py in _send_request(self, method, url, body, headers, *args, **kwargs)
92 rval = super(AWSConnection, self)._send_request(
---> 93 method, url, body, headers, *args, **kwargs)
94 self._expect_header_set = False
~/anaconda3/envs/python3/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1307 body = _encode(body, 'body')
-> 1308 self.endheaders(body, encode_chunked=encode_chunked)
1309
~/anaconda3/envs/python3/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1256 raise CannotSendHeader()
-> 1257 self._send_output(message_body, encode_chunked=encode_chunked)
1258
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/awsrequest.py in _send_output(self, message_body, *args, **kwargs)
119 message_body = None
--> 120 self.send(msg)
121 if self._expect_header_set:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/awsrequest.py in send(self, str)
203 return
--> 204 return super(AWSConnection, self).send(str)
205
~/anaconda3/envs/python3/lib/python3.6/http/client.py in send(self, data)
995 try:
--> 996 self.sock.sendall(data)
997 except TypeError:
~/anaconda3/envs/python3/lib/python3.6/ssl.py in sendall(self, data, flags)
974 while count < amount:
--> 975 v = self.send(byte_view[count:])
976 count += v
~/anaconda3/envs/python3/lib/python3.6/ssl.py in send(self, data, flags)
943 self.__class__)
--> 944 return self._sslobj.write(data)
945 else:
~/anaconda3/envs/python3/lib/python3.6/ssl.py in write(self, data)
641 """
--> 642 return self._sslobj.write(data)
643
ProtocolError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))
During handling of the above exception, another exception occurred:
ConnectionClosedError Traceback (most recent call last)
<ipython-input-14-95dda20e8a70> in <module>
1 predictor = DeepARPredictor(endpoint_name=endpoint_name, sagemaker_session=sagemaker_session)
2 predictor.set_prediction_parameters(freq, prediction_length)
----> 3 list_of_df = predictor.predict(time_series_training)
<ipython-input-13-a0fbac2b9b07> in predict(self, ts, num_samples, quantiles)
7 prediction_times = [x.index[-1] + pd.Timedelta(1, unit=self.freq) for x in ts]
8 req = self.__encode_request(ts, num_samples, quantiles)
----> 9 res = super(DeepARPredictor, self).predict(req, initial_args={"ContentType": "application/json"})
10 return self.__decode_response(res, prediction_times)
11
~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/predictor.py in predict(self, data, initial_args, target_model, target_variant)
123
124 request_args = self._create_request_args(data, initial_args, target_model, target_variant)
--> 125 response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)
126 return self._handle_response(response)
127
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
355 "%s() only accepts keyword arguments." % py_operation_name)
356 # The "self" in this scope is referring to the BaseClient.
--> 357 return self._make_api_call(operation_name, kwargs)
358
359 _api_call.__name__ = str(py_operation_name)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
661 else:
662 http, parsed_response = self._make_request(
--> 663 operation_model, request_dict, request_context)
664
665 self.meta.events.emit(
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _make_request(self, operation_model, request_dict, request_context)
680 def _make_request(self, operation_model, request_dict, request_context):
681 try:
--> 682 return self._endpoint.make_request(operation_model, request_dict)
683 except Exception as e:
684 self.meta.events.emit(
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in make_request(self, operation_model, request_dict)
100 logger.debug("Making request for %s with params: %s",
101 operation_model, request_dict)
--> 102 return self._send_request(request_dict, operation_model)
103
104 def create_request(self, params, operation_model=None):
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _send_request(self, request_dict, operation_model)
135 request, operation_model, context)
136 while self._needs_retry(attempts, operation_model, request_dict,
--> 137 success_response, exception):
138 attempts += 1
139 # If there is a stream associated with the request, we need
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _needs_retry(self, attempts, operation_model, request_dict, response, caught_exception)
254 event_name, response=response, endpoint=self,
255 operation=operation_model, attempts=attempts,
--> 256 caught_exception=caught_exception, request_dict=request_dict)
257 handler_response = first_non_none_response(responses)
258 if handler_response is None:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/hooks.py in emit(self, event_name, **kwargs)
354 def emit(self, event_name, **kwargs):
355 aliased_event_name = self._alias_event_name(event_name)
--> 356 return self._emitter.emit(aliased_event_name, **kwargs)
357
358 def emit_until_response(self, event_name, **kwargs):
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/hooks.py in emit(self, event_name, **kwargs)
226 handlers.
227 """
--> 228 return self._emit(event_name, kwargs)
229
230 def emit_until_response(self, event_name, **kwargs):
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/hooks.py in _emit(self, event_name, kwargs, stop_on_response)
209 for handler in handlers_to_call:
210 logger.debug('Event %s: calling handler %s', event_name, handler)
--> 211 response = handler(**kwargs)
212 responses.append((handler, response))
213 if stop_on_response and response is not None:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempts, response, caught_exception, **kwargs)
181
182 """
--> 183 if self._checker(attempts, response, caught_exception):
184 result = self._action(attempts=attempts)
185 logger.debug("Retry needed, action of: %s", result)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempt_number, response, caught_exception)
249 def __call__(self, attempt_number, response, caught_exception):
250 should_retry = self._should_retry(attempt_number, response,
--> 251 caught_exception)
252 if should_retry:
253 if attempt_number >= self._max_attempts:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in _should_retry(self, attempt_number, response, caught_exception)
275 # If we've exceeded the max attempts we just let the exception
276 # propogate if one has occurred.
--> 277 return self._checker(attempt_number, response, caught_exception)
278
279
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempt_number, response, caught_exception)
315 for checker in self._checkers:
316 checker_response = checker(attempt_number, response,
--> 317 caught_exception)
318 if checker_response:
319 return checker_response
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempt_number, response, caught_exception)
221 elif caught_exception is not None:
222 return self._check_caught_exception(
--> 223 attempt_number, caught_exception)
224 else:
225 raise ValueError("Both response and caught_exception are None.")
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in _check_caught_exception(self, attempt_number, caught_exception)
357 # the MaxAttemptsDecorator is not interested in retrying the exception
358 # then this exception just propogates out past the retry code.
--> 359 raise caught_exception
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _do_get_response(self, request, operation_model)
198 http_response = first_non_none_response(responses)
199 if http_response is None:
--> 200 http_response = self._send(request)
201 except HTTPClientError as e:
202 return (None, e)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _send(self, request)
267
268 def _send(self, request):
--> 269 return self.http_session.send(request)
270
271
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/httpsession.py in send(self, request)
349 error=e,
350 request=request,
--> 351 endpoint_url=request.url
352 )
353 except Exception as e:
ConnectionClosedError: Connection was closed before we received a valid response from endpoint URL
Somebody know's why this happens?

I believe that Tarun might on the right path. The BrokenPipeError that you got is thrown when the connection is abruptly closed. See the python docs for BrokenPipeError.
The SageMaker endpoint probably drops the connection as soon as you go over the limit of 5MB. I suggest you try a smaller dataset. Also the data you send might get enlarged because of how sagemaker.tensorflow.model.TensorFlowPredictor encodes the data according to this comment on a similar issue.
If that doesn't work I've also seen a couple of people having problems with their networks in general. Specifically firewall/antivirus (for example this comment) or network timeout.
Hope this points you in the right direction.

Related

How can i using local photo for processing in Face API calling (Microsoft Cognitive Services) using Python

Update:
I have used the face_client.face.detect_with_url before and it worked.So my network should be fine (maybe, I guess). However, when I switched to the face_client.face.detect_with_stream, it still got the errors... Finally, I rewrote my code with referring to Azure Detect faces API, how to change the URL picture to a local picture?, and it could operate successfully. I'm still not sure why the previous question happened, but your advices # Jim Xu, # Satya V are appreciated!
After passing the official example (Microsoft Cognitive Services) to do the face detection via python. I have tried to detect the face with a local image. However, i am getting some errors....
The official code:
group_photo = 'test-image-person-group.jpg'
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)))
# Get test image
test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, group_photo))
image = open(test_image_array[0], 'r+b')
# Detect faces
face_ids = []
faces = face_client.face.detect_with_stream(image)
Refer to the official code, My code is:
IMAGES_FOLDER='' #the image path
test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, '*jpg'))
image = open(test_image_array[0], 'r+b')
faces = face_client.face.detect_with_stream(image)
However, there are some error
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
466
--> 467 low_conn.endheaders()
468
~/.conda/envs/Emotion_Azure/lib/python3.8/http/client.py in endheaders(self, message_body, encode_chunked)
1249 raise CannotSendHeader()
-> 1250 self._send_output(message_body, encode_chunked=encode_chunked)
1251
~/.conda/envs/Emotion_Azure/lib/python3.8/http/client.py in _send_output(self, message_body, encode_chunked)
1009 del self._buffer[:]
-> 1010 self.send(msg)
1011
~/.conda/envs/Emotion_Azure/lib/python3.8/http/client.py in send(self, data)
949 if self.auto_open:
--> 950 self.connect()
951 else:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/urllib3/connection.py in connect(self)
361
--> 362 self.sock = ssl_wrap_socket(
363 sock=conn,
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/urllib3/util/ssl_.py in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context, ca_cert_dir, key_password, ca_cert_data)
396
--> 397 return context.wrap_socket(sock)
398
~/.conda/envs/Emotion_Azure/lib/python3.8/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,
~/.conda/envs/Emotion_Azure/lib/python3.8/ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1040 self.do_handshake()
1041 except (OSError, ValueError):
~/.conda/envs/Emotion_Azure/lib/python3.8/ssl.py in do_handshake(self, block)
1308 self.settimeout(None)
-> 1309 self._sslobj.do_handshake()
1310 finally:
OSError: [Errno 0] Error
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
138 try:
--> 139 response = session.request(
140 request.method,
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
ConnectionError: [Errno 0] Error
During handling of the above exception, another exception occurred:
ClientRequestError Traceback (most recent call last)
<ipython-input-15-122d5fe49d09> in <module>
----> 1 faces = face_client.face.detect_with_stream(image)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/azure/cognitiveservices/vision/face/operations/_face_operations.py in detect_with_stream(self, image, return_face_id, return_face_landmarks, return_face_attributes, recognition_model, return_recognition_model, detection_model, custom_headers, raw, callback, **operation_config)
786 # Construct and send request
787 request = self._client.post(url, query_parameters, header_parameters, body_content)
--> 788 response = self._client.send(request, stream=False, **operation_config)
789
790 if response.status_code not in [200]:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/service_client.py in send(self, request, headers, content, **kwargs)
334 kwargs.setdefault('stream', True)
335 try:
--> 336 pipeline_response = self.config.pipeline.run(request, **kwargs)
337 # There is too much thing that expects this method to return a "requests.Response"
338 # to break it in a compatible release.
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/__init__.py in run(self, request, **kwargs)
195 pipeline_request = Request(request, context) # type: Request[HTTPRequestType]
196 first_node = self._impl_policies[0] if self._impl_policies else self._sender
--> 197 return first_node.send(pipeline_request, **kwargs) # type: ignore
198
199 class HTTPSender(AbstractContextManager, ABC, Generic[HTTPRequestType, HTTPResponseType]):
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/__init__.py in send(self, request, **kwargs)
148 self._policy.on_request(request, **kwargs)
149 try:
--> 150 response = self.next.send(request, **kwargs)
151 except Exception:
152 if not self._policy.on_exception(request, **kwargs):
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/requests.py in send(self, request, **kwargs)
70 try:
71 try:
---> 72 return self.next.send(request, **kwargs)
73 except (oauth2.rfc6749.errors.InvalidGrantError,
74 oauth2.rfc6749.errors.TokenExpiredError) as err:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/requests.py in send(self, request, **kwargs)
135
136 try:
--> 137 return self.next.send(request, **kwargs)
138 finally:
139 if old_max_redirects:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/__init__.py in send(self, request, **kwargs)
148 self._policy.on_request(request, **kwargs)
149 try:
--> 150 response = self.next.send(request, **kwargs)
151 except Exception:
152 if not self._policy.on_exception(request, **kwargs):
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/requests.py in send(self, request, **kwargs)
191 return Response(
192 request,
--> 193 self.driver.send(request.http_request, **kwargs)
194 )
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
331 """
332 requests_kwargs = self._configure_send(request, **kwargs)
--> 333 return super(RequestsHTTPSender, self).send(request, **requests_kwargs)
334
335
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
143 except requests.RequestException as err:
144 msg = "Error occurred in request."
--> 145 raise_with_traceback(ClientRequestError, msg, err)
146
147 return RequestsClientResponse(request, response)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/exceptions.py in raise_with_traceback(exception, message, *args, **kwargs)
49 error = exception(exc_msg, *args, **kwargs)
50 try:
---> 51 raise error.with_traceback(exc_traceback)
52 except AttributeError:
53 error.__traceback__ = exc_traceback
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
137 session = kwargs.pop('session', self.session)
138 try:
--> 139 response = session.request(
140 request.method,
141 request.url,
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
528 }
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
532 return resp
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
641
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
645 # Total elapsed time of the request (approximately)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
496
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
500 except MaxRetryError as e:
ClientRequestError: Error occurred in request., ConnectionError: [Errno 0] Error
How can i fix it? Did I miss something or make a mistake?

Python can't request with proxies to most servers

I got the following code, proxy is a public proxy I found on this site.
import requests
proxies = {
"https": "http://27.203.242.127:8060",
"http": "http://27.203.242.127:8060"}
url = "http://httpbin.org/ip"
url2 = "https://www.google.com/"
r1 = requests.get(url, proxies=proxies, headers={'User-Agent': 'Mozilla'})
r2 = requests.get(url2, proxies=proxies, headers={'User-Agent': 'Mozilla'})
r1 runs just fine but I get a long error message when I try to run r2.
First error I get is "BadStatusLine" and from there a few "During handling of the above exception, another exception occurred:" spawning ProtocolError and ConnectionError.
Full traceback:
---------------------------------------------------------------------------
BadStatusLine Traceback (most recent call last)
~\anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
661 if is_new_proxy_conn:
--> 662 self._prepare_proxy(conn)
663
~\anaconda3\lib\site-packages\urllib3\connectionpool.py in _prepare_proxy(self, conn)
947 conn.set_tunnel(self._proxy_host, self.port, self.proxy_headers)
--> 948 conn.connect()
949
~\anaconda3\lib\site-packages\urllib3\connection.py in connect(self)
307 # self._tunnel_host below.
--> 308 self._tunnel()
309 # Mark this connection as not reusable
~\anaconda3\lib\http\client.py in _tunnel(self)
915 response = self.response_class(self.sock, method=self._method)
--> 916 (version, code, message) = response._read_status()
917
~\anaconda3\lib\http\client.py in _read_status(self)
287 self._close_conn()
--> 288 raise BadStatusLine(line)
289
BadStatusLine: <html>
During handling of the above exception, another exception occurred:
ProtocolError Traceback (most recent call last)
~\anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
448 retries=self.max_retries,
--> 449 timeout=timeout
450 )
~\anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
719 retries = retries.increment(
--> 720 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
721 )
~\anaconda3\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
399 if read is False or not self._is_method_retryable(method):
--> 400 raise six.reraise(type(error), error, _stacktrace)
401 elif read is not None:
~\anaconda3\lib\site-packages\urllib3\packages\six.py in reraise(tp, value, tb)
733 if value.__traceback__ is not tb:
--> 734 raise value.with_traceback(tb)
735 raise value
~\anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
661 if is_new_proxy_conn:
--> 662 self._prepare_proxy(conn)
663
~\anaconda3\lib\site-packages\urllib3\connectionpool.py in _prepare_proxy(self, conn)
947 conn.set_tunnel(self._proxy_host, self.port, self.proxy_headers)
--> 948 conn.connect()
949
~\anaconda3\lib\site-packages\urllib3\connection.py in connect(self)
307 # self._tunnel_host below.
--> 308 self._tunnel()
309 # Mark this connection as not reusable
~\anaconda3\lib\http\client.py in _tunnel(self)
915 response = self.response_class(self.sock, method=self._method)
--> 916 (version, code, message) = response._read_status()
917
~\anaconda3\lib\http\client.py in _read_status(self)
287 self._close_conn()
--> 288 raise BadStatusLine(line)
289
ProtocolError: ('Connection aborted.', BadStatusLine('<html>\r\n'))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
<ipython-input-77-db3266a345e8> in <module>
7 url = "http://httpbin.org/ip"
8 url2 = "https://www.google.com/"
----> 9 r = requests.get(url2, proxies=proxies, headers={'User-Agent': 'Mozilla'})
~\anaconda3\lib\site-packages\requests\api.py in get(url, params, **kwargs)
73
74 kwargs.setdefault('allow_redirects', True)
---> 75 return request('get', url, params=params, **kwargs)
76
77
~\anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
58 # cases, and look like a memory leak in others.
59 with sessions.Session() as session:
---> 60 return session.request(method=method, url=url, **kwargs)
61
62
~\anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
531 }
532 send_kwargs.update(settings)
--> 533 resp = self.send(prep, **send_kwargs)
534
535 return resp
~\anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
644
645 # Send the request
--> 646 r = adapter.send(request, **kwargs)
647
648 # Total elapsed time of the request (approximately)
~\anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
496
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
500 except MaxRetryError as e:
ConnectionError: ('Connection aborted.', BadStatusLine('<html>\r\n'))
Sometimes you may need to initiate a session for certain request. I have no idea if this is one, but I'd give that a go:
s = requests.Session()
s.proxies = {
“http”: “http://10.10.10.10:8000”,
“https”: “http://10.10.10.10:8000”,
}
r = s.get(“http://toscrape.com”)
This is referenced from here. Hope that helps!

Requests error only with one specific website : OS Error 10054 Connection aborted

I get this error only on this particular website.
I have googled and read everything but none of them solved my problem.
Very simple piece of code, can anybody figure out what's going on? Other websites are working fine.
import requests
url = 'http://www.trading212.com/en/Trading-Instruments?id=3'
response = requests.get(url)
Error code:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
671 headers=headers,
--> 672 chunked=chunked,
673 )
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
420 # Otherwise it looks like a bug in the code.
--> 421 six.raise_from(e, None)
422 except (SocketTimeout, BaseSSLError, SocketError) as e:
~\Anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
415 try:
--> 416 httplib_response = conn.getresponse()
417 except BaseException as e:
~\Anaconda3\lib\http\client.py in getresponse(self)
1320 try:
-> 1321 response.begin()
1322 except ConnectionError:
~\Anaconda3\lib\http\client.py in begin(self)
295 while True:
--> 296 version, status, reason = self._read_status()
297 if status != CONTINUE:
~\Anaconda3\lib\http\client.py in _read_status(self)
256 def _read_status(self):
--> 257 line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
258 if len(line) > _MAXLINE:
~\Anaconda3\lib\socket.py in readinto(self, b)
588 try:
--> 589 return self._sock.recv_into(b)
590 except timeout:
~\Anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py in recv_into(self, *args, **kwargs)
317 else:
--> 318 raise SocketError(str(e))
319 except OpenSSL.SSL.ZeroReturnError:
OSError: (10054, 'WSAECONNRESET')
During handling of the above exception, another exception occurred:
ProtocolError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
448 retries=self.max_retries,
--> 449 timeout=timeout
450 )
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
719 retries = retries.increment(
--> 720 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
721 )
~\Anaconda3\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
399 if read is False or not self._is_method_retryable(method):
--> 400 raise six.reraise(type(error), error, _stacktrace)
401 elif read is not None:
~\Anaconda3\lib\site-packages\urllib3\packages\six.py in reraise(tp, value, tb)
733 if value.__traceback__ is not tb:
--> 734 raise value.with_traceback(tb)
735 raise value
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
671 headers=headers,
--> 672 chunked=chunked,
673 )
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
420 # Otherwise it looks like a bug in the code.
--> 421 six.raise_from(e, None)
422 except (SocketTimeout, BaseSSLError, SocketError) as e:
~\Anaconda3\lib\site-packages\urllib3\packages\six.py in raise_from(value, from_value)
~\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
415 try:
--> 416 httplib_response = conn.getresponse()
417 except BaseException as e:
~\Anaconda3\lib\http\client.py in getresponse(self)
1320 try:
-> 1321 response.begin()
1322 except ConnectionError:
~\Anaconda3\lib\http\client.py in begin(self)
295 while True:
--> 296 version, status, reason = self._read_status()
297 if status != CONTINUE:
~\Anaconda3\lib\http\client.py in _read_status(self)
256 def _read_status(self):
--> 257 line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
258 if len(line) > _MAXLINE:
~\Anaconda3\lib\socket.py in readinto(self, b)
588 try:
--> 589 return self._sock.recv_into(b)
590 except timeout:
~\Anaconda3\lib\site-packages\urllib3\contrib\pyopenssl.py in recv_into(self, *args, **kwargs)
317 else:
--> 318 raise SocketError(str(e))
319 except OpenSSL.SSL.ZeroReturnError:
ProtocolError: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')"))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
<ipython-input-2-c162877d446b> in <module>
1 url = 'http://www.trading212.com/en/Trading-Instruments?id=3'
----> 2 response = requests.get(url)
~\Anaconda3\lib\site-packages\requests\api.py in get(url, params, **kwargs)
74
75 kwargs.setdefault('allow_redirects', True)
---> 76 return request('get', url, params=params, **kwargs)
77
78
~\Anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
59 # cases, and look like a memory leak in others.
60 with sessions.Session() as session:
---> 61 return session.request(method=method, url=url, **kwargs)
62
63
~\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
528 }
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
532 return resp
~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
663
664 # Resolve redirects if allowed.
--> 665 history = [resp for resp in gen] if allow_redirects else []
666
667 # Shuffle things around if there's history.
~\Anaconda3\lib\site-packages\requests\sessions.py in <listcomp>(.0)
663
664 # Resolve redirects if allowed.
--> 665 history = [resp for resp in gen] if allow_redirects else []
666
667 # Shuffle things around if there's history.
~\Anaconda3\lib\site-packages\requests\sessions.py in resolve_redirects(self, resp, req, stream, timeout, verify, cert, proxies, yield_requests, **adapter_kwargs)
243 proxies=proxies,
244 allow_redirects=False,
--> 245 **adapter_kwargs
246 )
247
~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
641
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
645 # Total elapsed time of the request (approximately)
~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
496
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
500 except MaxRetryError as e:
ConnectionError: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')"))
My guess is they are blocking "unwanted" users. Spoof the User-Agent header, it should be fine:
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15'}
response = requests.get(url, headers=headers)

Remote end closed connection without response (Python 3- Bugzilla)

I am currently working on a project that involves python-bugzilla module.
When I try to collect some bug data, I get the following error :
RemoteDisconnected : Remote end closed connection without response
api = Bugzilla(url)
product = ...
request = api.build_query(product=product, include_fields=["id"])
data = api.query(request)
ids = np.array([bug.id for bug in data]).reshape(-1)
n = ids.shape[0]
q = 500 #size of bug package
if q < n :
m = n%q
k = (n+q-m)/q
else:
k = n
ids_splitted = np.array_split(ids, k)
bugs = []
for ids_ in ids_splitted:
bugs = bugs + api.getbugs(ids_)
The complete error is :
---------------------------------------------------------------------------
RemoteDisconnected Traceback (most recent call last)
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
599 body=body, headers=headers,
--> 600 chunked=chunked)
601
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
383 # otherwise it looks like a programming error was the cause.
--> 384 six.raise_from(e, None)
385 except (SocketTimeout, BaseSSLError, SocketError) as e:
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/packages/six.py in raise_from(value, from_value)
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
379 try:
--> 380 httplib_response = conn.getresponse()
381 except Exception as e:
~/anaconda3/envs/DataScience/lib/python3.7/http/client.py in getresponse(self)
1320 try:
-> 1321 response.begin()
1322 except ConnectionError:
~/anaconda3/envs/DataScience/lib/python3.7/http/client.py in begin(self)
295 while True:
--> 296 version, status, reason = self._read_status()
297 if status != CONTINUE:
~/anaconda3/envs/DataScience/lib/python3.7/http/client.py in _read_status(self)
264 # sending a valid response.
--> 265 raise RemoteDisconnected("Remote end closed connection without"
266 " response")
RemoteDisconnected: Remote end closed connection without response
During handling of the above exception, another exception occurred:
ProtocolError Traceback (most recent call last)
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
448 retries=self.max_retries,
--> 449 timeout=timeout
450 )
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
637 retries = retries.increment(method, url, error=e, _pool=self,
--> 638 _stacktrace=sys.exc_info()[2])
639 retries.sleep()
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
366 if read is False or not self._is_method_retryable(method):
--> 367 raise six.reraise(type(error), error, _stacktrace)
368 elif read is not None:
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/packages/six.py in reraise(tp, value, tb)
684 if value.__traceback__ is not tb:
--> 685 raise value.with_traceback(tb)
686 raise value
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
599 body=body, headers=headers,
--> 600 chunked=chunked)
601
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
383 # otherwise it looks like a programming error was the cause.
--> 384 six.raise_from(e, None)
385 except (SocketTimeout, BaseSSLError, SocketError) as e:
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/packages/six.py in raise_from(value, from_value)
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
379 try:
--> 380 httplib_response = conn.getresponse()
381 except Exception as e:
~/anaconda3/envs/DataScience/lib/python3.7/http/client.py in getresponse(self)
1320 try:
-> 1321 response.begin()
1322 except ConnectionError:
~/anaconda3/envs/DataScience/lib/python3.7/http/client.py in begin(self)
295 while True:
--> 296 version, status, reason = self._read_status()
297 if status != CONTINUE:
~/anaconda3/envs/DataScience/lib/python3.7/http/client.py in _read_status(self)
264 # sending a valid response.
--> 265 raise RemoteDisconnected("Remote end closed connection without"
266 " response")
ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
<ipython-input-9-820dd90f4151> in <module>
1 t0 = time()
2 request = api.build_query(product=product, include_fields=["id"])
----> 3 data = api.query(request)
4 ids = np.array([bug.id for bug in data]).reshape(-1)
5 n = ids.shape[0]
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/bugzilla/base.py in query(self, query)
1263 """
1264 try:
-> 1265 r = self._proxy.Bug.search(query)
1266 except Fault as e:
1267
~/anaconda3/envs/DataScience/lib/python3.7/xmlrpc/client.py in __call__(self, *args)
1110 return _Method(self.__send, "%s.%s" % (self.__name, name))
1111 def __call__(self, *args):
-> 1112 return self.__send(self.__name, args)
1113
1114 ##
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/bugzilla/transport.py in _ServerProxy__request(self, methodname, params)
102 # pylint: disable=no-member
103 ret = super(_BugzillaServerProxy,
--> 104 self)._ServerProxy__request(methodname, params)
105 # pylint: enable=no-member
106
~/anaconda3/envs/DataScience/lib/python3.7/xmlrpc/client.py in __request(self, methodname, params)
1450 self.__handler,
1451 request,
-> 1452 verbose=self.__verbose
1453 )
1454
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/bugzilla/transport.py in request(self, host, handler, request_body, verbose)
199 request_body = request_body.replace(b'\r', b'
')
200
--> 201 return self._request_helper(url, request_body)
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/bugzilla/transport.py in _request_helper(self, url, request_body)
162 try:
163 response = self.session.post(
--> 164 url, data=request_body, **self.request_defaults)
165
166 # We expect utf-8 from the server
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/requests/sessions.py in post(self, url, data, json, **kwargs)
579 """
580
--> 581 return self.request('POST', url, data=data, json=json, **kwargs)
582
583 def put(self, url, data=None, **kwargs):
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
531 }
532 send_kwargs.update(settings)
--> 533 resp = self.send(prep, **send_kwargs)
534
535 return resp
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/requests/sessions.py in send(self, request, **kwargs)
644
645 # Send the request
--> 646 r = adapter.send(request, **kwargs)
647
648 # Total elapsed time of the request (approximately)
~/anaconda3/envs/DataScience/lib/python3.7/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
496
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
500 except MaxRetryError as e:
ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
Can someone help me to fix this ? This is really strange as my code worked something like 2 weeks ago...
EDIT: I ran the code some times, and I noticed that it can either run perfectly or be stopped ... So, I assume it is not due to my code but to another thing. Since I am not an expert, I do not know can be the cause of this ... So, if someone can explain this to me, it will be great
EDIT2: If someone has a same issue, that is due to the server activity. Some servers limit the frequency of calling. To work around, one would try to sleep the code for let's say 1 second, that should do the job. But, the runtime will be increased.

Python JavaScript Scraping: ConnectionRefusedError: [Errno 61] Connection refused

I am trying to scrape some info off of Yahoo Finance. Below is my simple code:
url = "https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL"
from selenium import webdriver
webdriver.PhantomJS(executable_path="/anaconda/bin/phantomjs")
browser.get(url)
browser.quit
But I get the following error:
ConnectionRefusedError Traceback (most recent call last)
<ipython-input-12-f4d26b367ef7> in <module>()
8 webdriver.PhantomJS(executable_path="/anaconda/bin/phantomjs")
9
---> 10 browser.get(url)
> /anaconda/lib/python3.6/site-packages/selenium-3.4.3- py3.6.egg/selenium/webdriver/remote/webdriver.py in get(self, url)
266 Loads a web page in the current browser session.
267 """
--> 268 self.execute(Command.GET, {'url': url})
269
270 #property
/anaconda/lib/python3.6/site-packages/selenium-3.4.3-py3.6.egg/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
252
253 params = self._wrap_value(params)
--> 254 response = self.command_executor.execute(driver_command, params)
255 if response:
256 self.error_handler.check_response(response)
/anaconda/lib/python3.6/site-packages/selenium-3.4.3-py3.6.egg/selenium/webdriver/remote/remote_connection.py in execute(self, command, params)
462 path = string.Template(command_info[1]).substitute(params)
463 url = '%s%s' % (self._url, path)
--> 464 return self._request(command_info[0], url, body=data)
465
466 def _request(self, method, url, body=None):
/anaconda/lib/python3.6/site-packages/selenium-3.4.3-py3.6.egg/selenium/webdriver/remote/remote_connection.py in _request(self, method, url, body)
485 body = None
486 try:
--> 487 self._conn.request(method, parsed_url.path, body, headers)
488 resp = self._conn.getresponse()
489 except (httplib.HTTPException, socket.error):
/anaconda/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
1237 encode_chunked=False):
1238 """Send a complete request to the server."""
-> 1239 self._send_request(method, url, body, headers, encode_chunked)
1240
1241 def _send_request(self, method, url, body, headers, encode_chunked):
/anaconda/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
1283 # default charset of iso-8859-1.
1284 body = _encode(body, 'body')
-> 1285 self.endheaders(body, encode_chunked=encode_chunked)
1286
1287 def getresponse(self):
/anaconda/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
1232 else:
1233 raise CannotSendHeader()
-> 1234 self._send_output(message_body, encode_chunked=encode_chunked)
1235
1236 def request(self, method, url, body=None, headers={}, *,
/anaconda/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
1024 msg = b"\r\n".join(self._buffer)
1025 del self._buffer[:]
-> 1026 self.send(msg)
1027
1028 if message_body is not None:
/anaconda/lib/python3.6/http/client.py in send(self, data)
962 if self.sock is None:
963 if self.auto_open:
--> 964 self.connect()
965 else:
966 raise NotConnected()
/anaconda/lib/python3.6/http/client.py in connect(self)
934 """Connect to the host and port specified in __init__."""
935 self.sock = self._create_connection(
--> 936 (self.host,self.port), self.timeout, self.source_address)
937 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
938
/anaconda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
720
721 if err is not None:
--> 722 raise err
723 else:
724 raise error("getaddrinfo returns an empty list")
/anaconda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
711 if source_address:
712 sock.bind(source_address)
713 sock.connect(sa)
714 return sock
715
ConnectionRefusedError: [Errno 61] Connection refused
I have checked other questions on this topic but couldn't find any relevant answers. How can I correct this error?
Here is the Answer to your Question:
The error stack trace gives us a hint that the actual issue may be in this line of the code:
webdriver.PhantomJS(executable_path="/anaconda/bin/phantomjs")
As you are trying to open the URL through the instance browser as in browser.get(url), the browser instance must be configured with the absolute path of the PhantomJS executable as follows (as on my Win8 machine through PyCharm IDE):
from selenium import webdriver
url = "https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL"
browser = webdriver.PhantomJS(executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
browser.get(url)
# your code block
print(browser.title)
# your code block
browser.quit
The output on my console is as follows:
AAPL Key Statistics | Apple Inc. Stock - Yahoo Finance
Let me know if this Answers your Question.

Categories