سؤال

When using pycurl what is the best way to see what options (opt) have been set?

For example, I call a method which returns a pycurl object. What I would like to do is make a call to see what opts (pycurl.setopts) have been set.

هل كانت مفيدة؟

المحلول

I don't think there's such an option. But it would be easy to encapsulate the curl object into another one that would be like:

class ExtendedCurl:
    invpycurl = {v:k for k, v in pycurl.__dict__.items()}
    def __init__(self):
        self._pycurl = pycurl.Curl()
        self._options = {}
    def setopt(self, opt, val):
        self._pycurl.setopt(self, opt, val)
        self._options[opt] = val
    def unsetopt(self, opt):
        self._pycurl.setopt(self, opt, val)
        del self._options[opt]
    def reset(self):
        self._pycurl.setopt(self, opt, val)
        self._options = dict()
    def getopts(self):
        for opt, val in self._options.iteritems():
            if opt in self.invpycurl.keys():
                print "{}: {}".formart(opt, val)
    def errstr(self):
        return self._pycurl.errstr()
    def perform(self):
        return self._pycurl.perform()
    def close(self):
        return self._pycurl.close()

And based on that, you could even make the pycurl interface nicer, by adding methods like __enter__ and __exit__ (for with statement support), support the pycurl.URL option in the constructor etc..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top