Python pycurl.READFUNCTION Examples

The following are 5 code examples of pycurl.READFUNCTION(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pycurl , or try the search function .
Example #1
Source File: client.py    From pycopia with Apache License 2.0 6 votes vote down vote up
def get_raw_poster(self):
        """Initialze a Curl object for a single POST request.

        This sends whatever data you give it, without specifying the content
        type.

        Returns a tuple of initialized Curl and HTTPResponse objects.
        """
        ld = len(self._data)
        c = pycurl.Curl()
        resp = HTTPResponse(self._encoding)
        c.setopt(c.URL, str(self._url))
        c.setopt(pycurl.POST, 1)
        c.setopt(c.READFUNCTION, DataWrapper(self._data).read)
        c.setopt(c.POSTFIELDSIZE, ld)
        c.setopt(c.WRITEFUNCTION, resp._body_callback)
        c.setopt(c.HEADERFUNCTION, resp._header_callback)
        headers = self._headers[:]
        headers.append(httputils.ContentType("")) # removes libcurl internal header
        headers.append(httputils.ContentLength(str(ld)))
        c.setopt(c.HTTPHEADER, map(str, headers))
        self._set_common(c)
        return c, resp 
Example #2
Source File: client.py    From pycopia with Apache License 2.0 6 votes vote down vote up
def get_uploader(self):
        """Initialze a Curl object for a single PUT request.

        Returns a tuple of initialized Curl and HTTPResponse objects.
        """
        c = pycurl.Curl()
        resp = HTTPResponse(self._encoding)
        c.setopt(pycurl.UPLOAD, 1) # does an HTTP PUT
        data = self._data.get("PUT", "")
        filesize = len(data)
        c.setopt(pycurl.READFUNCTION, DataWrapper(data).read)
        c.setopt(pycurl.INFILESIZE, filesize)
        c.setopt(c.WRITEFUNCTION, resp._body_callback)
        c.setopt(c.HEADERFUNCTION, resp._header_callback)
        # extra options to avoid hanging forever
        c.setopt(c.URL, str(self._url))
        c.setopt(c.HTTPHEADER, map(str, self._headers))
        self._set_common(c)
        return c, resp 
Example #3
Source File: test_fetch.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def test_post_data(self):
        curl = CurlStub(b"result")
        result = fetch("http://example.com", post=True, data="data", curl=curl)
        self.assertEqual(result, b"result")
        self.assertEqual(curl.options[pycurl.READFUNCTION](), b"data")
        self.assertEqual(curl.options,
                         {pycurl.URL: b"http://example.com",
                          pycurl.FOLLOWLOCATION: 1,
                          pycurl.MAXREDIRS: 5,
                          pycurl.CONNECTTIMEOUT: 30,
                          pycurl.LOW_SPEED_LIMIT: 1,
                          pycurl.LOW_SPEED_TIME: 600,
                          pycurl.NOSIGNAL: 1,
                          pycurl.WRITEFUNCTION: Any(),
                          pycurl.POST: True,
                          pycurl.POSTFIELDSIZE: 4,
                          pycurl.READFUNCTION: Any(),
                          pycurl.DNS_CACHE_TIMEOUT: 0,
                          pycurl.ENCODING: b"gzip,deflate"}) 
Example #4
Source File: curl.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def set_binary_data(self):
        """
        Set binary data to be transmitted attached to POST request.

        `binary_data` is either a bytes string, or a filename, or
        a list of key-value pairs of a multipart form.
        """

        if self.binary_data:

            if type(self.binary_data) is list:
                self.construct_binary_data()
            if type(self.binary_data) is bytes:
                self.binary_data_size = len(self.binary_data)
                self.binary_data_file = BytesIO()
                self.binary_data_file.write(self.binary_data)
                self.binary_data_file.seek(0)
            elif os.path.exists(self.binary_data):
                self.binary_data_size = os.path.getsize(self.binary_data)
                self.binary_data_file = open(self.binary_data, 'rb')

            self.curl.setopt(pycurl.POST, 1)
            self.curl.setopt(pycurl.POSTFIELDSIZE, self.binary_data_size)
            self.curl.setopt(pycurl.READFUNCTION, self.binary_data_file.read)
            self.curl.setopt(pycurl.CUSTOMREQUEST, 'POST')
            self.curl.setopt(pycurl.POSTREDIR, 3)
            self._log('Binary data added to query (not showing).') 
Example #5
Source File: transport.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request(self, url, method, body, headers):
            c = pycurl.Curl()
            c.setopt(pycurl.URL, str(url))
            if 'proxy_host' in self.proxy:
                c.setopt(pycurl.PROXY, self.proxy['proxy_host'])
            if 'proxy_port' in self.proxy:
                c.setopt(pycurl.PROXYPORT, self.proxy['proxy_port'])
            if 'proxy_user' in self.proxy:
                c.setopt(pycurl.PROXYUSERPWD, "%(proxy_user)s:%(proxy_pass)s" % self.proxy)
            self.buf = StringIO()
            c.setopt(pycurl.WRITEFUNCTION, self.buf.write)
            #c.setopt(pycurl.READFUNCTION, self.read)
            #self.body = StringIO(body)
            #c.setopt(pycurl.HEADERFUNCTION, self.header)
            if self.cacert:
                c.setopt(c.CAINFO, str(self.cacert)) 
            c.setopt(pycurl.SSL_VERIFYPEER, self.cacert and 1 or 0)
            c.setopt(pycurl.SSL_VERIFYHOST, self.cacert and 2 or 0)
            c.setopt(pycurl.CONNECTTIMEOUT, self.timeout/6) 
            c.setopt(pycurl.TIMEOUT, self.timeout)
            if method=='POST':
                c.setopt(pycurl.POST, 1)
                c.setopt(pycurl.POSTFIELDS, body)            
            if headers:
                hdrs = ['%s: %s' % (str(k), str(v)) for k, v in headers.items()]
                ##print hdrs
                c.setopt(pycurl.HTTPHEADER, hdrs)
            c.perform()
            ##print "pycurl perform..."
            c.close()
            return {}, self.buf.getvalue()