Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'pycurl' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
gc.collect()
##print gc.get_referrers(c)
##print gc.get_objects()
if opts.verbose >= 1:
print "Tracked objects:", len(gc.get_objects())
# The `del' below should delete these 4 objects:
# Curl + internal dict, CurlMulti + internal dict
del c
gc.collect()
if opts.verbose >= 1:
print "Tracked objects:", len(gc.get_objects())
if 1:
# Ensure that the refcounting error in "reset" is fixed:
for i in xrange(100000):
c = Curl()
c.reset()
# /***********************************************************************
# // done
# ************************************************************************/
print "All tests passed."
opts.verbose = opts.verbose - 1
print "Python", sys.version
print "PycURL %s (compiled against 0x%x)" % (pycurl.version, pycurl.COMPILE_LIBCURL_VERSION_NUM)
print "PycURL version info", pycurl.version_info()
print " %s, compiled %s" % (pycurl.__file__, pycurl.COMPILE_DATE)
# /***********************************************************************
# // test misc
# ************************************************************************/
if 1:
c = Curl()
assert c.URL is pycurl.URL
del c
# /***********************************************************************
# // test handles
# ************************************************************************/
# remove an invalid handle: this should fail
if 1:
m = CurlMulti()
c = Curl()
try:
m.remove_handle(c)
except pycurl.error:
pass
else:
header_line = header_line.decode(_CHAR_ENCODING)
if ':' not in header_line:
return
name, value = header_line.split(':', 1)
name = name.strip()
value = value.strip()
name = name.lower()
response.headers[name] = value
buffer = BytesIO()
curl = pycurl.Curl()
curl.reset()
curl.setopt(pycurl.URL, url)
if headers != None:
headers = ["{}: {}".format(str(each[0]), str(each[1])) for each in headers.items()]
curl.setopt(pycurl.HTTPHEADER, headers)
pass
curl.setopt(pycurl.WRITEDATA, buffer)
curl.setopt(pycurl.HTTP_VERSION, pycurl.CURL_HTTP_VERSION_2_0)
if verbose:
curl.setopt(curl.VERBOSE, True)
curl.setopt(pycurl.SSL_VERIFYPEER, 0)
curl.setopt(pycurl.SSL_VERIFYHOST, 0)
curl.setopt(pycurl.HEADERFUNCTION, _header_function)
curl.setopt(pycurl.TIMEOUT, _TIMEOUT_SECS)
return curl, response, buffer
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vi:ts=4:et
# $Id$
import pycurl
m = pycurl.CurlMulti()
m.handles = []
c1 = pycurl.Curl()
c2 = pycurl.Curl()
c1.setopt(c1.URL, 'http://curl.haxx.se')
c2.setopt(c2.URL, 'http://cnn.com')
c2.setopt(c2.FOLLOWLOCATION, 1)
m.add_handle(c1)
m.add_handle(c2)
m.handles.append(c1)
m.handles.append(c2)
num_handles = len(m.handles)
while num_handles:
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
m.select(1.0)
def sendMultipleMetricsThread(self,url,payload,count):
for i in range(0,count):
for j in range(0,self.errorRetry):
cbuffer = StringIO()
c = pycurl.Curl()
c.setopt(pycurl.URL,url )
c.setopt(pycurl.HTTPHEADER, ['X-Postmark-Server-Token: API_TOKEN_HERE','Accept: application/json'])
c.setopt(pycurl.POST, 1)
c.setopt(c.WRITEDATA, cbuffer)
c.setopt(pycurl.POSTFIELDS, payload)
c.perform()
# if len(cbuffer.getvalue()) >0: print buffer.getvalue()
c.close()
if cbuffer.getvalue().find("error") != -1:
# logging.info("sendMultipleMetricsThread: Error Retry "+j)
if j >= self.errorRetry - 1:
logging.info("sendMultipleMetricsThread: Max Error Retry Failure")
time.sleep(1)
continue
break
def sendMultipleMetrics(self,node,tsname,count):
print 'Testing', pycurl.version
# The codeword to upload file.
codeword = "uploadfile"
# This is the tricky part. Just give the filename.
# In actual code - use system independent path delimiter
file = "file=@C:\upload.gif"
# Enter the url to upload the file to
put_url = 'http://mywebsite.com/uploadfile/using/codeword/'
t = Test()
c = pycurl.Curl()
c.setopt(pycurl.URL, put_url)
c.setopt(pycurl.WRITEFUNCTION, t.body_callback)
c.setopt(pycurl.HTTPPOST, [token, file])
c.perform()
c.close()
print t.contents
import pycurl
self.server.response_once['code'] = 302
self.server.response_once['cookies'] = {'foo': 'bar', '1': '2'}.items()
self.server.response_once['headers'] = [
('Location', self.server.get_url())]
self.server.response['get.data'] = 'foo'
buf = BytesIO()
header_buf = BytesIO()
# Configure pycurl instance
# Usually all these crap is automatically handled by the Grab
curl = pycurl.Curl()
curl.setopt(pycurl.URL, self.server.get_url())
curl.setopt(pycurl.WRITEFUNCTION, buf.write)
curl.setopt(pycurl.HEADERFUNCTION, header_buf.write)
curl.setopt(pycurl.FOLLOWLOCATION, 1)
curl.setopt(pycurl.COOKIEFILE, "")
curl.perform()
self.assertEqual(b'foo', buf.getvalue())
#print(curl.getinfo(pycurl.INFO_COOKIELIST))
self.assertEqual(2, len(curl.getinfo(pycurl.INFO_COOKIELIST)))
# Just make another request and check that pycurl has
# submitted two cookies
curl.setopt(pycurl.URL, self.server.get_url())
curl.perform()
self.assertEqual(2, len(self.server.request['cookies']))
# Erase cookies
# basic check of reference counting (use a memory checker like valgrind)
if 1:
c = Curl()
m = CurlMulti()
m.add_handle(c)
del m
m = CurlMulti()
c.close()
del m, c
# basic check of cyclic garbage collection
if 1 and gc:
gc.collect()
c = Curl()
c.m = CurlMulti()
c.m.add_handle(c)
# create some nasty cyclic references
c.c = c
c.c.c1 = c
c.c.c2 = c
c.c.c3 = c.c
c.c.c4 = c.m
c.m.c = c
c.m.m = c.m
c.m.c = c
# delete
gc.collect()
flags = gc.DEBUG_COLLECTABLE | gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_OBJECTS
if opts.verbose >= 1:
flags = flags | gc.DEBUG_STATS
gc.set_debug(flags)
def __init__(self, username=None, password=None):
self.c = pycurl.Curl()
self.c.setopt(pycurl.POST, 1)
self.c.setopt(pycurl.HTTPHEADER, self.xmlrpc_h)
if username != None and password != None:
self.c.setopt(pycurl.USERPWD, '%s:%s' % (username, password))
curl = pycurl.Curl()
curl.setopt = MagicMock()
curl.perform = MagicMock()
curl.getinfo = MagicMock(return_value=200)
curl.close = MagicMock()
pycurl.Curl = MagicMock(return_value=curl)
method = 'post'
url = 'http://localhost:4502/.cqactions.html'
params = {'foo1': 'bar1', 'foo2': ['bar2a', 'bar2b']}
handlers = {200: self._handler_dummy}
result = pyaem2.bagofrequests.request(method, url, params, handlers)
curl.setopt.assert_any_call(pycurl.POST, 1)
curl.setopt.assert_any_call(pycurl.POSTFIELDS, 'foo1=bar1&foo2=bar2a&foo2=bar2b')
curl.setopt.assert_any_call(pycurl.URL, 'http://localhost:4502/.cqactions.html')
curl.setopt.assert_any_call(pycurl.FOLLOWLOCATION, 1)
curl.setopt.assert_any_call(pycurl.FRESH_CONNECT, 1)
# 6 calls including the one with pycurl.WRITEFUNCTION
self.assertEqual(curl.setopt.call_count, 6)
curl.perform.assert_called_once_with()
curl.getinfo.assert_called_once_with(pycurl.HTTP_CODE)
curl.close.assert_called_once_with()
self.assertEqual(result.is_success(), True)
self.assertEqual(result.message, 'some dummy message')
self.assertEqual(result.response['request']['method'], 'post')
self.assertEqual(result.response['request']['url'], 'http://localhost:4502/.cqactions.html')
self.assertEqual(result.response['request']['params'], params)