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.

8
  • It looks like HTTP basic authentication is simply not implemented in PySolr. There's an outstanding pull request, but that's several years old, I doubt that this PR is ever going to be merged like that.
    – Lukas Graf
    Oct 4, 2014 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. stackoverflow.com/questions/6466188/…
    – Hari_1123
    Oct 4, 2014 at 22:45
  • 1
    Honestly, I would actually consider not using an integration package at all. Solr's JSON API is pretty simple, and with the Python requests 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 4, 2014 at 22:54
  • From a quick grep it does look like solrpy does implement HTTP basic auth though: solr/core.py:426
    – Lukas Graf
    Oct 4, 2014 at 22:57
  • 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 4, 2014 at 23:45

2 Answers 2

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

That's all you need ...

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.