Started moving to python-requests so we can do a get() with an
interator on the response, but it turns out that it's really crappy with regards to socket closing, so I'll revert this in a moment. git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10875 ddd99763-3ecb-0310-9145-efcb8ce7c51f
This commit is contained in:
parent
3f436e6dfd
commit
624980a47b
|
@ -10,8 +10,9 @@ import os
|
|||
import simplejson as json
|
||||
import urlparse
|
||||
import urllib
|
||||
import pycurl
|
||||
import cStringIO
|
||||
import requests
|
||||
import logging
|
||||
|
||||
version = "1.0"
|
||||
|
||||
|
@ -42,7 +43,7 @@ class ServerError(Error):
|
|||
pass
|
||||
|
||||
class MyCurl(object):
|
||||
"""Curl wrapper for HTTP client requests"""
|
||||
"""Wrapper for HTTP requests using python-requests"""
|
||||
def __init__(self, baseurl = ""):
|
||||
"""If baseurl is supplied, all other functions that take
|
||||
a URL can be given a relative URL instead."""
|
||||
|
@ -51,21 +52,16 @@ class MyCurl(object):
|
|||
if '://' not in reparsed:
|
||||
reparsed = urlparse.urlparse("http://" + baseurl).geturl()
|
||||
self.baseurl = reparsed
|
||||
self.curl = pycurl.Curl()
|
||||
self.curl.setopt(pycurl.SSL_VERIFYHOST, 2)
|
||||
self.curl.setopt(pycurl.FOLLOWLOCATION, 1)
|
||||
self.curl.setopt(pycurl.MAXREDIRS, 5)
|
||||
self._setup_url()
|
||||
|
||||
def _setup_url(self, url = "", params = ""):
|
||||
url = urlparse.urljoin(self.baseurl, url)
|
||||
if params:
|
||||
url = urlparse.urljoin(url, "?" + urllib.urlencode(params, True))
|
||||
self.curl.setopt(pycurl.URL, url)
|
||||
self.url = url
|
||||
|
||||
def _check_error(self, body = None):
|
||||
code = self.curl.getinfo(pycurl.RESPONSE_CODE)
|
||||
code = self.req.status_code
|
||||
if code == 200:
|
||||
return
|
||||
# Default variables for exception
|
||||
|
@ -91,21 +87,22 @@ class MyCurl(object):
|
|||
|
||||
def _req(self, url, params):
|
||||
"""GET or POST that returns raw data"""
|
||||
print "req:", url, params
|
||||
self._setup_url(url, params)
|
||||
body = cStringIO.StringIO()
|
||||
self.curl.setopt(pycurl.WRITEFUNCTION, body.write)
|
||||
try:
|
||||
self.curl.perform()
|
||||
except pycurl.error as e:
|
||||
self.req = requests.get(self.url)
|
||||
except requests.ConnectionError as e:
|
||||
raise ServerError(status = "502 Error",
|
||||
url = self.url,
|
||||
message = e[1])
|
||||
body_str = body.getvalue()
|
||||
message = "Connection error")
|
||||
body_str = self.req.text
|
||||
self._check_error(body_str)
|
||||
print "req done:", url, params
|
||||
return body_str
|
||||
|
||||
def close(self):
|
||||
self.curl.close()
|
||||
print "Should have closed!"
|
||||
return
|
||||
|
||||
def getjson(self, url, params = None):
|
||||
"""Simple GET that returns JSON string"""
|
||||
|
@ -117,15 +114,12 @@ class MyCurl(object):
|
|||
|
||||
def get(self, url, params = None):
|
||||
"""Simple GET"""
|
||||
self.curl.setopt(pycurl.UPLOAD, 0)
|
||||
return self._req(url, params)
|
||||
|
||||
def put(self, url, postdata, params = None):
|
||||
"""Simple PUT"""
|
||||
self._setup_url(url, params)
|
||||
data = cStringIO.StringIO(postdata)
|
||||
self.curl.setopt(pycurl.UPLOAD, 1)
|
||||
self.curl.setopt(pycurl.READFUNCTION, data.read)
|
||||
# send postdata
|
||||
return self._req(url, params)
|
||||
|
||||
class Client(object):
|
||||
|
@ -265,7 +259,6 @@ class Client(object):
|
|||
|
||||
more = True
|
||||
while more:
|
||||
self.curl.get
|
||||
(intervals, more) = self.curl.getjson("stream/intervals", params)
|
||||
for interval in intervals:
|
||||
yield interval
|
||||
|
|
Loading…
Reference in New Issue
Block a user