How to use the pycurl.WRITEFUNCTION function in pycurl

To help you get started, we’ve selected a few pycurl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pycurl / pycurl / tests / test_fileupload.py View on Github external
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
github lorien / grab / tests / pycurl_cookie.py View on Github external
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
github nuxeo / nuxeo / nuxeo-distribution / curl.py View on Github external
self.set_option(pycurl.SSL_VERIFYHOST, 2)
        # Follow redirects in case it wants to take us to a CGI...
        self.set_option(pycurl.FOLLOWLOCATION, 1)
        self.set_option(pycurl.MAXREDIRS, 5)
        self.set_option(pycurl.NOSIGNAL, 1)
        # Setting this option with even a nonexistent file makes libcurl
        # handle cookie capture and playback automatically.
        self.set_option(pycurl.COOKIEFILE, "/dev/null")
        # Set timeouts to avoid hanging too long
        self.set_timeout(30)
        # Use password identification from .netrc automatically
        self.set_option(pycurl.NETRC, 1)
        # Set up a callback to capture the payload
        def payload_callback(x):
            self.payload += x
        self.set_option(pycurl.WRITEFUNCTION, payload_callback)
        def header_callback(x):
            self.hdr += x
        self.set_option(pycurl.HEADERFUNCTION, header_callback)
github Juniper / contrail-server-manager / src / cobbler / python_cobbler / server_post_install.py View on Github external
def send_REST_request(ip, port, object, payload):
    try:
        response = StringIO()
        headers = ["Content-Type:application/json"]
        url = "http://%s:%s/%s" %(
            ip, port, object)
        conn = pycurl.Curl()
        conn.setopt(pycurl.URL, url)
        conn.setopt(pycurl.HTTPHEADER, headers)
        conn.setopt(pycurl.POST, 1)
        conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload))
        conn.setopt(pycurl.CUSTOMREQUEST, "PUT")
        conn.setopt(pycurl.WRITEFUNCTION, response.write)
        conn.perform()
        return response.getvalue()
    except:
        return None
github s0m30ne / pymsf / zoomeye.py View on Github external
def getToken(self):
        user_auth = '{"username": "%s","password": "%s"}' % (self.USERNAME, self.PASSWORD)
        b = StringIO.StringIO()
        c = pycurl.Curl()
        c.setopt(pycurl.URL, "http://api.zoomeye.org/user/login")
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.FOLLOWLOCATION, 1)
        c.setopt(pycurl.CUSTOMREQUEST, "POST")
        c.setopt(pycurl.POSTFIELDS, user_auth)
        c.perform()
        ReturnData = json.loads(b.getvalue())
        self.API_TOKEN = ReturnData['access_token']
        b.close()
        c.close()
github zthxxx / python-Speech_Recognition / WebCurl / WebCurl.py View on Github external
def get_page_data(url, head = None, curl = None):
    stream_buffer = StringIO()
    if not curl:
        curl = pycurl.Curl()
    curl.setopt(pycurl.URL, url)#curl doesn't support unicode
    if head:
        curl.setopt(pycurl.HTTPHEADER,  head)#must be list, not dict
    curl.setopt(pycurl.WRITEFUNCTION, stream_buffer.write)
    curl.setopt(pycurl.CUSTOMREQUEST,"GET")
    curl.setopt(pycurl.CONNECTTIMEOUT, 30)
    curl.setopt(pycurl.TIMEOUT, 30)
    curl.setopt(pycurl.SSL_VERIFYPEER, 0)
    curl.setopt(pycurl.SSL_VERIFYHOST, 0)
    curl.perform()
    page_data =stream_buffer.getvalue()
    stream_buffer.close()
    return page_data
github pulp / pulpcore / puppet-support / src / pulp_puppet / importer / downloaders / web.py View on Github external
"""
        Downloads the content at the given URL into the given destination.
        The object passed into destination must have a method called "update"
        that accepts a single parameter (the buffer that was read).

        :param url: location to download
        :type  url: str

        :param destination: object
        @return:
        """
        curl = self._create_and_configure_curl()
        url = encode_unicode(url) # because of how the config is stored in pulp

        curl.setopt(pycurl.URL, url)
        curl.setopt(pycurl.WRITEFUNCTION, destination.update)
        curl.perform()
        status = curl.getinfo(curl.HTTP_CODE)
        curl.close()

        if status == 401:
            raise exceptions.UnauthorizedException(url)
        elif status == 404:
            raise exceptions.FileNotFoundException(url)
        elif status != 200:
            raise exceptions.FileRetrievalException(url)
github knolleary / twitter-to-mqtt / twitter-to-mqtt.py View on Github external
def startstream(self):
    self.conn = pycurl.Curl()
    self.conn.setopt(pycurl.USERPWD, "%s:%s" % (settings.TWITTER_USER, settings.TWITTER_PASS))
    self.conn.setopt(pycurl.URL, settings.TWITTER_STREAM_URL)
    self.conn.setopt(pycurl.WRITEFUNCTION, self.onstreamreceive)
    self.conn.perform()
github kristopolous / DRR / tools / schedule_scraper.py View on Github external
def url_get(url):
  curl_handle = pycurl.Curl()
  curl_handle.setopt(curl_handle.URL, url)
  curl_handle.setopt(pycurl.WRITEFUNCTION, cback)
  curl_handle.setopt(pycurl.FOLLOWLOCATION, True)

  try:
    curl_handle.perform()

  except Exception as exc:
    logging.warning("Couldn't resolve or connect to %s. %s" % (url, exc))

  curl_handle.close()
github Juniper / contrail-server-manager / src / server_manager / ansible / sm_ansible_utils.py View on Github external
url = url + '?'
                            first = False
                        else:
                            url = url + '&'
                        url = url + ("%s=%s" % (k,v))
                else:
                    url = url + '?' + payload
            if self.logger:
                self.logger.log(self.logger.INFO,
                                "Sending post request to %s" % url)
            conn.setopt(pycurl.URL, url)
            conn.setopt(pycurl.HTTPHEADER, headers)
            conn.setopt(pycurl.POST, 1)
            if urlencode == False:
                conn.setopt(pycurl.POSTFIELDS, '%s'%json.dumps(payload))
            conn.setopt(pycurl.WRITEFUNCTION, response.write)
            conn.perform()
            return response.getvalue()
        except:
            return None