0

Firs the code:

def add_credentials(request, username, password):
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)
    return request


def send(options, data, cb):
    if type(data).__name__ == "function":
        cb = data
        data = None
        print "YES"
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    results = {}
    url = "http://%s:%s" % (options['host'], str(options['port']))
    url += "%s" % options['path']
    if data is not None:
        request = urllib2.Request(url, data=data)
    else:
        request = urllib2.Request(url)
    request.add_header("Content-Type", "application/json")
    if 'username' in options and "password" in options:
        add_credentials(request, options['username'], options['password'])
    request.get_method = lambda: options['method'].upper()
    content = opener.open(request)
    for get_json in content:
        results.update(json.loads(get_json))
    exec "cb(results)"

options = {"host": 'localhost', "port": 5984, "path": '/hello-world/mangos', "method": "GET"}



def cb(p):
    print p

send(options, cb, "")

I'm building a CouchDB client and the problem is that when I execute my file I get the error:

SyntaxError: unqualified exec is not allowed in function 'send' it contains a nested function with free variables

I do not really know what this happens, I've searched at google and nothing seems fix my problem :/ Thank's for your answers D:

Victor Castillo Torres
  • 10,581
  • 7
  • 40
  • 50

0 Answers0