question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Port number automatically assigned when not needed

See original GitHub issue

Hello!

I had an issue using elasticsearch-py to connect to an elasticsearch index that was resulting from the url formatting in connection.http_requests.RequestHttpConnection. In the __init__ of this class, a default value for port is specified which was making its way into the url which seemed to allow the connection but caused a TransportError(404, u'index_not_found_exception', u'no such index') when trying to search my index. Here’s the code I used:

from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth

host = "my_elasticsearch_index.es.amazonaws.com/"
AWS_ACCESS_KEY_ID = "the_access_key"
AWS_SECRET_ACCESS_KEY = "the_secret_access_key"
AWS_REGION = "my-region"

awsauth = AWS4Auth(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, 'es')

es = Elasticsearch(
    hosts=[{'host': host}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
)
query =  {}
page = es.search(
    index="my-index-name",
    scroll='2m',
    size=1000,
    body=query
)

The error occurs on the page=es.search() line:

  File "/venv/local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
NotFoundError: TransportError(404, u'index_not_found_exception', u'no such index')

Removing the port value specification fixed the issue. The default port number was making the url come out as https://my_elasticsearch_index.es.amazonaws.com/:9200/my-index-name I ended up making the following modification to the __init__ in RequestHttpConnection around line 46:

        if port is not None and type(port) == int:
            self.base_url = 'http%s://%s:%d%s' % (
                's' if self.use_ssl else '',
                host, port, self.url_prefix
            )
        elif port is None:
            self.base_url = 'http%s://%s%s' % (
                's' if self.use_ssl else '',
                host, self.url_prefix
            )

And changed the code for the variable es to specify the port as None:

es = Elasticsearch(
    hosts=[{'host': host, 'port': None}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
)

This works, and formats the url properly, but I’m wondering: is there another methodology I could use to connect to AWS and query an index without modifying elasticsearch-py?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fxdgearcommented, Nov 30, 2017

By default Elasticsearch is available on port 9200. Which is why elasticsearch-py behaves this way. If you want to use a different port you can specify any port you want.

It looks like you are using Elasticsearch running on port 443. (unless you’re using http, then it would be port 80).

Default ports:

I would recommend not altering the elasticsearch-py code and instead instantiate your connection using the port that your elasticsearch instance is running on.

es = Elasticsearch(
    hosts=[{'host': host, 'port': 443}],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection
)

I’m making an assumption on port 443 here since you have use_ssl=True

0reactions
mr-wolverinecommented, Nov 15, 2022

I have same problem. Why this issues is closed? Is there any solutions? I didn’t find one from the context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are dynamic port numbers and how do they work?
Dynamic port numbers enable applications to communicate with each other using TCP or UDP. Learn how dynamic ports work and are standardized.
Read more >
python - How can I automatically assign a free port for socket ...
1 Answer 1 · Before listening, the server binds its socket to port 0, which causes the system to choose a free port....
Read more >
Ephemeral port - Wikipedia
Such short-lived ports are allocated automatically within a predefined range of port numbers by the IP stack software of a computer operating system....
Read more >
Registered Port - an overview | ScienceDirect Topics
Dynamic ports—Ports in the range 49152 to 65535 are not assigned, controlled, or registered. They are used for temporary or private ports.
Read more >
Ports - IBM
Because advance knowledge of the client's port number is not needed, a client often leaves it to TCP/IP to assign a free port...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found