Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:02:43+00:00 2026-05-13T22:02:43+00:00

Is there a way to write a callback function for pycurl’s READFUNCTION that does

  • 0

Is there a way to write a callback function for pycurl’s READFUNCTION that does not return a string? I am planning on sending blocks of binary data via pycurl. i tried writing a callback function that does this:

def read_callback(self, size):
  for block in data:
    yield block

but pycurl exits with an error that says the return type must be a string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:02:44+00:00Added an answer on May 13, 2026 at 10:02 pm

    The “read function” must return a string of bytes — remember, libcurl is a wrapper on an underlying C library, so of course it’s type-picky!-). However, it can perfectly be a binary string of bytes (in Python 2.* at least — I don’t think pycurl works with Python 3 anyway), so of course it can return “blocks of binary data” — as long as they’re encoded into strings of bytes and respect the size constraint.

    What you just encoded, when called, returns a “generator function” — obviously no good. Assuming data is a list of non-empty byte strings, you need to dice and slice it appropriately, returning each time a string of bytes, with return, definitely not yield (!).

    Not sure where that mysterious data of yours comes from — let’s assume you mean self.data instead. Then you need to keep track, in other instance variables, of the current index into self.data and possibly — if the items are longer than size — of the “yet unsent part” of the current piece, too.

    E.g., it could be:

    class MySender(object):
      def __init__(self, data):
        self.data = data
        self.i = 0
        self.rest = 0
      def readfunction(self, size):
        if self.i >= len(self.data):
          return ''
        result = self.data[self.i][self.rest:self.rest+size]
        self.rest += size
        if self.rest >= len(self.data[self.i]):
          self.i += 1
          self.rest = 0
    

    If self.data’s items are other kinds of binary data (not already encoded as byte strings), you can turn them into byte strings e.g. with help from Python library modules such as struct and array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to write something like this with Mojo::UserAgent ,having the possibility
Is there a way to write a single LINQ expression to get the same
Is there any way to write to a text file in Flex 4.6? Its
Is there a way to write in Visual Studio in nice formated way the
Is there any way to write a valid header/link/image? Something like <a href=index.html><h1 id=logo>Company
Is there any way to write Shoes application using IDE (NetBeans for example)? I
Is there a way to write in a app.config file the information about endpoints?
Is there a way to write a Java Swing application with a custom chrome?
Is there a way to write this C/C++ code in Python? a = (b
I have a callback function for another function that loops through the comments of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.