Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Colaboratory - Can I use it to connect to a local server?

Excuse my ignorance; I'm trying to share a Python script that uses ElasticSearch to connect to a local instance. I am unable to get this to work, because I keep seeing:

ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

NewConnectionError                        Traceback (most recent call last)
NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f0e60739c50>: Failed to establish a new connection: [Errno 111] Connection refused

I believe it's because the script being run on "Google Colabratory" is connecting relative to where the Python notebook is being run, and it's not able to connect to my ES instance locally. Is there a good way to allow the script to execute the commands on my local machine, through Google Colab?

If you're curious as to the exact command that is failing, I believe this is it: es = Elasticsearch(config.get('elasticsearch_url'))

like image 539
Shazbots Avatar asked Oct 23 '25 02:10

Shazbots


2 Answers

Here's how I run ElasticSearch on Colab

!wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux-x86_64.tar.gz -q
!tar -xzf elasticsearch-7.0.0-linux-x86_64.tar.gz
!chown -R daemon:daemon elasticsearch-7.0.0
# start server
import os
from subprocess import Popen, PIPE, STDOUT
es_server = Popen(['elasticsearch-7.0.0/bin/elasticsearch'], 
                  stdout=PIPE, stderr=STDOUT,
                  preexec_fn=lambda: os.setuid(1)  # as daemon
                 )
# wait a bit then test
!curl -X GET "localhost:9200/"

I have more details and example in this gist

like image 80
korakot Avatar answered Oct 25 '25 17:10

korakot


There is no local on Colab by default. As it's based on Jupyter, the runtime that the interface uses might be connected to a local Jupyter endpoint for this you must install Jupyter, see the guide.

You might want to try the hosted version of Jupyter called Notebook instances. There you are on the same VPC as other VMS in Google Cloud Platform.

like image 42
Pentium10 Avatar answered Oct 25 '25 19:10

Pentium10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!