示例#1
0
def upload(url, path, fields=None, headers=None, use_json=False):
    if modutil.exists('pycurl'):
        import pycurl
        url = stringutil.u2s(url)
        path = stringutil.u2s(path)
        if not os.path.exists(path):
            raise Exception('File "%s" not found.' % path)
        buffer = StringIO.StringIO()
        c = pycurl.Curl()
        c.setopt(c.POST, 1)
        c.setopt(c.HEADER, 1)
        c.setopt(c.WRITEFUNCTION, buffer.write)
        c.setopt(c.URL, url)
        field_list = [('file', (c.FORM_FILE, path))]
        if fields is not None:
            for k,v in fields.items():
                if use_json:
                    v = json2.dumps(v)
                field_list.append((k, v))
        c.setopt(c.HTTPPOST, field_list)
        if headers is not None:
            header_list = []
            for k, v in headers.items():
                header = '%s: %s' % (k, v)
                header = str(header) # header may be unicode, which is un-acceptable for curl
                header_list.append(header)
            c.setopt(c.HTTPHEADER, header_list)
        c.perform()
        code = c.getinfo(pycurl.HTTP_CODE)
        c.close()
        if code != 200:
            raise CodedError('UPLOAD_FAILED', 'Failed to upload file %s.' % path)
        resp = buffer.getvalue()
        if use_json:
            json = resp.splitlines()[-1]
            json = unicode(json, 'utf-8')
            resp = json2.loads(json)
        return resp
    else:
        raise ModNotFoundError('pycurl')
示例#2
0
 def handle(self, path):
     try:
         path = stringutil.u2s(path)
         handler = self._load_handler(path)
         headers = self._read_headers()
         cookies = self._read_cookies()
         fields = self._read_fields()
         for k, v in fields.items():
             log.info('Arg %s=%s' % (k, v))
         with self._build_context(path, fields, headers, cookies) as ctx:
             ctx.header('Cache-Control', 'max-age=0,no-cache,no-store')
             return handler.handle(fields)
     except Response, resp:
         return resp
示例#3
0
 def handle(self, path):
     try:
         path = stringutil.u2s(path)
         handler = self._load_handler(path)
         headers = self._read_headers()
         cookies = self._read_cookies()
         fields = self._read_fields()
         for k, v in fields.items():
             log.info('Arg %s=%s' % (k, v))
         with self._build_context(path, fields, headers,
                                  cookies) as ctx:
             ctx.header('Cache-Control', 'max-age=0,no-cache,no-store')
             return handler.handle(fields)
     except Response, resp:
         return resp