Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created December 9, 2011 19:55
Show Gist options
  • Save mattdeboard/1453026 to your computer and use it in GitHub Desktop.
Save mattdeboard/1453026 to your computer and use it in GitHub Desktop.
Replace httplib* in pysolr with requests
import requests
class Solr(object):
def __init__(self, url, decoder=None, auth=None, timeout=60):
self.decoder = decoder or json.JSONDecoder()
self.auth = auth
self.url = url
self.scheme, netloc, path, query, fragment = urlsplit(url)
self.base_url = urlunsplit((self.scheme, netloc, '', '', ''))
netloc = netloc.split(':')
self.host = netloc[0]
if len(netloc) == 1:
self.host, self.port = netloc[0], None
else:
self.host, self.port = netloc[0], int(netloc[1])
self.path = path.rstrip('/')
self.timeout = timeout
self.log = self._get_log()
def _get_log(self):
return LOG
def _send_request(self, method, path, body=None, headers=None):
url = self.base_url + path
start_time = time.time()
self.log.debug("Starting request to '%s' (%s) with body '%s'..." % (url, method, str(body)[:10]))
request = requests.request(method, url, data=body, headers=headers,
timeout=self.timeout, auth=self.auth)
end_time = time.time()
self.log.info("Finished '%s' (%s) with body '%s' in %0.3f seconds." %
(url, method, str(body)[:10], end_time - start_time))
headers,response = request.headers, request.content
if request.status_code != 200:
error_message = self._extract_error(headers, response)
self.log.error(error_message)
raise SolrError(error_message)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment