本文整理匯總了Python中pycurl.HTTP_CODE屬性的典型用法代碼示例。如果您正苦於以下問題:Python pycurl.HTTP_CODE屬性的具體用法?Python pycurl.HTTP_CODE怎麽用?Python pycurl.HTTP_CODE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類pycurl
的用法示例。
在下文中一共展示了pycurl.HTTP_CODE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __call__
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def __call__(self, json, throw):
"""Fetch the URL"""
try:
self.curl.perform()
status = self.curl.getinfo(pycurl.HTTP_CODE)
text = self.buf.getvalue()
except pycurl.error as ex:
(code, message) = ex
status = 400
text = message
finally:
self.curl.close()
self.buf.close()
#If status is outside the HTTP 2XX success range
if status < 200 or status > 299:
if throw:
raise URLException(text.strip())
else:
return (status, text)
if json:
return (status, json_load(text))
else:
return (status, text)
示例2: get_file
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def get_file(url, path):
if path and os.path.exists(path):
buf = _open_output(path, 'ab')
size = os.path.getsize(path)
success_codes = (200, 206)
else:
buf = _open_output(path)
size = 0
success_codes = (200,)
try:
c = _new_curl_object_for_url(url)
c.setopt(c.WRITEFUNCTION, buf.write)
if size > 0:
log.info('transfer of %s will resume at %s bytes', url, size)
c.setopt(c.RESUME_FROM, size)
c.perform()
status_code = int(c.getinfo(HTTP_CODE))
if status_code not in success_codes:
message = GET_FAILED_MESSAGE % (url, status_code)
raise Exception(message)
finally:
buf.close()
示例3: put
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def put(url, data, encoding, headers=None):
"""Make a PUT request to the url, using data in the message body,
with the additional headers, if any"""
if headers is None:
headers = {}
reply = -1 # default, non-http response
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
if len(headers) > 0:
curl.setopt(pycurl.HTTPHEADER, [k + ': ' + v for k, v in list(headers.items())])
curl.setopt(pycurl.PUT, 1)
curl.setopt(pycurl.INFILESIZE, len(data))
databuffer = BytesIO(data.encode(encoding))
curl.setopt(pycurl.READDATA, databuffer)
try:
curl.perform()
reply = curl.getinfo(pycurl.HTTP_CODE)
except Exception:
pass
curl.close()
return reply
示例4: fetch
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def fetch(self, request, **kwargs):
"""Executes an HTTPRequest, returning an HTTPResponse.
If an error occurs during the fetch, we raise an HTTPError.
"""
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
buffer = cStringIO.StringIO()
headers = httputil.HTTPHeaders()
try:
_curl_setup_request(self._curl, request, buffer, headers)
self._curl.perform()
code = self._curl.getinfo(pycurl.HTTP_CODE)
effective_url = self._curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
response = HTTPResponse(
request=request, code=code, headers=headers,
buffer=buffer, effective_url=effective_url)
if code < 200 or code >= 300:
raise HTTPError(code, response=response)
return response
except pycurl.error, e:
buffer.close()
raise CurlError(*e)
示例5: __init__
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def __init__(self, url_results):
self.curls = {}
for url in url_results:
result = url_results[url]
if not isinstance(result, tuple):
body = result
http_code = 200
else:
body = result[0]
http_code = result[1]
self.curls[url] = CurlStub(
result=body, infos={pycurl.HTTP_CODE: http_code})
# Use thread local storage to keep the current CurlStub since
# CurlManyStub is passed to multiple threads, but the state needs to be
# local.
self._local = local()
self._local.current = None
示例6: download
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def download(self):
url = "https://github.com/{}/{}/tarball/{}".format(
self.repo_owner, self.repo_name, self.checkout)
conn = self._create_curl_conn(url)
self.tarFile = os.path.join(self.workDir, "source.tar.gz")
with open(self.tarFile, "w") as output:
conn.setopt(pycurl.WRITEFUNCTION, output.write)
conn.perform()
http_code = conn.getinfo(pycurl.HTTP_CODE)
if http_code != 200:
raise Exception("Error downloading archive: response {}".format(http_code))
return self.extract()
示例7: meta
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def meta(self):
"""
Return repository meta data as a dictionary.
"""
result = {}
if self.commitHash is not None:
result['CommitHash'] = self.commitHash
if self.commitMessage is not None:
result['CommitMessage'] = self.commitMessage
# If set, self.commitHash may be more specific than self.checkout (e.g.
# commit hash vs. branch name). It is better to use the most specific
# one to query for meta data.
checkout = self.commitHash
if checkout is None:
checkout = self.checkout
url = "https://api.github.com/repos/{owner}/{repo}/commits/{sha}".format(
owner=self.repo_owner, repo=self.repo_name, sha=checkout)
conn = self._create_curl_conn(url)
response = six.StringIO()
conn.setopt(pycurl.WRITEFUNCTION, response.write)
conn.perform()
http_code = conn.getinfo(pycurl.HTTP_CODE)
if http_code == 200:
data = json.loads(response.getvalue())
result['Commit'] = data['commit']
result['CommitMessage'] = data['commit']['message']
return result
示例8: _finish
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
# the various curl timings are documented at
# http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
time_info = dict(
queue=info["curl_start_time"] - info["request"].start_time,
namelookup=curl.getinfo(pycurl.NAMELOOKUP_TIME),
connect=curl.getinfo(pycurl.CONNECT_TIME),
pretransfer=curl.getinfo(pycurl.PRETRANSFER_TIME),
starttransfer=curl.getinfo(pycurl.STARTTRANSFER_TIME),
total=curl.getinfo(pycurl.TOTAL_TIME),
redirect=curl.getinfo(pycurl.REDIRECT_TIME),
)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
reason=info['headers'].get("X-Http-Reason", None),
request_time=time.time() - info["curl_start_time"],
time_info=time_info))
except Exception:
self.handle_callback_exception(info["callback"])
示例9: _finish
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
# the various curl timings are documented at
# http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
time_info = dict(
queue=info["curl_start_time"] - info["request"].start_time,
namelookup=curl.getinfo(pycurl.NAMELOOKUP_TIME),
connect=curl.getinfo(pycurl.CONNECT_TIME),
pretransfer=curl.getinfo(pycurl.PRETRANSFER_TIME),
starttransfer=curl.getinfo(pycurl.STARTTRANSFER_TIME),
total=curl.getinfo(pycurl.TOTAL_TIME),
redirect=curl.getinfo(pycurl.REDIRECT_TIME),
)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
request_time=time.time() - info["curl_start_time"],
time_info=time_info))
except Exception:
self.handle_callback_exception(info["callback"])
示例10: post_file
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def post_file(url, path):
if not os.path.exists(path):
# pycurl doesn't always produce a great exception for this,
# wrap it in a better one.
message = NO_SUCH_FILE_MESSAGE % (path, url)
raise Exception(message)
c = _new_curl_object_for_url(url)
c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, path.encode('ascii')))])
c.perform()
status_code = c.getinfo(HTTP_CODE)
if int(status_code) != 200:
message = POST_FAILED_MESSAGE % (url, status_code)
raise Exception(message)
示例11: _finish
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
# the various curl timings are documented at
# http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
time_info = dict(
queue=info["curl_start_time"] - info["request"].start_time,
namelookup=curl.getinfo(pycurl.NAMELOOKUP_TIME),
connect=curl.getinfo(pycurl.CONNECT_TIME),
pretransfer=curl.getinfo(pycurl.PRETRANSFER_TIME),
starttransfer=curl.getinfo(pycurl.STARTTRANSFER_TIME),
total=curl.getinfo(pycurl.TOTAL_TIME),
redirect=curl.getinfo(pycurl.REDIRECT_TIME),
)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
request_time=monotime() - info["curl_start_time"],
time_info=time_info))
except Exception:
self.handle_callback_exception(info["callback"])
示例12: _finish
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
body = None
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
request_time=time.time() - info["start_time"]))
except (KeyboardInterrupt, SystemExit):
raise
except:
logging.error("Exception in callback %r", info["callback"],
exc_info=True)
示例13: load_url
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def load_url(url, shape=(3, 256, 256)):
if shape != (3, 256, 256):
shape = shape[0][0], shape[1][0], shape[2][0]
""" Loads a tms png inside a thread and returns as an ndarray """
thread_id = threading.current_thread().ident
_curl = _curl_pool[thread_id]
_curl.setopt(_curl.URL, url)
_curl.setopt(pycurl.NOSIGNAL, 1)
# cert errors on windows
_curl.setopt(pycurl.CAINFO, certifi.where())
_, ext = os.path.splitext(urlparse(url).path)
with NamedTemporaryFile(prefix="gbdxtools", suffix=ext, delete=False) as temp: # TODO: apply correct file extension
_curl.setopt(_curl.WRITEDATA, temp.file)
_curl.perform()
code = _curl.getinfo(pycurl.HTTP_CODE)
try:
if(code != 200):
raise TypeError("Request for {} returned unexpected error code: {}".format(url, code))
temp.file.flush()
temp.close()
arr = np.rollaxis(imread(temp.name), 2, 0)
except Exception as e:
print(e)
arr = np.zeros(shape, dtype=np.uint8)
_curl.close()
del _curl_pool[thread_id]
finally:
temp.close()
os.remove(temp.name)
return arr
示例14: _check_status_code
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def _check_status_code(self):
if self.status_code == 0:
self.status_code = self.curl.getinfo(pycurl.HTTP_CODE)
if self.status_code != 0 and self.status_code != 200:
raise Exception(str(self.status_code) + ' ' + self.url)
示例15: test_non_200_result
# 需要導入模塊: import pycurl [as 別名]
# 或者: from pycurl import HTTP_CODE [as 別名]
def test_non_200_result(self):
curl = CurlStub(b"result", {pycurl.HTTP_CODE: 404})
try:
fetch("http://example.com", curl=curl)
except HTTPCodeError as error:
self.assertEqual(error.http_code, 404)
self.assertEqual(error.body, b"result")
else:
self.fail("HTTPCodeError not raised")