0

This is the first time I am using Python and Solr. I have my Solr instance set up within tomcat on GCE. I am trying to connect to it from my Python code using PySolr. However, I am not sure how to send authentication parameters via PySolr.

This is the exception I get:

   solr = pysolr.Solr('http://MY INSTANCE IP/solr/News', timeout=10)
Apache Tomcat/7.0.28 - Error report

HTTP Status 401 -

type Status report

message

description This request requires HTTP authentication ().

Apache Tomcat/7.0.28

Please advise.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Hari_1123
  • 1
  • 1
  • 1
  • It looks like HTTP basic authentication is simply not implemented in PySolr. There's an [outstanding pull request](https://github.com/toastdriven/pysolr/pull/30), but that's several years old, I doubt that this PR is ever going to be merged like that. – Lukas Graf Oct 04 '14 at 22:26
  • Hmmm, may be I ll try switching to solrpy, do you recommend any other package that works well ? I found another post on using sunburnt, but looking at their API documentation, I dont see a provision for authentication. http://stackoverflow.com/questions/6466188/django-python-and-apache-solr-pysolr-or-solrpy – Hari_1123 Oct 04 '14 at 22:45
  • 1
    Honestly, I would actually consider not using an integration package at all. [Solr's JSON API](http://wiki.apache.org/solr/SolJSON) is pretty simple, and with the Python [`requests`](http://docs.python-requests.org/en/latest/) module it's trivial to make HTTP requests with custom headers (like `Authorization`), read status codes and deal with JSON. So you might be better off to just talk to Solr yourself, and maybe write your own little wrapper module if necessary. – Lukas Graf Oct 04 '14 at 22:54
  • From a quick `grep` it does look like `solrpy` does implement HTTP basic auth though: [`solr/core.py:426`](https://code.google.com/p/solrpy/source/browse/solr/core.py#426) – Lukas Graf Oct 04 '14 at 22:57
  • Yup - it's barely documented, but you can [pass `http_user` and `http_pass`](https://code.google.com/p/solrpy/source/browse/solr/core.py#371) to the `SolrConnection()` to get HTTP basic auth with `solrpy`. I haven't ever used `solrpy` though (or `pysolr` for that matter), so I can't comment on the *"works well"* part. – Lukas Graf Oct 04 '14 at 23:03
  • Bad news for `sunburnt` though, I can't see any traces of it supporting auth. – Lukas Graf Oct 04 '14 at 23:07
  • Thank you Lukas for your advise, I will use Solrpy or Solr JSON directly. – Hari_1123 Oct 04 '14 at 23:41
  • 1
    You're welcome, and good luck. If you find a method that works well for you, feel free to post that as an answer and self-accept. – Lukas Graf Oct 04 '14 at 23:45

2 Answers2

2
solr = pysolr.Solr('http://user:pass@IP:8983/solr/')

That's all you need ...

kristall
  • 21
  • 2
2

You can pass Solr authentication as part of the Solr connection parameter.

You don't have proper documentation in pySolr on how to carry out authentication. Since pySolr internally uses requests for authentication you can follow authentication in requests. Here is a small example on custom authentication as well.

In the case of Basic Authentication, you can use it as

    solr = pysolr.Solr('http://IP:8983/solr/collection',auth=('username','password'))

or

    from requests.auth import HTTPBasicAuth
    solr = pysolr.Solr('http://IP:8983/solr/collection',auth=HTTPBasicAuth('username','password'))

This is the proper way of authentication. Passing username and password as a part of URL is not recommended as it might create issues if @ or ' are used in any of those may create issues in the authentication.Refer this GitHub issue

Mani
  • 5,401
  • 1
  • 30
  • 51